-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfill_UVA10603OJ.cpp
148 lines (115 loc) · 3.23 KB
/
fill_UVA10603OJ.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <bits/stdc++.h>
using namespace std;
const int inf = INT_MAX / 2;
struct segment
{
int j1;
int j2;
int j3;
int total;
bool operator<(const segment & temp) const
{
return total > temp.total;
}
};
segment _segment(int j1, int j2, int j3, int total)
{
segment temp{j1, j2, j3, total}; return temp;
}
int pour(int jm1, int & j1, int jm2, int & j2)
{
if (jm2 - j2 <= j1)
{
int res = jm2 - j2;
j1 -= (jm2 - j2);
j2 = jm2;
return res;
}
else
{
int res = j1;
j2 += j1;
j1 = 0;
return res;
}
}
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int testCase; cin >> testCase;
for (int t = 1; t <= testCase; ++t)
{
int a, b, c, d; cin >> a >> b >> c >> d;
vector<vector<int>> dp(a + 1, vector<int>(b + 1, inf));
dp[0][0] = 0;
vector<int> ans(max(a, max(b, max(c, d))) + 1, inf);
priority_queue<segment> pq; pq.push(_segment(0, 0, c, 0));
while (pq.empty() == false)
{
segment now = pq.top(); pq.pop();
if (now.total >= dp[now.j1][now.j2] && now.total > 0)
{
continue;
}
dp[now.j1][now.j2] = now.total;
ans[now.j1] = min(ans[now.j1], now.total);
ans[now.j2] = min(ans[now.j2], now.total);
ans[now.j3] = min(ans[now.j3], now.total);
segment temp;
if (now.j1 > 0)
{
if (now.j2 < b)
{
temp = now; // 1 -> 2
temp.total += pour(a, temp.j1, b, temp.j2);
pq.push(temp);
}
if (now.j3 < c)
{
temp = now; // 1 -> 3
temp.total += pour(a, temp.j1, c, temp.j3);
pq.push(temp);
}
}
if (now.j2 > 0)
{
if (now.j1 < a)
{
temp = now; // 2 -> 1
temp.total += pour(b, temp.j2, a, temp.j1);
pq.push(temp);
}
if (now.j3 < c)
{
temp = now; // 2 -> 3
temp.total += pour(b, temp.j2, c, temp.j3);
pq.push(temp);
}
}
if (now.j3 > 0)
{
if (now.j1 < a)
{
temp = now; // 3 -> 1
temp.total += pour(c, temp.j3, a, temp.j1);
pq.push(temp);
}
if (now.j2 < b)
{
temp = now; // 3 -> 2
temp.total += pour(c, temp.j3, b, temp.j2);
pq.push(temp);
}
}
}
int _d = d;
while (_d >= 0 && ans[_d] == inf)
{
--_d;
}
cout << ans[_d] << ' ' << _d << '\n';
}
cout.flush();
return 0;
}