#include <bits/stdc++.h>
//#include "bits_stdc++.h"
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pi;
ll n, m, k;
ll ans = LLONG_MAX;
vector<pi> adj[100005];
bool special[100005], skip[100005];
vector<ll> dist;
ll par[100005];
ll b1, b2, distan;
void findclosestpair()
{
b1= -1, b2=-1;
distan = LLONG_MAX;
priority_queue<pi, vector<pi>, greater<pi> > pq;
dist.assign(n+1, LLONG_MAX);
for (ll i=1; i<=n; ++i)
{
if (!special[i] || skip[i]) continue;
dist[i] = 0;
pq.push(mp(dist[i], i));
par[i] = i;
}
while (!pq.empty())
{
pi temp = pq.top();
ll from = temp.second;
pq.pop();
//if (dist[from]< temp.f) continue;
for (pi u: adj[from])
{
if (!skip[u.f] && dist[u.f]>dist[from]+u.s)
{
dist[u.f] = dist[from] + u.s;
par[u.f] = par[from];
pq.push(mp(dist[u.f], u.f));
}
}
}
for (ll i=1; i<=n; ++i)
{
if (skip[par[i]]) continue;
for(pi u: adj[i])
{
if (par[u.f]==par[i] || skip[par[u.f]]) continue;
if (dist[i] + u.s + dist[u.f] < distan)
{
distan = dist[i] + u.s + dist[u.f];
b1 = par[i], b2 = par[u.f];
}
}
}
}
vector<ll> dijkstra(ll src)
{
priority_queue<pi, vector<pi>, greater<pi> > pq;
vector<ll> dist(n+1, LLONG_MAX);
dist[src] = 0;
pq.push(mp(0, src));
while (!pq.empty())
{
ll from = pq.top().second;
pq.pop();
for (pi u: adj[from])
{
if (dist[u.f]>dist[from]+u.s)
{
dist[u.f] = dist[from] + u.s;
pq.push(mp(dist[u.f], u.f));
}
}
}
return dist;
}
int main()
{
cin >> n >> m >> k;
for (ll i=0; i<m; ++i)
{
ll a,b,c;
cin >> a >> b >> c;
adj[a].pb(mp(b,c));
adj[b].pb(mp(a,c));
}
for (ll i=1; i<=k; ++i)
{
ll a;
cin >> a;
special[a] = 1;
}
//cout << "done" << endl;
// find x1, x2 (closest pair)
findclosestpair();
// case 1: join (x1, x2) -> find another paur
ll add = distan;
ll a = b1, b = b2;
//cout << a << b << endl;
skip[a] = 1, skip[b] = 1;
findclosestpair();
ans = add+ distan;
//cout << "done" << endl;
//cout << ans << endl;
// case 2: seperate x1 from x2 -> pair both up
vector<ll> fromb1 = dijkstra(a), fromb2 = dijkstra(b);
vector<pi> choose1, choose2;
for (ll i=1; i<=n; ++i)
{
if (i==a || i==b || !special[i]) continue;
choose1.pb(mp(fromb1[i], i));
choose2.pb(mp(fromb2[i], i));
}
//cout << "done" << endl;
sort(choose1.begin(), choose1.end());
sort(choose2.begin(), choose2.end());
if (choose1[0].s == choose2[0].s)
{
//cout << "one" << endl;
//cout << choose1[0].s << endl;
if (choose1[1].f!=LLONG_MAX && choose2[0].f!=LLONG_MAX) ans = min(ans, choose1[1].f + choose2[0].f);
if (choose2[1].f!=LLONG_MAX && choose1[0].f!=LLONG_MAX) ans = min(ans, choose2[1].f + choose1[0].f);
}
else
{
if (choose1[0].f!=LLONG_MAX && choose2[0].f!=LLONG_MAX) ans = min(ans, choose1[0].f + choose2[0].f);
//cout << choose1[0].f << ' ' << choose2[0].f << endl;
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3676 KB |
Output is correct |
3 |
Correct |
1 ms |
3588 KB |
Output is correct |
4 |
Correct |
1 ms |
3588 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3676 KB |
Output is correct |
8 |
Correct |
2 ms |
3676 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3420 KB |
Output is correct |
15 |
Correct |
2 ms |
3420 KB |
Output is correct |
16 |
Correct |
2 ms |
3420 KB |
Output is correct |
17 |
Correct |
1 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
3420 KB |
Output is correct |
19 |
Correct |
2 ms |
3676 KB |
Output is correct |
20 |
Correct |
1 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
3420 KB |
Output is correct |
22 |
Correct |
1 ms |
3416 KB |
Output is correct |
23 |
Correct |
1 ms |
3416 KB |
Output is correct |
24 |
Correct |
1 ms |
3420 KB |
Output is correct |
25 |
Correct |
1 ms |
3592 KB |
Output is correct |
26 |
Correct |
1 ms |
3420 KB |
Output is correct |
27 |
Correct |
1 ms |
3592 KB |
Output is correct |
28 |
Correct |
2 ms |
3676 KB |
Output is correct |
29 |
Correct |
2 ms |
3672 KB |
Output is correct |
30 |
Correct |
2 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3676 KB |
Output is correct |
3 |
Correct |
1 ms |
3588 KB |
Output is correct |
4 |
Correct |
1 ms |
3588 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3676 KB |
Output is correct |
8 |
Correct |
2 ms |
3676 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3420 KB |
Output is correct |
15 |
Correct |
2 ms |
3420 KB |
Output is correct |
16 |
Correct |
2 ms |
3420 KB |
Output is correct |
17 |
Correct |
1 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
3420 KB |
Output is correct |
19 |
Correct |
2 ms |
3676 KB |
Output is correct |
20 |
Correct |
1 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
3420 KB |
Output is correct |
22 |
Correct |
1 ms |
3416 KB |
Output is correct |
23 |
Correct |
1 ms |
3416 KB |
Output is correct |
24 |
Correct |
1 ms |
3420 KB |
Output is correct |
25 |
Correct |
1 ms |
3592 KB |
Output is correct |
26 |
Correct |
1 ms |
3420 KB |
Output is correct |
27 |
Correct |
1 ms |
3592 KB |
Output is correct |
28 |
Correct |
2 ms |
3676 KB |
Output is correct |
29 |
Correct |
2 ms |
3672 KB |
Output is correct |
30 |
Correct |
2 ms |
3676 KB |
Output is correct |
31 |
Correct |
2 ms |
3416 KB |
Output is correct |
32 |
Correct |
2 ms |
3420 KB |
Output is correct |
33 |
Correct |
2 ms |
3420 KB |
Output is correct |
34 |
Correct |
1 ms |
3416 KB |
Output is correct |
35 |
Correct |
1 ms |
3420 KB |
Output is correct |
36 |
Correct |
4 ms |
3676 KB |
Output is correct |
37 |
Correct |
5 ms |
3932 KB |
Output is correct |
38 |
Correct |
2 ms |
3420 KB |
Output is correct |
39 |
Correct |
45 ms |
8940 KB |
Output is correct |
40 |
Correct |
11 ms |
4700 KB |
Output is correct |
41 |
Correct |
2 ms |
3676 KB |
Output is correct |
42 |
Correct |
10 ms |
4884 KB |
Output is correct |
43 |
Correct |
3 ms |
3676 KB |
Output is correct |
44 |
Correct |
2 ms |
3508 KB |
Output is correct |
45 |
Correct |
1 ms |
3420 KB |
Output is correct |
46 |
Correct |
54 ms |
9300 KB |
Output is correct |
47 |
Correct |
8 ms |
4188 KB |
Output is correct |
48 |
Correct |
40 ms |
8988 KB |
Output is correct |
49 |
Correct |
44 ms |
9088 KB |
Output is correct |
50 |
Correct |
2 ms |
3600 KB |
Output is correct |
51 |
Correct |
2 ms |
3676 KB |
Output is correct |
52 |
Correct |
2 ms |
3420 KB |
Output is correct |
53 |
Correct |
25 ms |
6380 KB |
Output is correct |
54 |
Correct |
47 ms |
9040 KB |
Output is correct |
55 |
Correct |
1 ms |
3420 KB |
Output is correct |
56 |
Correct |
1 ms |
3420 KB |
Output is correct |
57 |
Correct |
2 ms |
3672 KB |
Output is correct |
58 |
Correct |
59 ms |
9300 KB |
Output is correct |
59 |
Correct |
1 ms |
3420 KB |
Output is correct |
60 |
Correct |
2 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
10876 KB |
Output is correct |
2 |
Correct |
8 ms |
6492 KB |
Output is correct |
3 |
Correct |
2054 ms |
161928 KB |
Output is correct |
4 |
Correct |
921 ms |
75904 KB |
Output is correct |
5 |
Correct |
209 ms |
17736 KB |
Output is correct |
6 |
Correct |
151 ms |
14996 KB |
Output is correct |
7 |
Correct |
213 ms |
19356 KB |
Output is correct |
8 |
Correct |
70 ms |
11156 KB |
Output is correct |
9 |
Correct |
123 ms |
14128 KB |
Output is correct |
10 |
Correct |
98 ms |
12240 KB |
Output is correct |
11 |
Correct |
2526 ms |
169400 KB |
Output is correct |
12 |
Correct |
117 ms |
12644 KB |
Output is correct |
13 |
Correct |
645 ms |
49580 KB |
Output is correct |
14 |
Correct |
264 ms |
19852 KB |
Output is correct |
15 |
Correct |
2253 ms |
167584 KB |
Output is correct |
16 |
Correct |
53 ms |
10392 KB |
Output is correct |
17 |
Correct |
1479 ms |
120280 KB |
Output is correct |
18 |
Correct |
8 ms |
6492 KB |
Output is correct |
19 |
Correct |
2403 ms |
178956 KB |
Output is correct |
20 |
Correct |
197 ms |
18848 KB |
Output is correct |
21 |
Correct |
176 ms |
16728 KB |
Output is correct |
22 |
Correct |
85 ms |
11780 KB |
Output is correct |
23 |
Correct |
34 ms |
9052 KB |
Output is correct |
24 |
Correct |
1512 ms |
133596 KB |
Output is correct |
25 |
Correct |
133 ms |
14328 KB |
Output is correct |
26 |
Correct |
69 ms |
11136 KB |
Output is correct |
27 |
Correct |
103 ms |
12952 KB |
Output is correct |
28 |
Correct |
20 ms |
7764 KB |
Output is correct |
29 |
Correct |
217 ms |
19132 KB |
Output is correct |
30 |
Correct |
454 ms |
36796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3676 KB |
Output is correct |
3 |
Correct |
1 ms |
3588 KB |
Output is correct |
4 |
Correct |
1 ms |
3588 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3676 KB |
Output is correct |
8 |
Correct |
2 ms |
3676 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3420 KB |
Output is correct |
15 |
Correct |
2 ms |
3420 KB |
Output is correct |
16 |
Correct |
2 ms |
3420 KB |
Output is correct |
17 |
Correct |
1 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
3420 KB |
Output is correct |
19 |
Correct |
2 ms |
3676 KB |
Output is correct |
20 |
Correct |
1 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
3420 KB |
Output is correct |
22 |
Correct |
1 ms |
3416 KB |
Output is correct |
23 |
Correct |
1 ms |
3416 KB |
Output is correct |
24 |
Correct |
1 ms |
3420 KB |
Output is correct |
25 |
Correct |
1 ms |
3592 KB |
Output is correct |
26 |
Correct |
1 ms |
3420 KB |
Output is correct |
27 |
Correct |
1 ms |
3592 KB |
Output is correct |
28 |
Correct |
2 ms |
3676 KB |
Output is correct |
29 |
Correct |
2 ms |
3672 KB |
Output is correct |
30 |
Correct |
2 ms |
3676 KB |
Output is correct |
31 |
Correct |
2 ms |
3416 KB |
Output is correct |
32 |
Correct |
2 ms |
3420 KB |
Output is correct |
33 |
Correct |
2 ms |
3420 KB |
Output is correct |
34 |
Correct |
1 ms |
3416 KB |
Output is correct |
35 |
Correct |
1 ms |
3420 KB |
Output is correct |
36 |
Correct |
4 ms |
3676 KB |
Output is correct |
37 |
Correct |
5 ms |
3932 KB |
Output is correct |
38 |
Correct |
2 ms |
3420 KB |
Output is correct |
39 |
Correct |
45 ms |
8940 KB |
Output is correct |
40 |
Correct |
11 ms |
4700 KB |
Output is correct |
41 |
Correct |
2 ms |
3676 KB |
Output is correct |
42 |
Correct |
10 ms |
4884 KB |
Output is correct |
43 |
Correct |
3 ms |
3676 KB |
Output is correct |
44 |
Correct |
2 ms |
3508 KB |
Output is correct |
45 |
Correct |
1 ms |
3420 KB |
Output is correct |
46 |
Correct |
54 ms |
9300 KB |
Output is correct |
47 |
Correct |
8 ms |
4188 KB |
Output is correct |
48 |
Correct |
40 ms |
8988 KB |
Output is correct |
49 |
Correct |
44 ms |
9088 KB |
Output is correct |
50 |
Correct |
2 ms |
3600 KB |
Output is correct |
51 |
Correct |
2 ms |
3676 KB |
Output is correct |
52 |
Correct |
2 ms |
3420 KB |
Output is correct |
53 |
Correct |
25 ms |
6380 KB |
Output is correct |
54 |
Correct |
47 ms |
9040 KB |
Output is correct |
55 |
Correct |
1 ms |
3420 KB |
Output is correct |
56 |
Correct |
1 ms |
3420 KB |
Output is correct |
57 |
Correct |
2 ms |
3672 KB |
Output is correct |
58 |
Correct |
59 ms |
9300 KB |
Output is correct |
59 |
Correct |
1 ms |
3420 KB |
Output is correct |
60 |
Correct |
2 ms |
3676 KB |
Output is correct |
61 |
Correct |
64 ms |
10876 KB |
Output is correct |
62 |
Correct |
8 ms |
6492 KB |
Output is correct |
63 |
Correct |
2054 ms |
161928 KB |
Output is correct |
64 |
Correct |
921 ms |
75904 KB |
Output is correct |
65 |
Correct |
209 ms |
17736 KB |
Output is correct |
66 |
Correct |
151 ms |
14996 KB |
Output is correct |
67 |
Correct |
213 ms |
19356 KB |
Output is correct |
68 |
Correct |
70 ms |
11156 KB |
Output is correct |
69 |
Correct |
123 ms |
14128 KB |
Output is correct |
70 |
Correct |
98 ms |
12240 KB |
Output is correct |
71 |
Correct |
2526 ms |
169400 KB |
Output is correct |
72 |
Correct |
117 ms |
12644 KB |
Output is correct |
73 |
Correct |
645 ms |
49580 KB |
Output is correct |
74 |
Correct |
264 ms |
19852 KB |
Output is correct |
75 |
Correct |
2253 ms |
167584 KB |
Output is correct |
76 |
Correct |
53 ms |
10392 KB |
Output is correct |
77 |
Correct |
1479 ms |
120280 KB |
Output is correct |
78 |
Correct |
8 ms |
6492 KB |
Output is correct |
79 |
Correct |
2403 ms |
178956 KB |
Output is correct |
80 |
Correct |
197 ms |
18848 KB |
Output is correct |
81 |
Correct |
176 ms |
16728 KB |
Output is correct |
82 |
Correct |
85 ms |
11780 KB |
Output is correct |
83 |
Correct |
34 ms |
9052 KB |
Output is correct |
84 |
Correct |
1512 ms |
133596 KB |
Output is correct |
85 |
Correct |
133 ms |
14328 KB |
Output is correct |
86 |
Correct |
69 ms |
11136 KB |
Output is correct |
87 |
Correct |
103 ms |
12952 KB |
Output is correct |
88 |
Correct |
20 ms |
7764 KB |
Output is correct |
89 |
Correct |
217 ms |
19132 KB |
Output is correct |
90 |
Correct |
454 ms |
36796 KB |
Output is correct |
91 |
Correct |
154 ms |
14520 KB |
Output is correct |
92 |
Correct |
2743 ms |
191596 KB |
Output is correct |
93 |
Correct |
330 ms |
21300 KB |
Output is correct |
94 |
Correct |
1858 ms |
113092 KB |
Output is correct |
95 |
Correct |
10 ms |
6408 KB |
Output is correct |
96 |
Correct |
12 ms |
6596 KB |
Output is correct |
97 |
Correct |
482 ms |
27448 KB |
Output is correct |
98 |
Correct |
211 ms |
14364 KB |
Output is correct |
99 |
Correct |
162 ms |
14764 KB |
Output is correct |
100 |
Correct |
3367 ms |
206560 KB |
Output is correct |
101 |
Correct |
1270 ms |
77344 KB |
Output is correct |
102 |
Correct |
1197 ms |
77040 KB |
Output is correct |
103 |
Correct |
1952 ms |
141600 KB |
Output is correct |
104 |
Correct |
2567 ms |
162656 KB |
Output is correct |
105 |
Correct |
116 ms |
11276 KB |
Output is correct |
106 |
Correct |
2119 ms |
142364 KB |
Output is correct |
107 |
Correct |
692 ms |
41096 KB |
Output is correct |
108 |
Correct |
278 ms |
19336 KB |
Output is correct |
109 |
Correct |
26 ms |
8168 KB |
Output is correct |
110 |
Correct |
76 ms |
10580 KB |
Output is correct |
111 |
Correct |
339 ms |
19936 KB |
Output is correct |
112 |
Correct |
3221 ms |
206944 KB |
Output is correct |
113 |
Correct |
3093 ms |
211000 KB |
Output is correct |
114 |
Correct |
233 ms |
19124 KB |
Output is correct |
115 |
Correct |
971 ms |
73096 KB |
Output is correct |
116 |
Correct |
2660 ms |
188696 KB |
Output is correct |
117 |
Correct |
1797 ms |
138632 KB |
Output is correct |
118 |
Correct |
1691 ms |
124084 KB |
Output is correct |
119 |
Correct |
2575 ms |
168652 KB |
Output is correct |
120 |
Correct |
1560 ms |
100076 KB |
Output is correct |