#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(5e3)+7;
const ll inf = ll(1e18)+7;
int n , m , k , q , en[maxN] , c[5];
ll dp[maxN][1<<5] , d[maxN];
vector<ii> g[maxN] , rev_g[maxN];
vector<int> nxt[maxN] , topo;
bool vis[maxN];
void dfs(int u){
vis[u] = 1;
for (int v : nxt[u]){
if (vis[v] == 0){
dfs(v);
}
}
topo.push_back(u);
}
void reset(){
for (int u = 0 ; u < n ; u++){
d[u] = inf;
nxt[u].clear();
vis[u] = 0;
for (int mask = 0 ; mask < (1<<5) ; mask++) dp[u][mask] = inf;
}
topo.clear();
}
void solve(){
cin >> n >> m >> k;
for (int i = 0 ; i < k ; i++){
cin >> en[i];
}
for (int i = 0 ; i < m ; i++){
int u , v , w;
cin >> u >> v >> w;
g[u].push_back({v , w});
rev_g[v].push_back({u , w});
}
cin >> q;
while (q--){
int st; cin >> st;
for (int i = 0 ; i < 5 ; i++) cin >> c[i];
reset();
d[st] = 0;
priority_queue<lli , vector<lli> , greater<lli>> pq;
pq.push({d[st] , st});
while (pq.empty() == 0){
auto it = pq.top(); pq.pop();
int u = it.se;
if (d[u] != it.fi) continue;
for (auto e : g[u]){
int v = e.fi;
int w = e.se;
if (d[v] > d[u] + 1ll * w){
d[v] = d[u] + 1ll * w;
pq.push({d[v] , v});
}
}
}
for (int u = 0 ; u < n ; u++){
for (auto e : g[u]){
int v = e.fi;
int w = e.se;
if (d[u] + 1ll * w == d[v]){
nxt[u].push_back(v);
}
}
}
dfs(st);
reverse(all(topo));
dp[st][0] = 0;
for (int t = 1 ; t <= 3 ; t++){
for (int v : topo){
for (auto e : rev_g[v]){
int u = e.fi;
int w = e.se;
for (int mask = 0 ; mask < (1<<5) ; mask++){
dp[v][mask] = min(dp[v][mask] , dp[u][mask] + 1ll * w);
for (int i = 0 ; i < 5 ; i++){
if ((mask>>i)&1) continue;
if (c[i] == -1) continue;
dp[v][mask ^ (1<<i)] = min(dp[v][mask ^ (1<<i)] , dp[u][mask] + 1ll * c[i] + (1ll * (10 - i - 1) * w) / 10);
}
}
}
}
}
ll ans = inf;
for (int i = 0 ; i < k ; i++){
for (int mask = 0 ; mask < (1<<5) ; mask++){
ans = min(ans , dp[en[i]][mask]);
}
}
if (ans == inf) ans = -1;
cout << ans << "\n";
}
}
#define name "A"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen(name".INP" , "r" , stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | freopen(name".OUT" , "w" , stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2384 KB |
Output is correct |
2 |
Correct |
4 ms |
2640 KB |
Output is correct |
3 |
Correct |
10 ms |
2608 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2384 KB |
Output is correct |
2 |
Correct |
4 ms |
2640 KB |
Output is correct |
3 |
Correct |
10 ms |
2608 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
401 ms |
2384 KB |
Output is correct |
7 |
Correct |
153 ms |
2640 KB |
Output is correct |
8 |
Correct |
469 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2384 KB |
Output is correct |
2 |
Correct |
4 ms |
2640 KB |
Output is correct |
3 |
Correct |
10 ms |
2608 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
401 ms |
2384 KB |
Output is correct |
7 |
Correct |
153 ms |
2640 KB |
Output is correct |
8 |
Correct |
469 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
400 ms |
2384 KB |
Output is correct |
12 |
Correct |
170 ms |
2640 KB |
Output is correct |
13 |
Correct |
467 ms |
2640 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2384 KB |
Output is correct |
2 |
Correct |
4 ms |
2640 KB |
Output is correct |
3 |
Correct |
10 ms |
2608 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
11 ms |
2552 KB |
Output is correct |
7 |
Correct |
5 ms |
2640 KB |
Output is correct |
8 |
Correct |
10 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2384 KB |
Output is correct |
2 |
Correct |
4 ms |
2640 KB |
Output is correct |
3 |
Correct |
10 ms |
2608 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
401 ms |
2384 KB |
Output is correct |
7 |
Correct |
153 ms |
2640 KB |
Output is correct |
8 |
Correct |
469 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
11 ms |
2552 KB |
Output is correct |
12 |
Correct |
5 ms |
2640 KB |
Output is correct |
13 |
Correct |
10 ms |
2640 KB |
Output is correct |
14 |
Correct |
570 ms |
2384 KB |
Output is correct |
15 |
Correct |
202 ms |
2828 KB |
Output is correct |
16 |
Correct |
535 ms |
2656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
421 ms |
2384 KB |
Output is correct |
2 |
Correct |
381 ms |
2612 KB |
Output is correct |
3 |
Correct |
186 ms |
2820 KB |
Output is correct |
4 |
Correct |
518 ms |
2664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
848 KB |
Output is correct |
2 |
Correct |
61 ms |
848 KB |
Output is correct |
3 |
Correct |
4 ms |
1020 KB |
Output is correct |
4 |
Correct |
35 ms |
848 KB |
Output is correct |
5 |
Correct |
57 ms |
848 KB |
Output is correct |
6 |
Correct |
4 ms |
848 KB |
Output is correct |
7 |
Correct |
61 ms |
848 KB |
Output is correct |
8 |
Correct |
63 ms |
848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2384 KB |
Output is correct |
2 |
Correct |
4 ms |
2640 KB |
Output is correct |
3 |
Correct |
10 ms |
2608 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
401 ms |
2384 KB |
Output is correct |
7 |
Correct |
153 ms |
2640 KB |
Output is correct |
8 |
Correct |
469 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
400 ms |
2384 KB |
Output is correct |
12 |
Correct |
170 ms |
2640 KB |
Output is correct |
13 |
Correct |
467 ms |
2640 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
11 ms |
2552 KB |
Output is correct |
17 |
Correct |
5 ms |
2640 KB |
Output is correct |
18 |
Correct |
10 ms |
2640 KB |
Output is correct |
19 |
Correct |
570 ms |
2384 KB |
Output is correct |
20 |
Correct |
202 ms |
2828 KB |
Output is correct |
21 |
Correct |
535 ms |
2656 KB |
Output is correct |
22 |
Correct |
421 ms |
2384 KB |
Output is correct |
23 |
Correct |
381 ms |
2612 KB |
Output is correct |
24 |
Correct |
186 ms |
2820 KB |
Output is correct |
25 |
Correct |
518 ms |
2664 KB |
Output is correct |
26 |
Correct |
69 ms |
848 KB |
Output is correct |
27 |
Correct |
61 ms |
848 KB |
Output is correct |
28 |
Correct |
4 ms |
1020 KB |
Output is correct |
29 |
Correct |
35 ms |
848 KB |
Output is correct |
30 |
Correct |
57 ms |
848 KB |
Output is correct |
31 |
Correct |
4 ms |
848 KB |
Output is correct |
32 |
Correct |
61 ms |
848 KB |
Output is correct |
33 |
Correct |
63 ms |
848 KB |
Output is correct |
34 |
Correct |
1 ms |
592 KB |
Output is correct |
35 |
Correct |
1 ms |
592 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
37 |
Correct |
584 ms |
2804 KB |
Output is correct |
38 |
Correct |
495 ms |
2640 KB |
Output is correct |
39 |
Correct |
583 ms |
2640 KB |
Output is correct |
40 |
Correct |
9 ms |
2128 KB |
Output is correct |
41 |
Correct |
27 ms |
2164 KB |
Output is correct |
42 |
Correct |
595 ms |
2904 KB |
Output is correct |
43 |
Correct |
632 ms |
2896 KB |
Output is correct |
44 |
Correct |
528 ms |
2844 KB |
Output is correct |
45 |
Correct |
195 ms |
2896 KB |
Output is correct |