#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (5005)
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
ll N,E,K;
cin>>N>>E>>K;
vector<pair<ll,ll> > v[N];
ll votingcities[K];
for(ll i = 0;i < K;i++){
cin>>votingcities[i];
}
for(ll i = 0;i < E;i++){
ll a,b,c;
cin>>a>>b>>c;
v[b].push_back({a,c});
//reverse the edges as we start from votingcities
}
priority_queue<pair<ll,pair<ll,ll> >,vector<pair<ll,pair<ll,ll> > >,greater<pair<ll,pair<ll,ll> > > > pq;
ll dist[N][32]; //(1<<5) = 32 (11111 is 31 so 31 is max index)
memset(dist,-1,sizeof(dist));
for(ll k = 0;k < K;k++){
ll x = votingcities[k];
for(ll j = 0;j < 32;j++){
dist[x][j] = 0;
pq.push({dist[x][j],{x,j}});
}
}
while(!pq.empty()){
ll d = pq.top().first;
ll x = pq.top().second.first;
ll used = pq.top().second.second; //tickets used so far
pq.pop();
if(d != dist[x][used]){
continue;
}
vector<ll> left;
for(ll i = 0;i < 5;i++){
if(!((1<<i) & used)){
left.push_back(i);
}
}
for(auto u : v[x]){
if(dist[u.first][used] == -1 || dist[u.first][used] > dist[x][used] + u.second){
dist[u.first][used] = dist[x][used] + u.second;
pq.push({dist[u.first][used],{u.first,used}});
}
for(auto ticket : left){
ll bitadd = 1<<ticket;
if(dist[u.first][used + bitadd] == -1 ||
dist[u.first][used + bitadd] > dist[x][used] +
(u.second - (((ticket + 1) * u.second) / 10))){
dist[u.first][used + bitadd] = dist[x][used] + (u.second - (((ticket + 1) * u.second) / 10));
pq.push({dist[u.first][used + bitadd],{u.first,used + bitadd}});
}
}
}
}
ll Q;
cin>>Q;
for(ll q = 0;q < Q;q++){
ll S;
ll P[5];
cin>>S>>P[0]>>P[1]>>P[2]>>P[3]>>P[4];
ll minimum = 1e18;
for(ll bitmask = 0;bitmask < (1<<5);bitmask++){
//when bitmask = 0, it is 00000 (no tickets used)
bool can = 1;
for(ll i = 0;i < 5;i++){
if(((1<<i) & bitmask) && P[i] == -1){
can = 0;
}
}
if(!can) continue;
ll addcost = 0;
for(ll i = 0;i < 5;i++){
if((1<<i) & bitmask){
addcost += P[i];
}
}
if(dist[S][bitmask] == -1) continue;
minimum = min(minimum,dist[S][bitmask] + addcost);
}
if(minimum >= 1e18) cout<<-1<<'\n';
else cout<<minimum<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
3664 KB |
Output is correct |
2 |
Correct |
30 ms |
1856 KB |
Output is correct |
3 |
Correct |
59 ms |
3644 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
3664 KB |
Output is correct |
2 |
Correct |
30 ms |
1856 KB |
Output is correct |
3 |
Correct |
59 ms |
3644 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
68 ms |
3600 KB |
Output is correct |
7 |
Correct |
32 ms |
1824 KB |
Output is correct |
8 |
Correct |
67 ms |
3596 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
3664 KB |
Output is correct |
2 |
Correct |
30 ms |
1856 KB |
Output is correct |
3 |
Correct |
59 ms |
3644 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
68 ms |
3600 KB |
Output is correct |
7 |
Correct |
32 ms |
1824 KB |
Output is correct |
8 |
Correct |
67 ms |
3596 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
54 ms |
5108 KB |
Output is correct |
12 |
Correct |
31 ms |
1832 KB |
Output is correct |
13 |
Correct |
75 ms |
3620 KB |
Output is correct |
14 |
Correct |
0 ms |
284 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
3664 KB |
Output is correct |
2 |
Correct |
30 ms |
1856 KB |
Output is correct |
3 |
Correct |
59 ms |
3644 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
5 ms |
1876 KB |
Output is correct |
7 |
Correct |
30 ms |
1852 KB |
Output is correct |
8 |
Correct |
69 ms |
3640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
3664 KB |
Output is correct |
2 |
Correct |
30 ms |
1856 KB |
Output is correct |
3 |
Correct |
59 ms |
3644 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
68 ms |
3600 KB |
Output is correct |
7 |
Correct |
32 ms |
1824 KB |
Output is correct |
8 |
Correct |
67 ms |
3596 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
5 ms |
1876 KB |
Output is correct |
12 |
Correct |
30 ms |
1852 KB |
Output is correct |
13 |
Correct |
69 ms |
3640 KB |
Output is correct |
14 |
Correct |
57 ms |
3532 KB |
Output is correct |
15 |
Correct |
30 ms |
1856 KB |
Output is correct |
16 |
Correct |
71 ms |
3604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
5128 KB |
Output is correct |
2 |
Correct |
61 ms |
5176 KB |
Output is correct |
3 |
Correct |
47 ms |
2308 KB |
Output is correct |
4 |
Correct |
73 ms |
3532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
620 KB |
Output is correct |
2 |
Correct |
3 ms |
664 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
464 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
3 ms |
660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
3664 KB |
Output is correct |
2 |
Correct |
30 ms |
1856 KB |
Output is correct |
3 |
Correct |
59 ms |
3644 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
68 ms |
3600 KB |
Output is correct |
7 |
Correct |
32 ms |
1824 KB |
Output is correct |
8 |
Correct |
67 ms |
3596 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
54 ms |
5108 KB |
Output is correct |
12 |
Correct |
31 ms |
1832 KB |
Output is correct |
13 |
Correct |
75 ms |
3620 KB |
Output is correct |
14 |
Correct |
0 ms |
284 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
1876 KB |
Output is correct |
17 |
Correct |
30 ms |
1852 KB |
Output is correct |
18 |
Correct |
69 ms |
3640 KB |
Output is correct |
19 |
Correct |
57 ms |
3532 KB |
Output is correct |
20 |
Correct |
30 ms |
1856 KB |
Output is correct |
21 |
Correct |
71 ms |
3604 KB |
Output is correct |
22 |
Correct |
70 ms |
5128 KB |
Output is correct |
23 |
Correct |
61 ms |
5176 KB |
Output is correct |
24 |
Correct |
47 ms |
2308 KB |
Output is correct |
25 |
Correct |
73 ms |
3532 KB |
Output is correct |
26 |
Correct |
4 ms |
620 KB |
Output is correct |
27 |
Correct |
3 ms |
664 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
464 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
660 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
77 ms |
5128 KB |
Output is correct |
38 |
Correct |
55 ms |
8192 KB |
Output is correct |
39 |
Correct |
70 ms |
3644 KB |
Output is correct |
40 |
Correct |
1 ms |
1620 KB |
Output is correct |
41 |
Correct |
47 ms |
8028 KB |
Output is correct |
42 |
Correct |
70 ms |
5112 KB |
Output is correct |
43 |
Correct |
58 ms |
8252 KB |
Output is correct |
44 |
Correct |
70 ms |
3608 KB |
Output is correct |
45 |
Correct |
46 ms |
2132 KB |
Output is correct |