#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 100010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#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 |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
4 ms |
5968 KB |
Output is correct |
3 |
Correct |
2 ms |
5880 KB |
Output is correct |
4 |
Correct |
2 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5768 KB |
Output is correct |
6 |
Correct |
3 ms |
5712 KB |
Output is correct |
7 |
Correct |
2 ms |
5712 KB |
Output is correct |
8 |
Correct |
3 ms |
5712 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5768 KB |
Output is correct |
11 |
Correct |
2 ms |
5712 KB |
Output is correct |
12 |
Correct |
3 ms |
5712 KB |
Output is correct |
13 |
Correct |
3 ms |
5712 KB |
Output is correct |
14 |
Correct |
2 ms |
5712 KB |
Output is correct |
15 |
Correct |
3 ms |
5780 KB |
Output is correct |
16 |
Correct |
2 ms |
5712 KB |
Output is correct |
17 |
Correct |
3 ms |
5712 KB |
Output is correct |
18 |
Correct |
2 ms |
5768 KB |
Output is correct |
19 |
Correct |
3 ms |
5884 KB |
Output is correct |
20 |
Correct |
3 ms |
5712 KB |
Output is correct |
21 |
Correct |
2 ms |
5712 KB |
Output is correct |
22 |
Correct |
2 ms |
5712 KB |
Output is correct |
23 |
Correct |
2 ms |
5660 KB |
Output is correct |
24 |
Correct |
2 ms |
5712 KB |
Output is correct |
25 |
Correct |
2 ms |
5712 KB |
Output is correct |
26 |
Correct |
2 ms |
5884 KB |
Output is correct |
27 |
Correct |
3 ms |
5712 KB |
Output is correct |
28 |
Correct |
3 ms |
5712 KB |
Output is correct |
29 |
Correct |
3 ms |
5712 KB |
Output is correct |
30 |
Correct |
3 ms |
5712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
4 ms |
5968 KB |
Output is correct |
3 |
Correct |
2 ms |
5880 KB |
Output is correct |
4 |
Correct |
2 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5768 KB |
Output is correct |
6 |
Correct |
3 ms |
5712 KB |
Output is correct |
7 |
Correct |
2 ms |
5712 KB |
Output is correct |
8 |
Correct |
3 ms |
5712 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5768 KB |
Output is correct |
11 |
Correct |
2 ms |
5712 KB |
Output is correct |
12 |
Correct |
3 ms |
5712 KB |
Output is correct |
13 |
Correct |
3 ms |
5712 KB |
Output is correct |
14 |
Correct |
2 ms |
5712 KB |
Output is correct |
15 |
Correct |
3 ms |
5780 KB |
Output is correct |
16 |
Correct |
2 ms |
5712 KB |
Output is correct |
17 |
Correct |
3 ms |
5712 KB |
Output is correct |
18 |
Correct |
2 ms |
5768 KB |
Output is correct |
19 |
Correct |
3 ms |
5884 KB |
Output is correct |
20 |
Correct |
3 ms |
5712 KB |
Output is correct |
21 |
Correct |
2 ms |
5712 KB |
Output is correct |
22 |
Correct |
2 ms |
5712 KB |
Output is correct |
23 |
Correct |
2 ms |
5660 KB |
Output is correct |
24 |
Correct |
2 ms |
5712 KB |
Output is correct |
25 |
Correct |
2 ms |
5712 KB |
Output is correct |
26 |
Correct |
2 ms |
5884 KB |
Output is correct |
27 |
Correct |
3 ms |
5712 KB |
Output is correct |
28 |
Correct |
3 ms |
5712 KB |
Output is correct |
29 |
Correct |
3 ms |
5712 KB |
Output is correct |
30 |
Correct |
3 ms |
5712 KB |
Output is correct |
31 |
Correct |
3 ms |
5880 KB |
Output is correct |
32 |
Correct |
3 ms |
5800 KB |
Output is correct |
33 |
Correct |
3 ms |
5712 KB |
Output is correct |
34 |
Correct |
2 ms |
5712 KB |
Output is correct |
35 |
Correct |
3 ms |
5712 KB |
Output is correct |
36 |
Correct |
4 ms |
6136 KB |
Output is correct |
37 |
Correct |
5 ms |
6224 KB |
Output is correct |
38 |
Correct |
3 ms |
5712 KB |
Output is correct |
39 |
Correct |
20 ms |
11100 KB |
Output is correct |
40 |
Correct |
7 ms |
6992 KB |
Output is correct |
41 |
Correct |
3 ms |
5712 KB |
Output is correct |
42 |
Correct |
6 ms |
6912 KB |
Output is correct |
43 |
Correct |
3 ms |
5968 KB |
Output is correct |
44 |
Correct |
3 ms |
5712 KB |
Output is correct |
45 |
Correct |
2 ms |
5548 KB |
Output is correct |
46 |
Correct |
22 ms |
11368 KB |
Output is correct |
47 |
Correct |
6 ms |
6648 KB |
Output is correct |
48 |
Correct |
19 ms |
10972 KB |
Output is correct |
49 |
Correct |
20 ms |
11088 KB |
Output is correct |
50 |
Correct |
3 ms |
5712 KB |
Output is correct |
51 |
Correct |
3 ms |
5712 KB |
Output is correct |
52 |
Correct |
2 ms |
5816 KB |
Output is correct |
53 |
Correct |
14 ms |
8528 KB |
Output is correct |
54 |
Correct |
23 ms |
11376 KB |
Output is correct |
55 |
Correct |
3 ms |
5884 KB |
Output is correct |
56 |
Correct |
3 ms |
5712 KB |
Output is correct |
57 |
Correct |
2 ms |
5712 KB |
Output is correct |
58 |
Correct |
24 ms |
11584 KB |
Output is correct |
59 |
Correct |
2 ms |
5712 KB |
Output is correct |
60 |
Correct |
3 ms |
5880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
10404 KB |
Output is correct |
2 |
Correct |
5 ms |
6392 KB |
Output is correct |
3 |
Correct |
1357 ms |
134924 KB |
Output is correct |
4 |
Correct |
667 ms |
71712 KB |
Output is correct |
5 |
Correct |
190 ms |
17928 KB |
Output is correct |
6 |
Correct |
153 ms |
14900 KB |
Output is correct |
7 |
Correct |
203 ms |
17684 KB |
Output is correct |
8 |
Correct |
81 ms |
9832 KB |
Output is correct |
9 |
Correct |
135 ms |
14304 KB |
Output is correct |
10 |
Correct |
97 ms |
10868 KB |
Output is correct |
11 |
Correct |
1223 ms |
138920 KB |
Output is correct |
12 |
Correct |
112 ms |
12392 KB |
Output is correct |
13 |
Correct |
468 ms |
49324 KB |
Output is correct |
14 |
Correct |
217 ms |
19748 KB |
Output is correct |
15 |
Correct |
1418 ms |
136584 KB |
Output is correct |
16 |
Correct |
45 ms |
9040 KB |
Output is correct |
17 |
Correct |
953 ms |
97544 KB |
Output is correct |
18 |
Correct |
5 ms |
6224 KB |
Output is correct |
19 |
Correct |
1414 ms |
159656 KB |
Output is correct |
20 |
Correct |
191 ms |
17004 KB |
Output is correct |
21 |
Correct |
168 ms |
15524 KB |
Output is correct |
22 |
Correct |
90 ms |
11644 KB |
Output is correct |
23 |
Correct |
16 ms |
8528 KB |
Output is correct |
24 |
Correct |
928 ms |
111200 KB |
Output is correct |
25 |
Correct |
141 ms |
14352 KB |
Output is correct |
26 |
Correct |
68 ms |
9800 KB |
Output is correct |
27 |
Correct |
120 ms |
12156 KB |
Output is correct |
28 |
Correct |
11 ms |
7504 KB |
Output is correct |
29 |
Correct |
212 ms |
19124 KB |
Output is correct |
30 |
Correct |
362 ms |
31672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
4 ms |
5968 KB |
Output is correct |
3 |
Correct |
2 ms |
5880 KB |
Output is correct |
4 |
Correct |
2 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5768 KB |
Output is correct |
6 |
Correct |
3 ms |
5712 KB |
Output is correct |
7 |
Correct |
2 ms |
5712 KB |
Output is correct |
8 |
Correct |
3 ms |
5712 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5768 KB |
Output is correct |
11 |
Correct |
2 ms |
5712 KB |
Output is correct |
12 |
Correct |
3 ms |
5712 KB |
Output is correct |
13 |
Correct |
3 ms |
5712 KB |
Output is correct |
14 |
Correct |
2 ms |
5712 KB |
Output is correct |
15 |
Correct |
3 ms |
5780 KB |
Output is correct |
16 |
Correct |
2 ms |
5712 KB |
Output is correct |
17 |
Correct |
3 ms |
5712 KB |
Output is correct |
18 |
Correct |
2 ms |
5768 KB |
Output is correct |
19 |
Correct |
3 ms |
5884 KB |
Output is correct |
20 |
Correct |
3 ms |
5712 KB |
Output is correct |
21 |
Correct |
2 ms |
5712 KB |
Output is correct |
22 |
Correct |
2 ms |
5712 KB |
Output is correct |
23 |
Correct |
2 ms |
5660 KB |
Output is correct |
24 |
Correct |
2 ms |
5712 KB |
Output is correct |
25 |
Correct |
2 ms |
5712 KB |
Output is correct |
26 |
Correct |
2 ms |
5884 KB |
Output is correct |
27 |
Correct |
3 ms |
5712 KB |
Output is correct |
28 |
Correct |
3 ms |
5712 KB |
Output is correct |
29 |
Correct |
3 ms |
5712 KB |
Output is correct |
30 |
Correct |
3 ms |
5712 KB |
Output is correct |
31 |
Correct |
3 ms |
5880 KB |
Output is correct |
32 |
Correct |
3 ms |
5800 KB |
Output is correct |
33 |
Correct |
3 ms |
5712 KB |
Output is correct |
34 |
Correct |
2 ms |
5712 KB |
Output is correct |
35 |
Correct |
3 ms |
5712 KB |
Output is correct |
36 |
Correct |
4 ms |
6136 KB |
Output is correct |
37 |
Correct |
5 ms |
6224 KB |
Output is correct |
38 |
Correct |
3 ms |
5712 KB |
Output is correct |
39 |
Correct |
20 ms |
11100 KB |
Output is correct |
40 |
Correct |
7 ms |
6992 KB |
Output is correct |
41 |
Correct |
3 ms |
5712 KB |
Output is correct |
42 |
Correct |
6 ms |
6912 KB |
Output is correct |
43 |
Correct |
3 ms |
5968 KB |
Output is correct |
44 |
Correct |
3 ms |
5712 KB |
Output is correct |
45 |
Correct |
2 ms |
5548 KB |
Output is correct |
46 |
Correct |
22 ms |
11368 KB |
Output is correct |
47 |
Correct |
6 ms |
6648 KB |
Output is correct |
48 |
Correct |
19 ms |
10972 KB |
Output is correct |
49 |
Correct |
20 ms |
11088 KB |
Output is correct |
50 |
Correct |
3 ms |
5712 KB |
Output is correct |
51 |
Correct |
3 ms |
5712 KB |
Output is correct |
52 |
Correct |
2 ms |
5816 KB |
Output is correct |
53 |
Correct |
14 ms |
8528 KB |
Output is correct |
54 |
Correct |
23 ms |
11376 KB |
Output is correct |
55 |
Correct |
3 ms |
5884 KB |
Output is correct |
56 |
Correct |
3 ms |
5712 KB |
Output is correct |
57 |
Correct |
2 ms |
5712 KB |
Output is correct |
58 |
Correct |
24 ms |
11584 KB |
Output is correct |
59 |
Correct |
2 ms |
5712 KB |
Output is correct |
60 |
Correct |
3 ms |
5880 KB |
Output is correct |
61 |
Correct |
76 ms |
10404 KB |
Output is correct |
62 |
Correct |
5 ms |
6392 KB |
Output is correct |
63 |
Correct |
1357 ms |
134924 KB |
Output is correct |
64 |
Correct |
667 ms |
71712 KB |
Output is correct |
65 |
Correct |
190 ms |
17928 KB |
Output is correct |
66 |
Correct |
153 ms |
14900 KB |
Output is correct |
67 |
Correct |
203 ms |
17684 KB |
Output is correct |
68 |
Correct |
81 ms |
9832 KB |
Output is correct |
69 |
Correct |
135 ms |
14304 KB |
Output is correct |
70 |
Correct |
97 ms |
10868 KB |
Output is correct |
71 |
Correct |
1223 ms |
138920 KB |
Output is correct |
72 |
Correct |
112 ms |
12392 KB |
Output is correct |
73 |
Correct |
468 ms |
49324 KB |
Output is correct |
74 |
Correct |
217 ms |
19748 KB |
Output is correct |
75 |
Correct |
1418 ms |
136584 KB |
Output is correct |
76 |
Correct |
45 ms |
9040 KB |
Output is correct |
77 |
Correct |
953 ms |
97544 KB |
Output is correct |
78 |
Correct |
5 ms |
6224 KB |
Output is correct |
79 |
Correct |
1414 ms |
159656 KB |
Output is correct |
80 |
Correct |
191 ms |
17004 KB |
Output is correct |
81 |
Correct |
168 ms |
15524 KB |
Output is correct |
82 |
Correct |
90 ms |
11644 KB |
Output is correct |
83 |
Correct |
16 ms |
8528 KB |
Output is correct |
84 |
Correct |
928 ms |
111200 KB |
Output is correct |
85 |
Correct |
141 ms |
14352 KB |
Output is correct |
86 |
Correct |
68 ms |
9800 KB |
Output is correct |
87 |
Correct |
120 ms |
12156 KB |
Output is correct |
88 |
Correct |
11 ms |
7504 KB |
Output is correct |
89 |
Correct |
212 ms |
19124 KB |
Output is correct |
90 |
Correct |
362 ms |
31672 KB |
Output is correct |
91 |
Incorrect |
133 ms |
14248 KB |
Output isn't correct |
92 |
Halted |
0 ms |
0 KB |
- |