//Singapore NOI 2022 - Voting Cities
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define mp make_pair
const int INF = 1e18+7;
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, e, k;
cin >> n >> e >> k; int t[k];
vector<vector<pair<int,int>>>edges(n+1);
set<pair<int,pair<int,int>>>d;
for(int i = 0; i < k; i++){
cin >> t[i];
d.insert(mp(0,mp(t[i],0)));
}
for(int i = 0; i < e; i++){
int u, v, w;
cin >> u >> v >> w;
//edges[u].push_back({v,w});
edges[v].push_back({u,w});
}
int dp[n][1<<5]; bool vis[n][1<<5];
memset(dp,-1,sizeof(dp));
memset(vis,0,sizeof(vis));
while(d.size())
{
int dist = (*d.begin()).fi;
int nd = (*d.begin()).se.fi;
int mask = (*d.begin()).se.se;
d.erase(d.begin());
if(vis[nd][mask])
continue;
dp[nd][mask] = dist;
vis[nd][mask] = 1;
for(pair<int,int> i : edges[nd]){
if(!vis[i.fi][mask])
d.insert(mp(dist+i.se,mp(i.fi, mask)));
for(int j = 0; j < 5; j++){
if(!((1<<j) & mask)){
if(!vis[i.fi][mask | (1<<j)])
d.insert(mp(dist+i.se/10ll * (10ll-j-1ll), mp(i.fi, mask | (1<<j))));
}
}
}
}
int q; cin >> q;
while(q--)
{
int p[6], s;
cin >> s;
for(int i = 0; i < 5; i++)
cin >> p[i];
int ans = 1e18;
for(int j = 0; j < 1 << 5; j++){
if(dp[s][j] != -1){
bool valid = 1; int cost = 0;
for(int k = 0; k < 5; k++){
if((1<<k) & j){
if(p[k] == -1)
valid = 0;
else
cost+=p[k];
}
}
if(valid)
ans = min(ans,dp[s][j] + cost);
}
}
if(ans == 1e18)
cout << -1 << endl;
else
cout << ans << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
6856 KB |
Output is correct |
2 |
Correct |
51 ms |
2080 KB |
Output is correct |
3 |
Correct |
140 ms |
7416 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
6856 KB |
Output is correct |
2 |
Correct |
51 ms |
2080 KB |
Output is correct |
3 |
Correct |
140 ms |
7416 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
126 ms |
7348 KB |
Output is correct |
7 |
Correct |
49 ms |
2084 KB |
Output is correct |
8 |
Correct |
138 ms |
7556 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
6856 KB |
Output is correct |
2 |
Correct |
51 ms |
2080 KB |
Output is correct |
3 |
Correct |
140 ms |
7416 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
126 ms |
7348 KB |
Output is correct |
7 |
Correct |
49 ms |
2084 KB |
Output is correct |
8 |
Correct |
138 ms |
7556 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
306 ms |
18420 KB |
Output is correct |
12 |
Correct |
56 ms |
2084 KB |
Output is correct |
13 |
Correct |
152 ms |
8344 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
6856 KB |
Output is correct |
2 |
Correct |
51 ms |
2080 KB |
Output is correct |
3 |
Correct |
140 ms |
7416 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
2260 KB |
Output is correct |
7 |
Correct |
49 ms |
2084 KB |
Output is correct |
8 |
Correct |
145 ms |
7844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
6856 KB |
Output is correct |
2 |
Correct |
51 ms |
2080 KB |
Output is correct |
3 |
Correct |
140 ms |
7416 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
126 ms |
7348 KB |
Output is correct |
7 |
Correct |
49 ms |
2084 KB |
Output is correct |
8 |
Correct |
138 ms |
7556 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
2260 KB |
Output is correct |
12 |
Correct |
49 ms |
2084 KB |
Output is correct |
13 |
Correct |
145 ms |
7844 KB |
Output is correct |
14 |
Correct |
129 ms |
7064 KB |
Output is correct |
15 |
Correct |
51 ms |
2092 KB |
Output is correct |
16 |
Correct |
191 ms |
7960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
174 ms |
11020 KB |
Output is correct |
2 |
Correct |
259 ms |
15840 KB |
Output is correct |
3 |
Correct |
95 ms |
3112 KB |
Output is correct |
4 |
Correct |
152 ms |
8144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
3392 KB |
Output is correct |
2 |
Correct |
15 ms |
3180 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
13 ms |
2900 KB |
Output is correct |
5 |
Correct |
15 ms |
3304 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
16 ms |
3376 KB |
Output is correct |
8 |
Correct |
16 ms |
3284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
6856 KB |
Output is correct |
2 |
Correct |
51 ms |
2080 KB |
Output is correct |
3 |
Correct |
140 ms |
7416 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
126 ms |
7348 KB |
Output is correct |
7 |
Correct |
49 ms |
2084 KB |
Output is correct |
8 |
Correct |
138 ms |
7556 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
306 ms |
18420 KB |
Output is correct |
12 |
Correct |
56 ms |
2084 KB |
Output is correct |
13 |
Correct |
152 ms |
8344 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
2260 KB |
Output is correct |
17 |
Correct |
49 ms |
2084 KB |
Output is correct |
18 |
Correct |
145 ms |
7844 KB |
Output is correct |
19 |
Correct |
129 ms |
7064 KB |
Output is correct |
20 |
Correct |
51 ms |
2092 KB |
Output is correct |
21 |
Correct |
191 ms |
7960 KB |
Output is correct |
22 |
Correct |
174 ms |
11020 KB |
Output is correct |
23 |
Correct |
259 ms |
15840 KB |
Output is correct |
24 |
Correct |
95 ms |
3112 KB |
Output is correct |
25 |
Correct |
152 ms |
8144 KB |
Output is correct |
26 |
Correct |
15 ms |
3392 KB |
Output is correct |
27 |
Correct |
15 ms |
3180 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
13 ms |
2900 KB |
Output is correct |
30 |
Correct |
15 ms |
3304 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
16 ms |
3376 KB |
Output is correct |
33 |
Correct |
16 ms |
3284 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
264 ms |
15992 KB |
Output is correct |
38 |
Correct |
347 ms |
21428 KB |
Output is correct |
39 |
Correct |
138 ms |
8064 KB |
Output is correct |
40 |
Correct |
1 ms |
1748 KB |
Output is correct |
41 |
Correct |
2 ms |
2124 KB |
Output is correct |
42 |
Correct |
281 ms |
17496 KB |
Output is correct |
43 |
Correct |
377 ms |
21780 KB |
Output is correct |
44 |
Correct |
157 ms |
8192 KB |
Output is correct |
45 |
Correct |
87 ms |
2644 KB |
Output is correct |