#include <bits/stdc++.h>
#define int long long
#define N 100005
#define inf 2000000000000000000LL
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n, m, k, q, P[N], id[N], dist[N], rev[N], pai[N], peso[N];
vector<pii> grafo[N];
void dijkstra()
{
priority_queue<pii, vector<pii>, greater<pii> > pq;
for(int i = 1; i <= n; i++) dist[i] = inf;
for(int i = 1; i <= k; i++) dist[ P[i] ] = 0, pq.push({0, P[i]});
while(!pq.empty())
{
int x = pq.top().s, d = pq.top().f;
pq.pop();
if(d > dist[x]) continue;
for(auto v: grafo[x])
{
if(dist[v.f] > dist[x] + v.s)
{
dist[v.f] = dist[x] + v.s;
pq.push({dist[v.f], v.f});
}
}
}
vector<pii> v;
for(int i = 1; i <= n; i++) v.push_back({dist[i], i});
sort(v.begin(), v.end());
for(int i = 0; i < v.size(); i++) id[ v[i].s ] = i + 1, rev[i + 1] = v[i].s;
}
int ok[N];
int Find(int x)
{
if(x == pai[x]) return x;
return pai[x] = Find(pai[x]);
}
void join(int a, int b)
{
a = Find(a), b = Find(b);
if(a == b) return;
if(peso[a] > peso[b]) pai[b] = a;
else if(peso[a] < peso[b]) pai[a] = b;
else pai[a] = b, peso[b] ++;
}
int cor = 1;
int32_t main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>m;
for(int i = 1, a, b, c; i <= m; i++)
{
cin>>a>>b>>c;
grafo[a].push_back({b, c});
grafo[b].push_back({a, c});
}
cin>>k;
for(int i = 1; i <= k; i++) cin>>P[i];
dijkstra();
cin>>q;
for(int t = 1, a, b; t <= q; t++)
{
cin>>a>>b;
int ini = 1, fim = n, mid, best = -1;
for(int x = 1; x <= n; x++) pai[x] = x, peso[x] = 0;
for(int x = n; x >= 1; x--)
{
int xi = rev[x];
for(auto v: grafo[xi])
{
if(id[v.f] > x)
{
join(v.f, xi);
}
}
if(Find(a) == Find(b))
{
cout<< dist[ xi ] <<"\n";
break;
}
}
}
}
Compilation message
plan.cpp: In function 'void dijkstra()':
plan.cpp:48:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++) id[ v[i].s ] = i + 1, rev[i + 1] = v[i].s;
~~^~~~~~~~~~
plan.cpp: In function 'int32_t main()':
plan.cpp:102:7: warning: unused variable 'ini' [-Wunused-variable]
int ini = 1, fim = n, mid, best = -1;
^~~
plan.cpp:102:16: warning: unused variable 'fim' [-Wunused-variable]
int ini = 1, fim = n, mid, best = -1;
^~~
plan.cpp:102:25: warning: unused variable 'mid' [-Wunused-variable]
int ini = 1, fim = n, mid, best = -1;
^~~
plan.cpp:102:30: warning: unused variable 'best' [-Wunused-variable]
int ini = 1, fim = n, mid, best = -1;
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
15 ms |
2808 KB |
Output is correct |
3 |
Correct |
16 ms |
2936 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
16 ms |
2808 KB |
Output is correct |
6 |
Correct |
12 ms |
2936 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 KB |
Output is correct |
8 |
Correct |
4 ms |
2780 KB |
Output is correct |
9 |
Correct |
16 ms |
2936 KB |
Output is correct |
10 |
Correct |
21 ms |
2808 KB |
Output is correct |
11 |
Correct |
19 ms |
2808 KB |
Output is correct |
12 |
Correct |
15 ms |
2808 KB |
Output is correct |
13 |
Correct |
20 ms |
2808 KB |
Output is correct |
14 |
Correct |
15 ms |
2808 KB |
Output is correct |
15 |
Correct |
19 ms |
2936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
15 ms |
2808 KB |
Output is correct |
3 |
Correct |
16 ms |
2936 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
16 ms |
2808 KB |
Output is correct |
6 |
Correct |
12 ms |
2936 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 KB |
Output is correct |
8 |
Correct |
4 ms |
2780 KB |
Output is correct |
9 |
Correct |
16 ms |
2936 KB |
Output is correct |
10 |
Correct |
21 ms |
2808 KB |
Output is correct |
11 |
Correct |
19 ms |
2808 KB |
Output is correct |
12 |
Correct |
15 ms |
2808 KB |
Output is correct |
13 |
Correct |
20 ms |
2808 KB |
Output is correct |
14 |
Correct |
15 ms |
2808 KB |
Output is correct |
15 |
Correct |
19 ms |
2936 KB |
Output is correct |
16 |
Execution timed out |
4096 ms |
12224 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2672 KB |
Output is correct |
2 |
Correct |
4 ms |
2780 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2684 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2764 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
18408 KB |
Output is correct |
2 |
Correct |
484 ms |
34412 KB |
Output is correct |
3 |
Correct |
468 ms |
34296 KB |
Output is correct |
4 |
Correct |
103 ms |
12220 KB |
Output is correct |
5 |
Correct |
508 ms |
35868 KB |
Output is correct |
6 |
Correct |
487 ms |
36072 KB |
Output is correct |
7 |
Correct |
494 ms |
36272 KB |
Output is correct |
8 |
Correct |
478 ms |
36136 KB |
Output is correct |
9 |
Correct |
470 ms |
36176 KB |
Output is correct |
10 |
Correct |
488 ms |
35972 KB |
Output is correct |
11 |
Correct |
485 ms |
36140 KB |
Output is correct |
12 |
Correct |
471 ms |
35748 KB |
Output is correct |
13 |
Correct |
470 ms |
35796 KB |
Output is correct |
14 |
Correct |
477 ms |
35868 KB |
Output is correct |
15 |
Correct |
526 ms |
37852 KB |
Output is correct |
16 |
Correct |
493 ms |
35588 KB |
Output is correct |
17 |
Correct |
462 ms |
35828 KB |
Output is correct |
18 |
Correct |
475 ms |
35828 KB |
Output is correct |
19 |
Correct |
87 ms |
12260 KB |
Output is correct |
20 |
Correct |
481 ms |
35736 KB |
Output is correct |
21 |
Correct |
451 ms |
35816 KB |
Output is correct |
22 |
Correct |
122 ms |
13532 KB |
Output is correct |
23 |
Correct |
112 ms |
12372 KB |
Output is correct |
24 |
Correct |
125 ms |
13552 KB |
Output is correct |
25 |
Correct |
116 ms |
13660 KB |
Output is correct |
26 |
Correct |
120 ms |
12696 KB |
Output is correct |
27 |
Correct |
96 ms |
11704 KB |
Output is correct |
28 |
Correct |
116 ms |
13092 KB |
Output is correct |
29 |
Correct |
103 ms |
11576 KB |
Output is correct |
30 |
Correct |
116 ms |
13180 KB |
Output is correct |
31 |
Correct |
94 ms |
11748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
15 ms |
2808 KB |
Output is correct |
3 |
Correct |
16 ms |
2936 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
16 ms |
2808 KB |
Output is correct |
6 |
Correct |
12 ms |
2936 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 KB |
Output is correct |
8 |
Correct |
4 ms |
2780 KB |
Output is correct |
9 |
Correct |
16 ms |
2936 KB |
Output is correct |
10 |
Correct |
21 ms |
2808 KB |
Output is correct |
11 |
Correct |
19 ms |
2808 KB |
Output is correct |
12 |
Correct |
15 ms |
2808 KB |
Output is correct |
13 |
Correct |
20 ms |
2808 KB |
Output is correct |
14 |
Correct |
15 ms |
2808 KB |
Output is correct |
15 |
Correct |
19 ms |
2936 KB |
Output is correct |
16 |
Execution timed out |
4096 ms |
12224 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |