#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 100005;
struct Edge {
int u, v, w;
} edge[2 * MAXN];
ii minw[3];
vector<ii> adj[MAXN];
int deg[MAXN], wEdge[MAXN], maxw, numNode, numEdge;
bool dp[1003][1003], check_sub1, check_sub2;
void init(int _N, int _M, vector<int> _U, vector<int> _V, vector<int> _W) {
numNode = _N, numEdge = _M;
minw[0] = minw[1] = minw[2] = {1e9+7, -1};
check_sub2 = (numEdge + 1 == numNode);
for (int i = 0; i < numEdge; ++i) {
edge[i] = {_U[i], _V[i], _W[i]};
adj[edge[i].u].push_back(ii(edge[i].v, edge[i].w));
adj[edge[i].v].push_back(ii(edge[i].u, edge[i].w));
check_sub2 &= (edge[i].u == 0);
maxw = max(maxw, edge[i].w);
wEdge[edge[i].v] = edge[i].w;
if(minw[0].fi > edge[i].w) {
minw[2] = minw[1], minw[1] = minw[0];
minw[0] = {edge[i].w, edge[i].v};
} else
if(minw[1].fi > edge[i].w) {
minw[2] = minw[1];
minw[1] = {edge[i].w, edge[i].v};
} else
if(minw[2].fi > edge[i].w) {
minw[2] = {edge[i].w, edge[i].v};
}
}
check_sub1 = 1;
for (int i = 0; i < numNode; ++i)
check_sub1 &= (sz(adj[i]) <= 2);
sort(edge, edge + numEdge, [](const Edge &a, const Edge &b) {
return (a.w < b.w);
});
}
int sub2(int x, int y) {
if(numNode <= 3)
return -1;
int res = max(wEdge[x], wEdge[y]), cnt(1 + (x == 0));
for (int k = 0; k < 3; ++k) {
if(minw[k].se != x && minw[k].se != y) {
if(--cnt == 0) {
res = max(res, minw[k].fi);
break;
}
}
}
return res;
}
int sub3(int x, int y) {
int l(0), r(numEdge - 1), ans(-1);
while(l <= r) {
int mid = (l + r) >> 1;
for (int i = 0; i < numNode; ++i) {
for (int j = 0; j < numNode; ++j)
dp[i][j] = 0;
}
queue<ii> qu;
qu.push(ii(x, y));
dp[x][y] = 1;
int xt(x), yt(y);
bool check(0);
while(sz(qu)) {
int x(qu.front().fi), y(qu.front().se);
qu.pop();
if(x == yt && y == xt) {
check = 1;
break;
}
for (int it = 0; it < sz(adj[x]); ++it) {
int w(adj[x][it].fi), c(adj[x][it].se);
if(c <= edge[mid].w && !dp[w][y] && w != y) {
dp[w][y] = 1;
qu.push(ii(w, y));
}
}
for (int it = 0; it < sz(adj[y]); ++it) {
int w(adj[y][it].fi), c(adj[y][it].se);
if(c <= edge[mid].w && !dp[x][w] && x != w) {
dp[x][w] = 1;
qu.push(ii(x, w));
}
}
}
if(check) {
ans = edge[mid].w;
r = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
int getMinimumFuelCapacity(int x, int y) {
if(check_sub1)
return (numNode == numEdge) ? maxw : -1;
if(check_sub2)
return sub2(x, y);
if(numNode <= 1000)
return sub3(x, y);
return -1;
}
#ifdef Nhoksocqt1
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "swap"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
vector<int> _U, _V, _W;
int _N, _M, _Q;
cin >> _N >> _M >> _Q;
_U.resize(_M), _V.resize(_M), _W.resize(_M);
for (int i = 0; i < _M; ++i) {
cin >> _U[i] >> _V[i] >> _W[i];
}
init(_N, _M, _U, _V, _W);
for (int t = 0; t < _Q; ++t) {
int _X, _Y;
cin >> _X >> _Y;
cout << "MINIMUM FUEL CAPACITY " << _X << " TO " << _Y << ": " << getMinimumFuelCapacity(_X, _Y) << '\n';
}
return 0;
}
#endif // Nhoksocqt1
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
32 ms |
10864 KB |
Output is correct |
10 |
Correct |
38 ms |
11868 KB |
Output is correct |
11 |
Correct |
37 ms |
11608 KB |
Output is correct |
12 |
Correct |
41 ms |
11864 KB |
Output is correct |
13 |
Correct |
51 ms |
12124 KB |
Output is correct |
14 |
Correct |
35 ms |
10840 KB |
Output is correct |
15 |
Correct |
87 ms |
13532 KB |
Output is correct |
16 |
Correct |
79 ms |
13572 KB |
Output is correct |
17 |
Correct |
83 ms |
13752 KB |
Output is correct |
18 |
Correct |
95 ms |
13756 KB |
Output is correct |
19 |
Correct |
42 ms |
10856 KB |
Output is correct |
20 |
Correct |
81 ms |
14700 KB |
Output is correct |
21 |
Correct |
80 ms |
14868 KB |
Output is correct |
22 |
Correct |
89 ms |
15132 KB |
Output is correct |
23 |
Correct |
89 ms |
15036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
79 ms |
15424 KB |
Output is correct |
4 |
Correct |
80 ms |
15664 KB |
Output is correct |
5 |
Correct |
83 ms |
15692 KB |
Output is correct |
6 |
Correct |
75 ms |
15784 KB |
Output is correct |
7 |
Correct |
79 ms |
15688 KB |
Output is correct |
8 |
Correct |
76 ms |
15444 KB |
Output is correct |
9 |
Correct |
88 ms |
15656 KB |
Output is correct |
10 |
Correct |
78 ms |
15492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
127 ms |
4956 KB |
Output is correct |
11 |
Correct |
137 ms |
4784 KB |
Output is correct |
12 |
Correct |
108 ms |
4696 KB |
Output is correct |
13 |
Correct |
106 ms |
4780 KB |
Output is correct |
14 |
Correct |
172 ms |
4700 KB |
Output is correct |
15 |
Correct |
59 ms |
4796 KB |
Output is correct |
16 |
Correct |
141 ms |
4696 KB |
Output is correct |
17 |
Correct |
157 ms |
4696 KB |
Output is correct |
18 |
Correct |
232 ms |
5092 KB |
Output is correct |
19 |
Correct |
210 ms |
5212 KB |
Output is correct |
20 |
Correct |
182 ms |
4700 KB |
Output is correct |
21 |
Correct |
111 ms |
5068 KB |
Output is correct |
22 |
Correct |
85 ms |
5008 KB |
Output is correct |
23 |
Correct |
137 ms |
4956 KB |
Output is correct |
24 |
Correct |
461 ms |
5968 KB |
Output is correct |
25 |
Correct |
373 ms |
6004 KB |
Output is correct |
26 |
Correct |
369 ms |
4948 KB |
Output is correct |
27 |
Correct |
101 ms |
4700 KB |
Output is correct |
28 |
Correct |
585 ms |
6172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
3 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
32 ms |
10864 KB |
Output is correct |
11 |
Correct |
38 ms |
11868 KB |
Output is correct |
12 |
Correct |
37 ms |
11608 KB |
Output is correct |
13 |
Correct |
41 ms |
11864 KB |
Output is correct |
14 |
Correct |
51 ms |
12124 KB |
Output is correct |
15 |
Correct |
127 ms |
4956 KB |
Output is correct |
16 |
Correct |
137 ms |
4784 KB |
Output is correct |
17 |
Correct |
108 ms |
4696 KB |
Output is correct |
18 |
Correct |
106 ms |
4780 KB |
Output is correct |
19 |
Correct |
172 ms |
4700 KB |
Output is correct |
20 |
Correct |
59 ms |
4796 KB |
Output is correct |
21 |
Correct |
141 ms |
4696 KB |
Output is correct |
22 |
Correct |
157 ms |
4696 KB |
Output is correct |
23 |
Correct |
232 ms |
5092 KB |
Output is correct |
24 |
Correct |
210 ms |
5212 KB |
Output is correct |
25 |
Correct |
182 ms |
4700 KB |
Output is correct |
26 |
Correct |
111 ms |
5068 KB |
Output is correct |
27 |
Correct |
85 ms |
5008 KB |
Output is correct |
28 |
Correct |
137 ms |
4956 KB |
Output is correct |
29 |
Correct |
461 ms |
5968 KB |
Output is correct |
30 |
Correct |
373 ms |
6004 KB |
Output is correct |
31 |
Correct |
369 ms |
4948 KB |
Output is correct |
32 |
Correct |
101 ms |
4700 KB |
Output is correct |
33 |
Correct |
585 ms |
6172 KB |
Output is correct |
34 |
Incorrect |
6 ms |
5464 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
32 ms |
10864 KB |
Output is correct |
10 |
Correct |
38 ms |
11868 KB |
Output is correct |
11 |
Correct |
37 ms |
11608 KB |
Output is correct |
12 |
Correct |
41 ms |
11864 KB |
Output is correct |
13 |
Correct |
51 ms |
12124 KB |
Output is correct |
14 |
Correct |
35 ms |
10840 KB |
Output is correct |
15 |
Correct |
87 ms |
13532 KB |
Output is correct |
16 |
Correct |
79 ms |
13572 KB |
Output is correct |
17 |
Correct |
83 ms |
13752 KB |
Output is correct |
18 |
Correct |
95 ms |
13756 KB |
Output is correct |
19 |
Correct |
79 ms |
15424 KB |
Output is correct |
20 |
Correct |
80 ms |
15664 KB |
Output is correct |
21 |
Correct |
83 ms |
15692 KB |
Output is correct |
22 |
Correct |
75 ms |
15784 KB |
Output is correct |
23 |
Correct |
79 ms |
15688 KB |
Output is correct |
24 |
Correct |
76 ms |
15444 KB |
Output is correct |
25 |
Correct |
88 ms |
15656 KB |
Output is correct |
26 |
Correct |
78 ms |
15492 KB |
Output is correct |
27 |
Correct |
127 ms |
4956 KB |
Output is correct |
28 |
Correct |
137 ms |
4784 KB |
Output is correct |
29 |
Correct |
108 ms |
4696 KB |
Output is correct |
30 |
Correct |
106 ms |
4780 KB |
Output is correct |
31 |
Correct |
172 ms |
4700 KB |
Output is correct |
32 |
Correct |
59 ms |
4796 KB |
Output is correct |
33 |
Correct |
141 ms |
4696 KB |
Output is correct |
34 |
Correct |
157 ms |
4696 KB |
Output is correct |
35 |
Correct |
232 ms |
5092 KB |
Output is correct |
36 |
Incorrect |
6 ms |
5464 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
3 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
32 ms |
10864 KB |
Output is correct |
11 |
Correct |
38 ms |
11868 KB |
Output is correct |
12 |
Correct |
37 ms |
11608 KB |
Output is correct |
13 |
Correct |
41 ms |
11864 KB |
Output is correct |
14 |
Correct |
51 ms |
12124 KB |
Output is correct |
15 |
Correct |
35 ms |
10840 KB |
Output is correct |
16 |
Correct |
87 ms |
13532 KB |
Output is correct |
17 |
Correct |
79 ms |
13572 KB |
Output is correct |
18 |
Correct |
83 ms |
13752 KB |
Output is correct |
19 |
Correct |
95 ms |
13756 KB |
Output is correct |
20 |
Correct |
79 ms |
15424 KB |
Output is correct |
21 |
Correct |
80 ms |
15664 KB |
Output is correct |
22 |
Correct |
83 ms |
15692 KB |
Output is correct |
23 |
Correct |
75 ms |
15784 KB |
Output is correct |
24 |
Correct |
79 ms |
15688 KB |
Output is correct |
25 |
Correct |
76 ms |
15444 KB |
Output is correct |
26 |
Correct |
88 ms |
15656 KB |
Output is correct |
27 |
Correct |
78 ms |
15492 KB |
Output is correct |
28 |
Correct |
127 ms |
4956 KB |
Output is correct |
29 |
Correct |
137 ms |
4784 KB |
Output is correct |
30 |
Correct |
108 ms |
4696 KB |
Output is correct |
31 |
Correct |
106 ms |
4780 KB |
Output is correct |
32 |
Correct |
172 ms |
4700 KB |
Output is correct |
33 |
Correct |
59 ms |
4796 KB |
Output is correct |
34 |
Correct |
141 ms |
4696 KB |
Output is correct |
35 |
Correct |
157 ms |
4696 KB |
Output is correct |
36 |
Correct |
232 ms |
5092 KB |
Output is correct |
37 |
Incorrect |
6 ms |
5464 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |