#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define fi first
#define se second
#define new new228
#define pb push_back
#define rank rank228
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
#pragma warning(disable : 4996)
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // st.oreder_of_key();
const int N = 100100;
const int MAXN = 4 * N;
const int INF = 1e9 + 7;
const int MOD = 998244353;
int TN = 1;
int n, m, k, q;
int a[N], d[N], c[N], f[20][20];
vector<pair<int, int>> verr[N];
void subtask12() {
for (int i = 1; i <= n; i++) {
d[i] = INF;
}
set<pair<int, int>> st;
for (int i = 1; i <= k; i++) {
d[a[i]] = 0;
st.insert({d[a[i]], a[i]});
}
while (!st.empty()) {
int x = st.begin()->se; st.erase(st.begin());
for (auto i: verr[x]) {
int to = i.fi, len = i.se;
if (d[x] + len < d[to]) {
st.erase({d[to], to});
d[to] = d[x] + len;
st.insert({d[to], to});
}
}
}
while (q--) {
int x, y; scanf("%d %d", &x, &y);
printf("%d\n", min(d[x], d[y]));
}
return;
}
void subtask3() {
//printf("I'm here :)\n");
for (int i = 1; i <= n; i++) {
d[i] = INF;
}
set<pair<int, int>> st;
for (int i = 1; i <= k; i++) {
d[a[i]] = 0;
st.insert({d[a[i]], a[i]});
}
while (!st.empty()) {
int x = st.begin()->se; st.erase(st.begin());
for (auto i: verr[x]) {
int to = i.fi, len = i.se;
if (d[x] + len < d[to]) {
st.erase({d[to], to});
d[to] = d[x] + len;
st.insert({d[to], to});
}
}
}
/*for (int i = 1; i <= n; i++) {
printf("%d ", d[i]);
}
puts("");
*/for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
f[i][j] = -1;
}
}
for (int from = 1; from <= n; from++) {
for (auto to: verr[from]) {
f[from][to.fi] = min(d[from], d[to.fi]);
}
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
//f[i][j] = max(min(d[i], d[j]), min(d[i], min(d[j], d[k])));
f[i][j] = max(f[i][j], min(f[i][k], f[k][j]));
}
}
}
while (q--) {
int x, y;
scanf("%d %d", &x, &y);
printf("%d\n", f[x][y]);
}
return;
}
void subtask4a() {
set<pair<int, int>> st;
for (int i = 1; i <= n; i++) {
d[i] = INF;
}
for (int i = 1; i <= k; i++) {
d[a[i]] = 0;
st.insert({d[a[i]], a[i]});
}
while (!st.empty()) {
int x = st.begin()->se; st.erase(st.begin());
for (auto i: verr[x]) {
int to = i.fi, len = i.se;
if (d[x] + len < d[to]) {
st.erase({d[to], to});
d[to] = d[x] + len;
st.insert({d[to], to});
}
}
}
int x, y;
scanf("%d %d", &x, &y);
st.clear();
d[x] = min(d[x], d[y]);
st.insert({d[x], x});
while (!st.empty()) {
int v = st.begin()->se; st.erase(st.begin());
for (auto i: verr[v]) {
int to = i.fi;
if (d[to] < min(d[v], d[to])) {
st.erase({d[to], to});
d[to] = min(d[v], d[to]);
st.insert({d[to], to});
}
}
}
for (int i = 1; i <= n; i++) {
printf("%d ", d[i]);
}
printf("%d\n", d[y]);
return;
}
void dfs(int x, int val) {
c[x] = 1;
for (auto i: verr[x]) {
if (c[i.fi] || d[i.fi] < val) {
continue;
}
dfs(i.fi, val);
}
}
void subtask4() {
for (int i = 1; i <= n; i++) {
d[i] = INF;
}
set<pair<int, int>> st;
for (int i = 1; i <= k; i++) {
d[a[i]] = 0;
st.insert({d[a[i]], a[i]});
}
while (!st.empty()) {
int x = st.begin()->se; st.erase(st.begin());
for (auto i: verr[x]) {
int to = i.fi, len = i.se;
if (d[x] + len < d[to]) {
st.erase({d[to], to});
d[to] = d[x] + len;
st.insert({d[to], to});
}
}
}
int x, y;
scanf("%d %d", &x, &y);
int l = 0, r = d[x];
while (l <= r) {
int mid = (l + r) >> 1;
fill(c + 1, c + 1 + n, 0);
c[x] = 1;
dfs(x, mid);
if (c[y]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
// printf("%d %d\n", l, r);
/*fill(c + 1, c + 1 + n, 0);
c[x] = 1;
dfs(x, r);
if (c[y]) {
l = r;
}*/
printf("%d\n", --l);
return;
}
signed main() {
// ios_base::sync_with_stdio(0);
// cin.tie(nullptr); cout.tie(nullptr);
// in; out; // cin >> TN;
// while (TN--) { solve(); }
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
int x, y, cost;
scanf("%d %d %d", &x, &y, &cost);
verr[x].pb({y, cost}); verr[y].pb({x, cost});
}
scanf("%d", &k);
for (int i = 1; i <= k; i++) {
scanf("%d", &a[i]);
}
scanf("%d", &q);
if (q == 1) {
subtask4(); return 0;
}
if (n <= 15 && m <= 200 && q <= 200) {
subtask3(); return 0;
}
if (n <= 100000 && m <= 5 * 100000 && q <= 100000) {
subtask12(); return 0;
}
return 0;
}
Compilation message
plan.cpp:22:0: warning: ignoring #pragma warning [-Wunknown-pragmas]
#pragma warning(disable : 4996)
plan.cpp: In function 'void subtask12()':
plan.cpp:58:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x, y; scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
plan.cpp: In function 'void subtask3()':
plan.cpp:109:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
plan.cpp: In function 'void subtask4a()':
plan.cpp:136:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
plan.cpp: In function 'void subtask4()':
plan.cpp:189:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
plan.cpp: In function 'int main()':
plan.cpp:218:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
plan.cpp:221:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &x, &y, &cost);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:224:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &k);
~~~~~^~~~~~~~~~
plan.cpp:226:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
plan.cpp:228:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2808 KB |
Output is correct |
3 |
Correct |
5 ms |
2756 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2856 KB |
Output is correct |
6 |
Correct |
5 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
5 ms |
2680 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2680 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
5 ms |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2808 KB |
Output is correct |
3 |
Correct |
5 ms |
2756 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2856 KB |
Output is correct |
6 |
Correct |
5 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
5 ms |
2680 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2680 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
5 ms |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
Correct |
150 ms |
8040 KB |
Output is correct |
17 |
Correct |
615 ms |
19592 KB |
Output is correct |
18 |
Correct |
40 ms |
4204 KB |
Output is correct |
19 |
Correct |
154 ms |
12060 KB |
Output is correct |
20 |
Correct |
603 ms |
19604 KB |
Output is correct |
21 |
Correct |
287 ms |
12728 KB |
Output is correct |
22 |
Correct |
120 ms |
7032 KB |
Output is correct |
23 |
Correct |
623 ms |
19640 KB |
Output is correct |
24 |
Correct |
613 ms |
19600 KB |
Output is correct |
25 |
Correct |
597 ms |
19504 KB |
Output is correct |
26 |
Correct |
150 ms |
11868 KB |
Output is correct |
27 |
Correct |
151 ms |
11988 KB |
Output is correct |
28 |
Correct |
169 ms |
11884 KB |
Output is correct |
29 |
Correct |
149 ms |
12120 KB |
Output is correct |
30 |
Correct |
152 ms |
12136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2684 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2764 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2652 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
308 ms |
13180 KB |
Output is correct |
2 |
Correct |
808 ms |
21780 KB |
Output is correct |
3 |
Correct |
804 ms |
21720 KB |
Output is correct |
4 |
Correct |
127 ms |
9248 KB |
Output is correct |
5 |
Correct |
818 ms |
21724 KB |
Output is correct |
6 |
Correct |
748 ms |
21640 KB |
Output is correct |
7 |
Correct |
703 ms |
21676 KB |
Output is correct |
8 |
Correct |
660 ms |
21624 KB |
Output is correct |
9 |
Correct |
710 ms |
21544 KB |
Output is correct |
10 |
Correct |
848 ms |
21688 KB |
Output is correct |
11 |
Correct |
876 ms |
21736 KB |
Output is correct |
12 |
Correct |
784 ms |
21588 KB |
Output is correct |
13 |
Correct |
836 ms |
21428 KB |
Output is correct |
14 |
Correct |
840 ms |
21108 KB |
Output is correct |
15 |
Correct |
794 ms |
21356 KB |
Output is correct |
16 |
Correct |
811 ms |
21324 KB |
Output is correct |
17 |
Correct |
711 ms |
21552 KB |
Output is correct |
18 |
Correct |
729 ms |
21680 KB |
Output is correct |
19 |
Correct |
86 ms |
7900 KB |
Output is correct |
20 |
Correct |
831 ms |
21684 KB |
Output is correct |
21 |
Correct |
521 ms |
22764 KB |
Output is correct |
22 |
Correct |
130 ms |
12012 KB |
Output is correct |
23 |
Correct |
166 ms |
7544 KB |
Output is correct |
24 |
Correct |
125 ms |
12016 KB |
Output is correct |
25 |
Correct |
124 ms |
12124 KB |
Output is correct |
26 |
Correct |
120 ms |
8056 KB |
Output is correct |
27 |
Correct |
129 ms |
9336 KB |
Output is correct |
28 |
Correct |
122 ms |
12016 KB |
Output is correct |
29 |
Correct |
143 ms |
9224 KB |
Output is correct |
30 |
Correct |
120 ms |
12008 KB |
Output is correct |
31 |
Correct |
135 ms |
9152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2808 KB |
Output is correct |
3 |
Correct |
5 ms |
2756 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2856 KB |
Output is correct |
6 |
Correct |
5 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
5 ms |
2680 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2680 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
5 ms |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
Correct |
150 ms |
8040 KB |
Output is correct |
17 |
Correct |
615 ms |
19592 KB |
Output is correct |
18 |
Correct |
40 ms |
4204 KB |
Output is correct |
19 |
Correct |
154 ms |
12060 KB |
Output is correct |
20 |
Correct |
603 ms |
19604 KB |
Output is correct |
21 |
Correct |
287 ms |
12728 KB |
Output is correct |
22 |
Correct |
120 ms |
7032 KB |
Output is correct |
23 |
Correct |
623 ms |
19640 KB |
Output is correct |
24 |
Correct |
613 ms |
19600 KB |
Output is correct |
25 |
Correct |
597 ms |
19504 KB |
Output is correct |
26 |
Correct |
150 ms |
11868 KB |
Output is correct |
27 |
Correct |
151 ms |
11988 KB |
Output is correct |
28 |
Correct |
169 ms |
11884 KB |
Output is correct |
29 |
Correct |
149 ms |
12120 KB |
Output is correct |
30 |
Correct |
152 ms |
12136 KB |
Output is correct |
31 |
Correct |
4 ms |
2684 KB |
Output is correct |
32 |
Correct |
4 ms |
2680 KB |
Output is correct |
33 |
Correct |
4 ms |
2680 KB |
Output is correct |
34 |
Correct |
4 ms |
2764 KB |
Output is correct |
35 |
Correct |
4 ms |
2680 KB |
Output is correct |
36 |
Correct |
4 ms |
2652 KB |
Output is correct |
37 |
Correct |
4 ms |
2680 KB |
Output is correct |
38 |
Correct |
4 ms |
2680 KB |
Output is correct |
39 |
Correct |
4 ms |
2680 KB |
Output is correct |
40 |
Correct |
4 ms |
2680 KB |
Output is correct |
41 |
Correct |
308 ms |
13180 KB |
Output is correct |
42 |
Correct |
808 ms |
21780 KB |
Output is correct |
43 |
Correct |
804 ms |
21720 KB |
Output is correct |
44 |
Correct |
127 ms |
9248 KB |
Output is correct |
45 |
Correct |
818 ms |
21724 KB |
Output is correct |
46 |
Correct |
748 ms |
21640 KB |
Output is correct |
47 |
Correct |
703 ms |
21676 KB |
Output is correct |
48 |
Correct |
660 ms |
21624 KB |
Output is correct |
49 |
Correct |
710 ms |
21544 KB |
Output is correct |
50 |
Correct |
848 ms |
21688 KB |
Output is correct |
51 |
Correct |
876 ms |
21736 KB |
Output is correct |
52 |
Correct |
784 ms |
21588 KB |
Output is correct |
53 |
Correct |
836 ms |
21428 KB |
Output is correct |
54 |
Correct |
840 ms |
21108 KB |
Output is correct |
55 |
Correct |
794 ms |
21356 KB |
Output is correct |
56 |
Correct |
811 ms |
21324 KB |
Output is correct |
57 |
Correct |
711 ms |
21552 KB |
Output is correct |
58 |
Correct |
729 ms |
21680 KB |
Output is correct |
59 |
Correct |
86 ms |
7900 KB |
Output is correct |
60 |
Correct |
831 ms |
21684 KB |
Output is correct |
61 |
Correct |
521 ms |
22764 KB |
Output is correct |
62 |
Correct |
130 ms |
12012 KB |
Output is correct |
63 |
Correct |
166 ms |
7544 KB |
Output is correct |
64 |
Correct |
125 ms |
12016 KB |
Output is correct |
65 |
Correct |
124 ms |
12124 KB |
Output is correct |
66 |
Correct |
120 ms |
8056 KB |
Output is correct |
67 |
Correct |
129 ms |
9336 KB |
Output is correct |
68 |
Correct |
122 ms |
12016 KB |
Output is correct |
69 |
Correct |
143 ms |
9224 KB |
Output is correct |
70 |
Correct |
120 ms |
12008 KB |
Output is correct |
71 |
Correct |
135 ms |
9152 KB |
Output is correct |
72 |
Incorrect |
278 ms |
11952 KB |
Output isn't correct |
73 |
Halted |
0 ms |
0 KB |
- |