#include <bits/stdc++.h>
using namespace std;
// why am I so weak
int n, m;
int d[100055];
typedef pair<int, int> P;
#define ft first
#define sd second
vector<P> g[100055], ng[100055];
int par[100055], _rank[100055];
bool used[100055];
int depth[100055], val[100055];
inline void add_edge(int from, int to, int cost) {
g[from].push_back(P(to, cost));
}
int fin(int x) {
if (par[x] == x) return x;
return fin(par[x]);
}
void unite(int x, int y, int val) {
x = fin(x), y = fin(y);
if (x == y) return;
if (_rank[x] > _rank[y]) swap(x, y);
par[x] = y;
ng[y].push_back(P(x, val));
if (_rank[x] == _rank[y]) _rank[y]++;
}
void dfs(int v, int par = -1, int d = 0) {
depth[v] = d;
for (auto u : ng[v]) if (u.ft != par) {
val[u.ft] = u.sd;
dfs(u.ft, v, d + 1);
}
}
int main() {
scanf("%d %d", &n, &m);
while (m--) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
x--, y--;
add_edge(x, y, z);
add_edge(y, x, z);
}
priority_queue<P, vector<P>, greater<P> > pq;
memset(d, 63, sizeof(d));
int k;
scanf("%d", &k);
while (k--) {
int x;
scanf("%d", &x);
x--;
d[x] = 0;
pq.push(P(0, x));
}
while (!pq.empty()) {
int v = pq.top().sd; pq.pop();
if (used[v]) continue;
used[v] = true;
for (auto p : g[v]) if (d[p.ft] > d[v] + p.sd) {
d[p.ft] = d[v] + p.sd;
pq.push(P(d[p.ft], p.ft));
}
}
for (int i = 0; i < n; i++) {
par[i] = i;
}
memset(used, false, sizeof(used));
vector<int> vec(n);
for (int i = 0; i < n; i++) vec[i] = i;
sort(vec.begin(), vec.end(), [&] (int u, int v) {
return d[u] > d[v];
});
for (auto u : vec) {
used[u] = true;
for (auto p : g[u]) if (used[p.ft]) {
unite(u, p.ft, d[u]);
}
}
dfs(fin(0), -1, 0);
int q;
scanf("%d", &q);
while (q--) {
int x, y;
scanf("%d %d", &x, &y);
x--, y--;
int res = INT_MAX;
while (depth[x] > depth[y]) {
res = min(res, val[x]);
x = par[x];
}
while (depth[y] > depth[x]) {
res = min(res, val[y]);
y = par[y];
}
while (x != y) {
res = min(res, min(val[x], val[y]));
x = par[x], y = par[y];
}
printf("%d\n", res);
}
return 0;
}
Compilation message
plan.cpp: In function 'int main()':
plan.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
plan.cpp:51:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &x, &y, &z);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &k);
~~~~~^~~~~~~~~~
plan.cpp:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &x);
~~~~~^~~~~~~~~~
plan.cpp:111:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
plan.cpp:115:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5544 KB |
Output is correct |
2 |
Correct |
8 ms |
5596 KB |
Output is correct |
3 |
Correct |
7 ms |
5624 KB |
Output is correct |
4 |
Correct |
6 ms |
5468 KB |
Output is correct |
5 |
Correct |
9 ms |
5648 KB |
Output is correct |
6 |
Correct |
9 ms |
5624 KB |
Output is correct |
7 |
Correct |
7 ms |
5496 KB |
Output is correct |
8 |
Correct |
8 ms |
5496 KB |
Output is correct |
9 |
Correct |
8 ms |
5624 KB |
Output is correct |
10 |
Correct |
7 ms |
5540 KB |
Output is correct |
11 |
Correct |
7 ms |
5624 KB |
Output is correct |
12 |
Correct |
8 ms |
5656 KB |
Output is correct |
13 |
Correct |
8 ms |
5624 KB |
Output is correct |
14 |
Correct |
8 ms |
5624 KB |
Output is correct |
15 |
Correct |
8 ms |
5568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5544 KB |
Output is correct |
2 |
Correct |
8 ms |
5596 KB |
Output is correct |
3 |
Correct |
7 ms |
5624 KB |
Output is correct |
4 |
Correct |
6 ms |
5468 KB |
Output is correct |
5 |
Correct |
9 ms |
5648 KB |
Output is correct |
6 |
Correct |
9 ms |
5624 KB |
Output is correct |
7 |
Correct |
7 ms |
5496 KB |
Output is correct |
8 |
Correct |
8 ms |
5496 KB |
Output is correct |
9 |
Correct |
8 ms |
5624 KB |
Output is correct |
10 |
Correct |
7 ms |
5540 KB |
Output is correct |
11 |
Correct |
7 ms |
5624 KB |
Output is correct |
12 |
Correct |
8 ms |
5656 KB |
Output is correct |
13 |
Correct |
8 ms |
5624 KB |
Output is correct |
14 |
Correct |
8 ms |
5624 KB |
Output is correct |
15 |
Correct |
8 ms |
5568 KB |
Output is correct |
16 |
Correct |
172 ms |
12204 KB |
Output is correct |
17 |
Correct |
547 ms |
23952 KB |
Output is correct |
18 |
Correct |
41 ms |
7432 KB |
Output is correct |
19 |
Correct |
148 ms |
13224 KB |
Output is correct |
20 |
Correct |
531 ms |
24040 KB |
Output is correct |
21 |
Correct |
278 ms |
15800 KB |
Output is correct |
22 |
Correct |
151 ms |
12128 KB |
Output is correct |
23 |
Correct |
551 ms |
23888 KB |
Output is correct |
24 |
Correct |
552 ms |
24000 KB |
Output is correct |
25 |
Correct |
555 ms |
23768 KB |
Output is correct |
26 |
Correct |
153 ms |
12972 KB |
Output is correct |
27 |
Correct |
148 ms |
13156 KB |
Output is correct |
28 |
Correct |
158 ms |
13028 KB |
Output is correct |
29 |
Correct |
151 ms |
12972 KB |
Output is correct |
30 |
Correct |
156 ms |
13112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5496 KB |
Output is correct |
2 |
Correct |
6 ms |
5468 KB |
Output is correct |
3 |
Correct |
7 ms |
5540 KB |
Output is correct |
4 |
Correct |
6 ms |
5472 KB |
Output is correct |
5 |
Correct |
6 ms |
5496 KB |
Output is correct |
6 |
Correct |
6 ms |
5496 KB |
Output is correct |
7 |
Correct |
6 ms |
5496 KB |
Output is correct |
8 |
Correct |
6 ms |
5500 KB |
Output is correct |
9 |
Correct |
7 ms |
5496 KB |
Output is correct |
10 |
Correct |
6 ms |
5520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
251 ms |
15200 KB |
Output is correct |
2 |
Correct |
522 ms |
23272 KB |
Output is correct |
3 |
Correct |
503 ms |
23328 KB |
Output is correct |
4 |
Correct |
106 ms |
11244 KB |
Output is correct |
5 |
Correct |
509 ms |
23256 KB |
Output is correct |
6 |
Correct |
538 ms |
23372 KB |
Output is correct |
7 |
Correct |
527 ms |
23344 KB |
Output is correct |
8 |
Correct |
510 ms |
23240 KB |
Output is correct |
9 |
Correct |
522 ms |
23260 KB |
Output is correct |
10 |
Correct |
531 ms |
23336 KB |
Output is correct |
11 |
Correct |
521 ms |
23352 KB |
Output is correct |
12 |
Correct |
559 ms |
23416 KB |
Output is correct |
13 |
Correct |
516 ms |
23272 KB |
Output is correct |
14 |
Correct |
510 ms |
23436 KB |
Output is correct |
15 |
Correct |
506 ms |
24316 KB |
Output is correct |
16 |
Correct |
512 ms |
23304 KB |
Output is correct |
17 |
Correct |
503 ms |
23292 KB |
Output is correct |
18 |
Correct |
501 ms |
23376 KB |
Output is correct |
19 |
Correct |
114 ms |
11228 KB |
Output is correct |
20 |
Correct |
519 ms |
23384 KB |
Output is correct |
21 |
Correct |
499 ms |
23112 KB |
Output is correct |
22 |
Correct |
110 ms |
12648 KB |
Output is correct |
23 |
Correct |
126 ms |
12388 KB |
Output is correct |
24 |
Correct |
105 ms |
12644 KB |
Output is correct |
25 |
Correct |
105 ms |
12648 KB |
Output is correct |
26 |
Correct |
152 ms |
12476 KB |
Output is correct |
27 |
Correct |
122 ms |
11228 KB |
Output is correct |
28 |
Correct |
113 ms |
12624 KB |
Output is correct |
29 |
Correct |
105 ms |
11088 KB |
Output is correct |
30 |
Correct |
111 ms |
12648 KB |
Output is correct |
31 |
Correct |
112 ms |
11324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5544 KB |
Output is correct |
2 |
Correct |
8 ms |
5596 KB |
Output is correct |
3 |
Correct |
7 ms |
5624 KB |
Output is correct |
4 |
Correct |
6 ms |
5468 KB |
Output is correct |
5 |
Correct |
9 ms |
5648 KB |
Output is correct |
6 |
Correct |
9 ms |
5624 KB |
Output is correct |
7 |
Correct |
7 ms |
5496 KB |
Output is correct |
8 |
Correct |
8 ms |
5496 KB |
Output is correct |
9 |
Correct |
8 ms |
5624 KB |
Output is correct |
10 |
Correct |
7 ms |
5540 KB |
Output is correct |
11 |
Correct |
7 ms |
5624 KB |
Output is correct |
12 |
Correct |
8 ms |
5656 KB |
Output is correct |
13 |
Correct |
8 ms |
5624 KB |
Output is correct |
14 |
Correct |
8 ms |
5624 KB |
Output is correct |
15 |
Correct |
8 ms |
5568 KB |
Output is correct |
16 |
Correct |
172 ms |
12204 KB |
Output is correct |
17 |
Correct |
547 ms |
23952 KB |
Output is correct |
18 |
Correct |
41 ms |
7432 KB |
Output is correct |
19 |
Correct |
148 ms |
13224 KB |
Output is correct |
20 |
Correct |
531 ms |
24040 KB |
Output is correct |
21 |
Correct |
278 ms |
15800 KB |
Output is correct |
22 |
Correct |
151 ms |
12128 KB |
Output is correct |
23 |
Correct |
551 ms |
23888 KB |
Output is correct |
24 |
Correct |
552 ms |
24000 KB |
Output is correct |
25 |
Correct |
555 ms |
23768 KB |
Output is correct |
26 |
Correct |
153 ms |
12972 KB |
Output is correct |
27 |
Correct |
148 ms |
13156 KB |
Output is correct |
28 |
Correct |
158 ms |
13028 KB |
Output is correct |
29 |
Correct |
151 ms |
12972 KB |
Output is correct |
30 |
Correct |
156 ms |
13112 KB |
Output is correct |
31 |
Correct |
6 ms |
5496 KB |
Output is correct |
32 |
Correct |
6 ms |
5468 KB |
Output is correct |
33 |
Correct |
7 ms |
5540 KB |
Output is correct |
34 |
Correct |
6 ms |
5472 KB |
Output is correct |
35 |
Correct |
6 ms |
5496 KB |
Output is correct |
36 |
Correct |
6 ms |
5496 KB |
Output is correct |
37 |
Correct |
6 ms |
5496 KB |
Output is correct |
38 |
Correct |
6 ms |
5500 KB |
Output is correct |
39 |
Correct |
7 ms |
5496 KB |
Output is correct |
40 |
Correct |
6 ms |
5520 KB |
Output is correct |
41 |
Correct |
251 ms |
15200 KB |
Output is correct |
42 |
Correct |
522 ms |
23272 KB |
Output is correct |
43 |
Correct |
503 ms |
23328 KB |
Output is correct |
44 |
Correct |
106 ms |
11244 KB |
Output is correct |
45 |
Correct |
509 ms |
23256 KB |
Output is correct |
46 |
Correct |
538 ms |
23372 KB |
Output is correct |
47 |
Correct |
527 ms |
23344 KB |
Output is correct |
48 |
Correct |
510 ms |
23240 KB |
Output is correct |
49 |
Correct |
522 ms |
23260 KB |
Output is correct |
50 |
Correct |
531 ms |
23336 KB |
Output is correct |
51 |
Correct |
521 ms |
23352 KB |
Output is correct |
52 |
Correct |
559 ms |
23416 KB |
Output is correct |
53 |
Correct |
516 ms |
23272 KB |
Output is correct |
54 |
Correct |
510 ms |
23436 KB |
Output is correct |
55 |
Correct |
506 ms |
24316 KB |
Output is correct |
56 |
Correct |
512 ms |
23304 KB |
Output is correct |
57 |
Correct |
503 ms |
23292 KB |
Output is correct |
58 |
Correct |
501 ms |
23376 KB |
Output is correct |
59 |
Correct |
114 ms |
11228 KB |
Output is correct |
60 |
Correct |
519 ms |
23384 KB |
Output is correct |
61 |
Correct |
499 ms |
23112 KB |
Output is correct |
62 |
Correct |
110 ms |
12648 KB |
Output is correct |
63 |
Correct |
126 ms |
12388 KB |
Output is correct |
64 |
Correct |
105 ms |
12644 KB |
Output is correct |
65 |
Correct |
105 ms |
12648 KB |
Output is correct |
66 |
Correct |
152 ms |
12476 KB |
Output is correct |
67 |
Correct |
122 ms |
11228 KB |
Output is correct |
68 |
Correct |
113 ms |
12624 KB |
Output is correct |
69 |
Correct |
105 ms |
11088 KB |
Output is correct |
70 |
Correct |
111 ms |
12648 KB |
Output is correct |
71 |
Correct |
112 ms |
11324 KB |
Output is correct |
72 |
Correct |
296 ms |
15816 KB |
Output is correct |
73 |
Correct |
586 ms |
23832 KB |
Output is correct |
74 |
Correct |
549 ms |
23728 KB |
Output is correct |
75 |
Correct |
597 ms |
23848 KB |
Output is correct |
76 |
Correct |
554 ms |
23848 KB |
Output is correct |
77 |
Correct |
539 ms |
23664 KB |
Output is correct |
78 |
Correct |
547 ms |
23708 KB |
Output is correct |
79 |
Correct |
566 ms |
23816 KB |
Output is correct |
80 |
Correct |
548 ms |
23768 KB |
Output is correct |
81 |
Correct |
563 ms |
23736 KB |
Output is correct |
82 |
Correct |
546 ms |
23672 KB |
Output is correct |
83 |
Correct |
560 ms |
23836 KB |
Output is correct |
84 |
Correct |
580 ms |
23704 KB |
Output is correct |
85 |
Correct |
589 ms |
24720 KB |
Output is correct |
86 |
Correct |
552 ms |
24236 KB |
Output is correct |
87 |
Correct |
554 ms |
24176 KB |
Output is correct |
88 |
Correct |
553 ms |
24724 KB |
Output is correct |
89 |
Correct |
156 ms |
12244 KB |
Output is correct |
90 |
Correct |
575 ms |
24532 KB |
Output is correct |
91 |
Correct |
524 ms |
24344 KB |
Output is correct |
92 |
Correct |
154 ms |
13860 KB |
Output is correct |
93 |
Correct |
180 ms |
13736 KB |
Output is correct |
94 |
Correct |
150 ms |
13544 KB |
Output is correct |
95 |
Correct |
149 ms |
13672 KB |
Output is correct |
96 |
Correct |
192 ms |
13688 KB |
Output is correct |
97 |
Correct |
155 ms |
12348 KB |
Output is correct |
98 |
Correct |
160 ms |
13520 KB |
Output is correct |
99 |
Correct |
192 ms |
12212 KB |
Output is correct |
100 |
Correct |
156 ms |
13924 KB |
Output is correct |