#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 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:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen(name".INP" , "r" , stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | freopen(name".OUT" , "w" , stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2384 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
3 |
Correct |
11 ms |
2640 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 |
6 ms |
2384 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
3 |
Correct |
11 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
164 ms |
2604 KB |
Output is correct |
7 |
Correct |
72 ms |
2640 KB |
Output is correct |
8 |
Correct |
218 ms |
2640 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 |
6 ms |
2384 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
3 |
Correct |
11 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
164 ms |
2604 KB |
Output is correct |
7 |
Correct |
72 ms |
2640 KB |
Output is correct |
8 |
Correct |
218 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
174 ms |
2612 KB |
Output is correct |
12 |
Correct |
71 ms |
2808 KB |
Output is correct |
13 |
Correct |
212 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 |
6 ms |
2384 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
3 |
Correct |
11 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
8 ms |
2384 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
8 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2384 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
3 |
Correct |
11 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
164 ms |
2604 KB |
Output is correct |
7 |
Correct |
72 ms |
2640 KB |
Output is correct |
8 |
Correct |
218 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
8 ms |
2384 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
8 ms |
2640 KB |
Output is correct |
14 |
Correct |
247 ms |
2396 KB |
Output is correct |
15 |
Correct |
89 ms |
2896 KB |
Output is correct |
16 |
Incorrect |
238 ms |
2888 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
2604 KB |
Output is correct |
2 |
Correct |
191 ms |
2604 KB |
Output is correct |
3 |
Correct |
72 ms |
2808 KB |
Output is correct |
4 |
Correct |
238 ms |
2696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
848 KB |
Output is correct |
2 |
Correct |
21 ms |
848 KB |
Output is correct |
3 |
Correct |
2 ms |
1016 KB |
Output is correct |
4 |
Correct |
13 ms |
876 KB |
Output is correct |
5 |
Correct |
19 ms |
848 KB |
Output is correct |
6 |
Correct |
2 ms |
848 KB |
Output is correct |
7 |
Correct |
40 ms |
848 KB |
Output is correct |
8 |
Correct |
23 ms |
848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2384 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
3 |
Correct |
11 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
164 ms |
2604 KB |
Output is correct |
7 |
Correct |
72 ms |
2640 KB |
Output is correct |
8 |
Correct |
218 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
174 ms |
2612 KB |
Output is correct |
12 |
Correct |
71 ms |
2808 KB |
Output is correct |
13 |
Correct |
212 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 |
8 ms |
2384 KB |
Output is correct |
17 |
Correct |
5 ms |
2808 KB |
Output is correct |
18 |
Correct |
8 ms |
2640 KB |
Output is correct |
19 |
Correct |
247 ms |
2396 KB |
Output is correct |
20 |
Correct |
89 ms |
2896 KB |
Output is correct |
21 |
Incorrect |
238 ms |
2888 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |