#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e10
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if(x < 0) x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if(x < 0) x += mod;
}
int n,m,k;
vii ke[maxn];
vi special;
int d[maxn];
int sources[maxn];
pii dist[maxn];
int mindist;
pii node;
void dijkstra(vi inp)
{
mindist = inf;
fo(i,1,n) d[i] = inf;
priority_queue<pii,vii,greater<pii>> q;
for(int it : inp)
{
d[it] = 0;
sources[it] = it;
q.push({0,it});
}
while(ss(q))
{
auto [du,u] = q.top();
q.pop();
if(du != d[u]) continue;
for(auto [v,w] : ke[u])
{
if(minimize(d[v],du + w))
{
q.push({d[v],v});
sources[v] = sources[u];
}
}
}
fo(u,1,n) for(auto [v,w] : ke[u]) if(sources[u] != sources[v])
{
if(minimize(mindist,d[u] + w + d[v])) node = {sources[u],sources[v]};
}
}
pii min1,min2;
void dij(int inp)
{
min1.fi = inf;
min2.fi = inf;
fo(i,1,n) d[i] = inf;
d[inp] = 0;
priority_queue<pii,vii,greater<pii>> q;
q.push({0,inp});
while(q.size())
{
auto [du,u] = q.top();
q.pop();
if(d[u] != du) continue;
for(auto [v,w] : ke[u])
{
if(d[v] > d[u] + w)
{
d[v] = d[u] + w;
q.push({d[v],v});
}
}
}
for(int it : special) if(it != inp)
{
if(min1.fi > d[it])
{
min2 = min1;
min1 = {d[it],it};
}
}
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> k;
special.resize(k);
fo(i,1,m)
{
int u,v,w;
cin >> u >> v >> w;
ke[u].pb(v,w);
ke[v].pb(u,w);
}
int res = inf;
for(int &it : special) cin >> it;
dijkstra(special);
int d1 = mindist;
vi cc;
for(int it : special) if(it != node.fi and it != node.se) cc.pb(it);
dijkstra(cc);
minimize(res,mindist + d1);
dij(node.fi);
pii x = min1;
pii y = min2;
dij(node.se);
if(x.se == node.se)
{
int cc = inf;
for(int it : special) if(it != node.fi and it != node.se and it != y.se) minimize(cc,d[it]);
minimize(res,y.fi + cc);
}
else
{
int cc = inf;
for(int it : special) if(it != node.fi and it != node.se and it != x.se) minimize(cc,d[it]);
minimize(res,x.fi + cc);
}
dij(node.se);
x = min1;
y = min2;
dij(node.fi);
if(x.se == node.fi)
{
int cc = inf;
for(int it : special) if(it != node.fi and it != node.se and it != y.se) minimize(cc,d[it]);
minimize(res,y.fi + cc);
}
else
{
int cc = inf;
for(int it : special) if(it != node.fi and it != node.se and it != x.se) minimize(cc,d[it]);
minimize(res,x.fi + cc);
}
cout << res;
}
Compilation message
RelayMarathon.cpp:128:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
128 | main()
| ^~~~
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
24144 KB |
Output is correct |
2 |
Correct |
11 ms |
24144 KB |
Output is correct |
3 |
Correct |
11 ms |
24144 KB |
Output is correct |
4 |
Correct |
11 ms |
24160 KB |
Output is correct |
5 |
Correct |
11 ms |
24144 KB |
Output is correct |
6 |
Correct |
11 ms |
24316 KB |
Output is correct |
7 |
Correct |
8 ms |
24400 KB |
Output is correct |
8 |
Correct |
6 ms |
24144 KB |
Output is correct |
9 |
Correct |
11 ms |
24144 KB |
Output is correct |
10 |
Correct |
6 ms |
24144 KB |
Output is correct |
11 |
Correct |
6 ms |
24144 KB |
Output is correct |
12 |
Correct |
11 ms |
24264 KB |
Output is correct |
13 |
Correct |
10 ms |
24144 KB |
Output is correct |
14 |
Correct |
8 ms |
24244 KB |
Output is correct |
15 |
Correct |
8 ms |
24144 KB |
Output is correct |
16 |
Correct |
8 ms |
24144 KB |
Output is correct |
17 |
Correct |
13 ms |
24316 KB |
Output is correct |
18 |
Correct |
13 ms |
24144 KB |
Output is correct |
19 |
Correct |
16 ms |
24156 KB |
Output is correct |
20 |
Correct |
15 ms |
24144 KB |
Output is correct |
21 |
Correct |
15 ms |
24144 KB |
Output is correct |
22 |
Correct |
10 ms |
24312 KB |
Output is correct |
23 |
Correct |
9 ms |
24144 KB |
Output is correct |
24 |
Correct |
9 ms |
24300 KB |
Output is correct |
25 |
Correct |
11 ms |
24144 KB |
Output is correct |
26 |
Correct |
11 ms |
24144 KB |
Output is correct |
27 |
Correct |
13 ms |
24144 KB |
Output is correct |
28 |
Correct |
13 ms |
24320 KB |
Output is correct |
29 |
Correct |
16 ms |
24144 KB |
Output is correct |
30 |
Correct |
14 ms |
24144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
24144 KB |
Output is correct |
2 |
Correct |
11 ms |
24144 KB |
Output is correct |
3 |
Correct |
11 ms |
24144 KB |
Output is correct |
4 |
Correct |
11 ms |
24160 KB |
Output is correct |
5 |
Correct |
11 ms |
24144 KB |
Output is correct |
6 |
Correct |
11 ms |
24316 KB |
Output is correct |
7 |
Correct |
8 ms |
24400 KB |
Output is correct |
8 |
Correct |
6 ms |
24144 KB |
Output is correct |
9 |
Correct |
11 ms |
24144 KB |
Output is correct |
10 |
Correct |
6 ms |
24144 KB |
Output is correct |
11 |
Correct |
6 ms |
24144 KB |
Output is correct |
12 |
Correct |
11 ms |
24264 KB |
Output is correct |
13 |
Correct |
10 ms |
24144 KB |
Output is correct |
14 |
Correct |
8 ms |
24244 KB |
Output is correct |
15 |
Correct |
8 ms |
24144 KB |
Output is correct |
16 |
Correct |
8 ms |
24144 KB |
Output is correct |
17 |
Correct |
13 ms |
24316 KB |
Output is correct |
18 |
Correct |
13 ms |
24144 KB |
Output is correct |
19 |
Correct |
16 ms |
24156 KB |
Output is correct |
20 |
Correct |
15 ms |
24144 KB |
Output is correct |
21 |
Correct |
15 ms |
24144 KB |
Output is correct |
22 |
Correct |
10 ms |
24312 KB |
Output is correct |
23 |
Correct |
9 ms |
24144 KB |
Output is correct |
24 |
Correct |
9 ms |
24300 KB |
Output is correct |
25 |
Correct |
11 ms |
24144 KB |
Output is correct |
26 |
Correct |
11 ms |
24144 KB |
Output is correct |
27 |
Correct |
13 ms |
24144 KB |
Output is correct |
28 |
Correct |
13 ms |
24320 KB |
Output is correct |
29 |
Correct |
16 ms |
24144 KB |
Output is correct |
30 |
Correct |
14 ms |
24144 KB |
Output is correct |
31 |
Correct |
12 ms |
24144 KB |
Output is correct |
32 |
Correct |
15 ms |
24312 KB |
Output is correct |
33 |
Correct |
15 ms |
24164 KB |
Output is correct |
34 |
Correct |
15 ms |
24144 KB |
Output is correct |
35 |
Correct |
15 ms |
24268 KB |
Output is correct |
36 |
Correct |
16 ms |
24400 KB |
Output is correct |
37 |
Correct |
15 ms |
24656 KB |
Output is correct |
38 |
Correct |
13 ms |
24400 KB |
Output is correct |
39 |
Correct |
30 ms |
28732 KB |
Output is correct |
40 |
Correct |
20 ms |
25424 KB |
Output is correct |
41 |
Correct |
14 ms |
24400 KB |
Output is correct |
42 |
Correct |
14 ms |
25424 KB |
Output is correct |
43 |
Correct |
11 ms |
24400 KB |
Output is correct |
44 |
Correct |
14 ms |
24400 KB |
Output is correct |
45 |
Correct |
12 ms |
24316 KB |
Output is correct |
46 |
Correct |
30 ms |
28744 KB |
Output is correct |
47 |
Correct |
17 ms |
24912 KB |
Output is correct |
48 |
Correct |
30 ms |
28744 KB |
Output is correct |
49 |
Correct |
30 ms |
28752 KB |
Output is correct |
50 |
Correct |
12 ms |
24312 KB |
Output is correct |
51 |
Correct |
17 ms |
24400 KB |
Output is correct |
52 |
Correct |
16 ms |
24144 KB |
Output is correct |
53 |
Correct |
24 ms |
26588 KB |
Output is correct |
54 |
Correct |
29 ms |
28752 KB |
Output is correct |
55 |
Correct |
14 ms |
24316 KB |
Output is correct |
56 |
Correct |
12 ms |
24144 KB |
Output is correct |
57 |
Correct |
13 ms |
24144 KB |
Output is correct |
58 |
Correct |
37 ms |
28752 KB |
Output is correct |
59 |
Correct |
15 ms |
24144 KB |
Output is correct |
60 |
Correct |
13 ms |
24200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
29772 KB |
Output is correct |
2 |
Correct |
19 ms |
25680 KB |
Output is correct |
3 |
Correct |
1364 ms |
153136 KB |
Output is correct |
4 |
Correct |
716 ms |
83780 KB |
Output is correct |
5 |
Correct |
214 ms |
36532 KB |
Output is correct |
6 |
Correct |
170 ms |
33772 KB |
Output is correct |
7 |
Correct |
242 ms |
37864 KB |
Output is correct |
8 |
Correct |
82 ms |
29928 KB |
Output is correct |
9 |
Correct |
156 ms |
32908 KB |
Output is correct |
10 |
Correct |
115 ms |
30980 KB |
Output is correct |
11 |
Correct |
1331 ms |
157440 KB |
Output is correct |
12 |
Correct |
121 ms |
31456 KB |
Output is correct |
13 |
Correct |
431 ms |
65108 KB |
Output is correct |
14 |
Correct |
207 ms |
38192 KB |
Output is correct |
15 |
Correct |
1254 ms |
154752 KB |
Output is correct |
16 |
Correct |
59 ms |
29068 KB |
Output is correct |
17 |
Correct |
975 ms |
117728 KB |
Output is correct |
18 |
Correct |
19 ms |
25680 KB |
Output is correct |
19 |
Correct |
1724 ms |
159172 KB |
Output is correct |
20 |
Correct |
329 ms |
36716 KB |
Output is correct |
21 |
Correct |
286 ms |
37140 KB |
Output is correct |
22 |
Correct |
165 ms |
30720 KB |
Output is correct |
23 |
Correct |
39 ms |
27984 KB |
Output is correct |
24 |
Correct |
1254 ms |
145972 KB |
Output is correct |
25 |
Correct |
218 ms |
33216 KB |
Output is correct |
26 |
Correct |
99 ms |
30680 KB |
Output is correct |
27 |
Correct |
146 ms |
31988 KB |
Output is correct |
28 |
Correct |
22 ms |
27216 KB |
Output is correct |
29 |
Correct |
268 ms |
38196 KB |
Output is correct |
30 |
Correct |
503 ms |
51644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
24144 KB |
Output is correct |
2 |
Correct |
11 ms |
24144 KB |
Output is correct |
3 |
Correct |
11 ms |
24144 KB |
Output is correct |
4 |
Correct |
11 ms |
24160 KB |
Output is correct |
5 |
Correct |
11 ms |
24144 KB |
Output is correct |
6 |
Correct |
11 ms |
24316 KB |
Output is correct |
7 |
Correct |
8 ms |
24400 KB |
Output is correct |
8 |
Correct |
6 ms |
24144 KB |
Output is correct |
9 |
Correct |
11 ms |
24144 KB |
Output is correct |
10 |
Correct |
6 ms |
24144 KB |
Output is correct |
11 |
Correct |
6 ms |
24144 KB |
Output is correct |
12 |
Correct |
11 ms |
24264 KB |
Output is correct |
13 |
Correct |
10 ms |
24144 KB |
Output is correct |
14 |
Correct |
8 ms |
24244 KB |
Output is correct |
15 |
Correct |
8 ms |
24144 KB |
Output is correct |
16 |
Correct |
8 ms |
24144 KB |
Output is correct |
17 |
Correct |
13 ms |
24316 KB |
Output is correct |
18 |
Correct |
13 ms |
24144 KB |
Output is correct |
19 |
Correct |
16 ms |
24156 KB |
Output is correct |
20 |
Correct |
15 ms |
24144 KB |
Output is correct |
21 |
Correct |
15 ms |
24144 KB |
Output is correct |
22 |
Correct |
10 ms |
24312 KB |
Output is correct |
23 |
Correct |
9 ms |
24144 KB |
Output is correct |
24 |
Correct |
9 ms |
24300 KB |
Output is correct |
25 |
Correct |
11 ms |
24144 KB |
Output is correct |
26 |
Correct |
11 ms |
24144 KB |
Output is correct |
27 |
Correct |
13 ms |
24144 KB |
Output is correct |
28 |
Correct |
13 ms |
24320 KB |
Output is correct |
29 |
Correct |
16 ms |
24144 KB |
Output is correct |
30 |
Correct |
14 ms |
24144 KB |
Output is correct |
31 |
Correct |
12 ms |
24144 KB |
Output is correct |
32 |
Correct |
15 ms |
24312 KB |
Output is correct |
33 |
Correct |
15 ms |
24164 KB |
Output is correct |
34 |
Correct |
15 ms |
24144 KB |
Output is correct |
35 |
Correct |
15 ms |
24268 KB |
Output is correct |
36 |
Correct |
16 ms |
24400 KB |
Output is correct |
37 |
Correct |
15 ms |
24656 KB |
Output is correct |
38 |
Correct |
13 ms |
24400 KB |
Output is correct |
39 |
Correct |
30 ms |
28732 KB |
Output is correct |
40 |
Correct |
20 ms |
25424 KB |
Output is correct |
41 |
Correct |
14 ms |
24400 KB |
Output is correct |
42 |
Correct |
14 ms |
25424 KB |
Output is correct |
43 |
Correct |
11 ms |
24400 KB |
Output is correct |
44 |
Correct |
14 ms |
24400 KB |
Output is correct |
45 |
Correct |
12 ms |
24316 KB |
Output is correct |
46 |
Correct |
30 ms |
28744 KB |
Output is correct |
47 |
Correct |
17 ms |
24912 KB |
Output is correct |
48 |
Correct |
30 ms |
28744 KB |
Output is correct |
49 |
Correct |
30 ms |
28752 KB |
Output is correct |
50 |
Correct |
12 ms |
24312 KB |
Output is correct |
51 |
Correct |
17 ms |
24400 KB |
Output is correct |
52 |
Correct |
16 ms |
24144 KB |
Output is correct |
53 |
Correct |
24 ms |
26588 KB |
Output is correct |
54 |
Correct |
29 ms |
28752 KB |
Output is correct |
55 |
Correct |
14 ms |
24316 KB |
Output is correct |
56 |
Correct |
12 ms |
24144 KB |
Output is correct |
57 |
Correct |
13 ms |
24144 KB |
Output is correct |
58 |
Correct |
37 ms |
28752 KB |
Output is correct |
59 |
Correct |
15 ms |
24144 KB |
Output is correct |
60 |
Correct |
13 ms |
24200 KB |
Output is correct |
61 |
Correct |
84 ms |
29772 KB |
Output is correct |
62 |
Correct |
19 ms |
25680 KB |
Output is correct |
63 |
Correct |
1364 ms |
153136 KB |
Output is correct |
64 |
Correct |
716 ms |
83780 KB |
Output is correct |
65 |
Correct |
214 ms |
36532 KB |
Output is correct |
66 |
Correct |
170 ms |
33772 KB |
Output is correct |
67 |
Correct |
242 ms |
37864 KB |
Output is correct |
68 |
Correct |
82 ms |
29928 KB |
Output is correct |
69 |
Correct |
156 ms |
32908 KB |
Output is correct |
70 |
Correct |
115 ms |
30980 KB |
Output is correct |
71 |
Correct |
1331 ms |
157440 KB |
Output is correct |
72 |
Correct |
121 ms |
31456 KB |
Output is correct |
73 |
Correct |
431 ms |
65108 KB |
Output is correct |
74 |
Correct |
207 ms |
38192 KB |
Output is correct |
75 |
Correct |
1254 ms |
154752 KB |
Output is correct |
76 |
Correct |
59 ms |
29068 KB |
Output is correct |
77 |
Correct |
975 ms |
117728 KB |
Output is correct |
78 |
Correct |
19 ms |
25680 KB |
Output is correct |
79 |
Correct |
1724 ms |
159172 KB |
Output is correct |
80 |
Correct |
329 ms |
36716 KB |
Output is correct |
81 |
Correct |
286 ms |
37140 KB |
Output is correct |
82 |
Correct |
165 ms |
30720 KB |
Output is correct |
83 |
Correct |
39 ms |
27984 KB |
Output is correct |
84 |
Correct |
1254 ms |
145972 KB |
Output is correct |
85 |
Correct |
218 ms |
33216 KB |
Output is correct |
86 |
Correct |
99 ms |
30680 KB |
Output is correct |
87 |
Correct |
146 ms |
31988 KB |
Output is correct |
88 |
Correct |
22 ms |
27216 KB |
Output is correct |
89 |
Correct |
268 ms |
38196 KB |
Output is correct |
90 |
Correct |
503 ms |
51644 KB |
Output is correct |
91 |
Incorrect |
168 ms |
33644 KB |
Output isn't correct |
92 |
Halted |
0 ms |
0 KB |
- |