#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 300001;
vector <ll> bit(nmax);
vector <vector <pll> > dp(nmax);
map <pii, bool> used;
vector <vector <pll> > g(nmax);
priority_queue <pair<ll, pii> > q;
void upd(int u, int x, ll len){
bool tt;
tt = false;
for(int j = 0; j < 5; j++){
if(dp[u][j].s == x){
tt = true;
if(len > dp[u][j].f) continue;
dp[u][j].f = len;
q.push({-dp[u][j].f, {u, dp[u][j].s}});
}
}
if(!tt){
if(len < dp[u].back().f)
dp[u].pb({len, x}), q.push({-len, {u, x}});
}
sort(dp[u].begin(), dp[u].end());
if(dp[u].size() > 5)
dp[u].pop_back();
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n, k, m; cin >> n >> m >> k;
for(int i = 1; i <= m; i++){
int u, v; cin >> u >> v;
ll w; cin >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
vector <int> sources;
for(int i = 1 ;i <= k; i++){
int x; cin >> x;
sources.pb(x);
}
// cout << 1;
for(int i = 1; i <= n; i++){
for(int j = 0; j < 5; j++)
dp[i].pb({1e18, 0});
}
// cout << 1;
for(int to : sources){
dp[to][0] = {0, to};
q.push({0, {to, to}});
}
//cout << 1;
while(!q.empty()){
int v = q.top().s.f, y = q.top().s.s;
ll val = -q.top().f;
q.pop();
if(used[{v, y}]) continue;
used[{v, y}] = true;
for(auto ed : g[v]){
int to = ed.f;
ll len = ed.s + val;
upd(to, y, len);
}
}
vector <pair<ll, pii> > vec;
for(int to : sources){
for(int j = 0; j < 5; j++){
if(dp[to][j].f != 1e18)
vec.pb({dp[to][j].f, {to, dp[to][j].s}});
}
}
sort(vec.begin(), vec.end());
int cnt[n + 1];
fill(cnt, cnt + n + 1, 0);
set <pii> st;
vector <pair<ll, pii> > cand;
for(auto to : vec){
if(cand.size() > 30) break;
if(to.s.f == to.s.s) continue;
if(st.find({to.s.f, to.s.s}) != st.end()) continue;
st.insert({to.s.f, to.s.s});
if(cnt[to.s.f] < 5 && cnt[to.s.s] < 5){
cand.pb(to);
cnt[to.s.f]++;
cnt[to.s.s]++;
}
}
// cout << 1;
ll ans = 1e18;
for(int i = 0; i < cand.size(); i++){
for(int j =0; j < cand.size(); j++){
set <int> st;
st.insert(cand[i].s.f);
st.insert(cand[i].s.s);
st.insert(cand[j].s.f);
st.insert(cand[j].s.s);
if(st.size() == 4){
ans = min(ans, cand[i].f + cand[j].f);
}
}
}
cout << ans;
return 0;
}
Compilation message
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:102:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> >, std::allocator<std::pair<long long int, std::pair<int, int> > > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i = 0; i < cand.size(); i++){
| ~~^~~~~~~~~~~~~
RelayMarathon.cpp:103:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> >, std::allocator<std::pair<long long int, std::pair<int, int> > > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for(int j =0; j < cand.size(); j++){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
10 ms |
16852 KB |
Output is correct |
3 |
Correct |
10 ms |
16724 KB |
Output is correct |
4 |
Correct |
10 ms |
16732 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16740 KB |
Output is correct |
7 |
Correct |
10 ms |
16804 KB |
Output is correct |
8 |
Correct |
12 ms |
16876 KB |
Output is correct |
9 |
Correct |
11 ms |
16724 KB |
Output is correct |
10 |
Correct |
9 ms |
16764 KB |
Output is correct |
11 |
Correct |
9 ms |
16768 KB |
Output is correct |
12 |
Correct |
8 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16852 KB |
Output is correct |
14 |
Correct |
10 ms |
16860 KB |
Output is correct |
15 |
Correct |
11 ms |
16836 KB |
Output is correct |
16 |
Correct |
8 ms |
16724 KB |
Output is correct |
17 |
Correct |
12 ms |
16852 KB |
Output is correct |
18 |
Correct |
8 ms |
16784 KB |
Output is correct |
19 |
Correct |
10 ms |
16852 KB |
Output is correct |
20 |
Correct |
10 ms |
16812 KB |
Output is correct |
21 |
Correct |
12 ms |
16852 KB |
Output is correct |
22 |
Correct |
8 ms |
16724 KB |
Output is correct |
23 |
Correct |
11 ms |
16852 KB |
Output is correct |
24 |
Correct |
8 ms |
16724 KB |
Output is correct |
25 |
Correct |
8 ms |
16724 KB |
Output is correct |
26 |
Correct |
8 ms |
16724 KB |
Output is correct |
27 |
Correct |
8 ms |
16672 KB |
Output is correct |
28 |
Correct |
11 ms |
16876 KB |
Output is correct |
29 |
Correct |
9 ms |
16852 KB |
Output is correct |
30 |
Correct |
10 ms |
16852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
10 ms |
16852 KB |
Output is correct |
3 |
Correct |
10 ms |
16724 KB |
Output is correct |
4 |
Correct |
10 ms |
16732 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16740 KB |
Output is correct |
7 |
Correct |
10 ms |
16804 KB |
Output is correct |
8 |
Correct |
12 ms |
16876 KB |
Output is correct |
9 |
Correct |
11 ms |
16724 KB |
Output is correct |
10 |
Correct |
9 ms |
16764 KB |
Output is correct |
11 |
Correct |
9 ms |
16768 KB |
Output is correct |
12 |
Correct |
8 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16852 KB |
Output is correct |
14 |
Correct |
10 ms |
16860 KB |
Output is correct |
15 |
Correct |
11 ms |
16836 KB |
Output is correct |
16 |
Correct |
8 ms |
16724 KB |
Output is correct |
17 |
Correct |
12 ms |
16852 KB |
Output is correct |
18 |
Correct |
8 ms |
16784 KB |
Output is correct |
19 |
Correct |
10 ms |
16852 KB |
Output is correct |
20 |
Correct |
10 ms |
16812 KB |
Output is correct |
21 |
Correct |
12 ms |
16852 KB |
Output is correct |
22 |
Correct |
8 ms |
16724 KB |
Output is correct |
23 |
Correct |
11 ms |
16852 KB |
Output is correct |
24 |
Correct |
8 ms |
16724 KB |
Output is correct |
25 |
Correct |
8 ms |
16724 KB |
Output is correct |
26 |
Correct |
8 ms |
16724 KB |
Output is correct |
27 |
Correct |
8 ms |
16672 KB |
Output is correct |
28 |
Correct |
11 ms |
16876 KB |
Output is correct |
29 |
Correct |
9 ms |
16852 KB |
Output is correct |
30 |
Correct |
10 ms |
16852 KB |
Output is correct |
31 |
Correct |
9 ms |
16888 KB |
Output is correct |
32 |
Correct |
11 ms |
16980 KB |
Output is correct |
33 |
Correct |
10 ms |
16980 KB |
Output is correct |
34 |
Correct |
9 ms |
16852 KB |
Output is correct |
35 |
Correct |
8 ms |
16724 KB |
Output is correct |
36 |
Correct |
16 ms |
17492 KB |
Output is correct |
37 |
Correct |
19 ms |
17720 KB |
Output is correct |
38 |
Correct |
11 ms |
16980 KB |
Output is correct |
39 |
Correct |
189 ms |
21656 KB |
Output is correct |
40 |
Correct |
39 ms |
18444 KB |
Output is correct |
41 |
Correct |
11 ms |
16980 KB |
Output is correct |
42 |
Correct |
42 ms |
18516 KB |
Output is correct |
43 |
Correct |
14 ms |
17364 KB |
Output is correct |
44 |
Correct |
11 ms |
17052 KB |
Output is correct |
45 |
Correct |
9 ms |
16852 KB |
Output is correct |
46 |
Correct |
245 ms |
21848 KB |
Output is correct |
47 |
Correct |
29 ms |
18016 KB |
Output is correct |
48 |
Correct |
186 ms |
21668 KB |
Output is correct |
49 |
Correct |
184 ms |
21832 KB |
Output is correct |
50 |
Correct |
12 ms |
17056 KB |
Output is correct |
51 |
Correct |
11 ms |
17108 KB |
Output is correct |
52 |
Correct |
10 ms |
16980 KB |
Output is correct |
53 |
Correct |
93 ms |
19636 KB |
Output is correct |
54 |
Correct |
190 ms |
21664 KB |
Output is correct |
55 |
Correct |
10 ms |
16980 KB |
Output is correct |
56 |
Correct |
10 ms |
16852 KB |
Output is correct |
57 |
Correct |
10 ms |
16980 KB |
Output is correct |
58 |
Correct |
285 ms |
22008 KB |
Output is correct |
59 |
Correct |
9 ms |
16724 KB |
Output is correct |
60 |
Correct |
13 ms |
17236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
509 ms |
51572 KB |
Output is correct |
2 |
Correct |
31 ms |
31592 KB |
Output is correct |
3 |
Execution timed out |
6045 ms |
189548 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
10 ms |
16852 KB |
Output is correct |
3 |
Correct |
10 ms |
16724 KB |
Output is correct |
4 |
Correct |
10 ms |
16732 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16740 KB |
Output is correct |
7 |
Correct |
10 ms |
16804 KB |
Output is correct |
8 |
Correct |
12 ms |
16876 KB |
Output is correct |
9 |
Correct |
11 ms |
16724 KB |
Output is correct |
10 |
Correct |
9 ms |
16764 KB |
Output is correct |
11 |
Correct |
9 ms |
16768 KB |
Output is correct |
12 |
Correct |
8 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16852 KB |
Output is correct |
14 |
Correct |
10 ms |
16860 KB |
Output is correct |
15 |
Correct |
11 ms |
16836 KB |
Output is correct |
16 |
Correct |
8 ms |
16724 KB |
Output is correct |
17 |
Correct |
12 ms |
16852 KB |
Output is correct |
18 |
Correct |
8 ms |
16784 KB |
Output is correct |
19 |
Correct |
10 ms |
16852 KB |
Output is correct |
20 |
Correct |
10 ms |
16812 KB |
Output is correct |
21 |
Correct |
12 ms |
16852 KB |
Output is correct |
22 |
Correct |
8 ms |
16724 KB |
Output is correct |
23 |
Correct |
11 ms |
16852 KB |
Output is correct |
24 |
Correct |
8 ms |
16724 KB |
Output is correct |
25 |
Correct |
8 ms |
16724 KB |
Output is correct |
26 |
Correct |
8 ms |
16724 KB |
Output is correct |
27 |
Correct |
8 ms |
16672 KB |
Output is correct |
28 |
Correct |
11 ms |
16876 KB |
Output is correct |
29 |
Correct |
9 ms |
16852 KB |
Output is correct |
30 |
Correct |
10 ms |
16852 KB |
Output is correct |
31 |
Correct |
9 ms |
16888 KB |
Output is correct |
32 |
Correct |
11 ms |
16980 KB |
Output is correct |
33 |
Correct |
10 ms |
16980 KB |
Output is correct |
34 |
Correct |
9 ms |
16852 KB |
Output is correct |
35 |
Correct |
8 ms |
16724 KB |
Output is correct |
36 |
Correct |
16 ms |
17492 KB |
Output is correct |
37 |
Correct |
19 ms |
17720 KB |
Output is correct |
38 |
Correct |
11 ms |
16980 KB |
Output is correct |
39 |
Correct |
189 ms |
21656 KB |
Output is correct |
40 |
Correct |
39 ms |
18444 KB |
Output is correct |
41 |
Correct |
11 ms |
16980 KB |
Output is correct |
42 |
Correct |
42 ms |
18516 KB |
Output is correct |
43 |
Correct |
14 ms |
17364 KB |
Output is correct |
44 |
Correct |
11 ms |
17052 KB |
Output is correct |
45 |
Correct |
9 ms |
16852 KB |
Output is correct |
46 |
Correct |
245 ms |
21848 KB |
Output is correct |
47 |
Correct |
29 ms |
18016 KB |
Output is correct |
48 |
Correct |
186 ms |
21668 KB |
Output is correct |
49 |
Correct |
184 ms |
21832 KB |
Output is correct |
50 |
Correct |
12 ms |
17056 KB |
Output is correct |
51 |
Correct |
11 ms |
17108 KB |
Output is correct |
52 |
Correct |
10 ms |
16980 KB |
Output is correct |
53 |
Correct |
93 ms |
19636 KB |
Output is correct |
54 |
Correct |
190 ms |
21664 KB |
Output is correct |
55 |
Correct |
10 ms |
16980 KB |
Output is correct |
56 |
Correct |
10 ms |
16852 KB |
Output is correct |
57 |
Correct |
10 ms |
16980 KB |
Output is correct |
58 |
Correct |
285 ms |
22008 KB |
Output is correct |
59 |
Correct |
9 ms |
16724 KB |
Output is correct |
60 |
Correct |
13 ms |
17236 KB |
Output is correct |
61 |
Correct |
509 ms |
51572 KB |
Output is correct |
62 |
Correct |
31 ms |
31592 KB |
Output is correct |
63 |
Execution timed out |
6045 ms |
189548 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |