#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
int edge[71][71];
vector<pair<int,ll>> adj[71];
ll dp[71][71][70*70+1];
ll solve(int i,int j,int k){
if(k == 0) return i == j ? 0 : 1e18;
ll &ret = dp[i][j][k];
if(~ret) return ret;
ret = solve(i,j,k-1);
for(auto [ch,c] : adj[i]) ret = min(ret,solve(ch,j,k-1)+c);
return ret;
}
void solve(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) edge[i][j] = 1e9;
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
edge[a][b] = min(edge[a][b] , c);
}
int cnt = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) {
if(edge[i][j] != 1e9) adj[i].push_back({j,edge[i][j]}) , cnt++;
}
}
int k,q;
cin>>k>>q;
k = min(k,cnt);
memset(dp,-1,sizeof(dp));
while(q--){
int a,b;
cin>>a>>b;
ll ans = solve(a,b,k);
cout<<(ans == 1e18 ? -1 : ans)<<endl;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message
Main.cpp: In function 'll solve(int, int, int)':
Main.cpp:13:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
13 | for(auto [ch,c] : adj[i]) ret = min(ret,solve(ch,j,k-1)+c);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
193660 KB |
Output is correct |
2 |
Correct |
27 ms |
193884 KB |
Output is correct |
3 |
Correct |
23 ms |
193712 KB |
Output is correct |
4 |
Correct |
23 ms |
193632 KB |
Output is correct |
5 |
Correct |
26 ms |
193612 KB |
Output is correct |
6 |
Correct |
22 ms |
193624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
193884 KB |
Output is correct |
2 |
Correct |
25 ms |
193880 KB |
Output is correct |
3 |
Correct |
24 ms |
193764 KB |
Output is correct |
4 |
Correct |
24 ms |
193880 KB |
Output is correct |
5 |
Correct |
27 ms |
193728 KB |
Output is correct |
6 |
Correct |
26 ms |
193884 KB |
Output is correct |
7 |
Correct |
27 ms |
193876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
193660 KB |
Output is correct |
2 |
Correct |
27 ms |
193884 KB |
Output is correct |
3 |
Correct |
23 ms |
193712 KB |
Output is correct |
4 |
Correct |
23 ms |
193632 KB |
Output is correct |
5 |
Correct |
26 ms |
193612 KB |
Output is correct |
6 |
Correct |
22 ms |
193624 KB |
Output is correct |
7 |
Correct |
29 ms |
193812 KB |
Output is correct |
8 |
Correct |
34 ms |
193876 KB |
Output is correct |
9 |
Correct |
25 ms |
193880 KB |
Output is correct |
10 |
Correct |
36 ms |
193884 KB |
Output is correct |
11 |
Correct |
33 ms |
193952 KB |
Output is correct |
12 |
Correct |
69 ms |
194128 KB |
Output is correct |
13 |
Correct |
96 ms |
193876 KB |
Output is correct |
14 |
Correct |
98 ms |
193872 KB |
Output is correct |
15 |
Correct |
175 ms |
193756 KB |
Output is correct |
16 |
Correct |
122 ms |
193992 KB |
Output is correct |
17 |
Correct |
113 ms |
193992 KB |
Output is correct |
18 |
Correct |
93 ms |
193876 KB |
Output is correct |
19 |
Correct |
184 ms |
193872 KB |
Output is correct |
20 |
Correct |
142 ms |
193876 KB |
Output is correct |
21 |
Correct |
95 ms |
194008 KB |
Output is correct |
22 |
Correct |
109 ms |
193876 KB |
Output is correct |
23 |
Correct |
110 ms |
194012 KB |
Output is correct |
24 |
Correct |
142 ms |
193916 KB |
Output is correct |
25 |
Correct |
96 ms |
193996 KB |
Output is correct |
26 |
Correct |
115 ms |
194008 KB |
Output is correct |
27 |
Correct |
130 ms |
193988 KB |
Output is correct |
28 |
Correct |
117 ms |
193996 KB |
Output is correct |
29 |
Correct |
101 ms |
194012 KB |
Output is correct |
30 |
Correct |
110 ms |
193872 KB |
Output is correct |
31 |
Correct |
144 ms |
193996 KB |
Output is correct |
32 |
Correct |
185 ms |
194012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
193660 KB |
Output is correct |
2 |
Correct |
27 ms |
193884 KB |
Output is correct |
3 |
Correct |
23 ms |
193712 KB |
Output is correct |
4 |
Correct |
23 ms |
193632 KB |
Output is correct |
5 |
Correct |
26 ms |
193612 KB |
Output is correct |
6 |
Correct |
22 ms |
193624 KB |
Output is correct |
7 |
Correct |
24 ms |
193884 KB |
Output is correct |
8 |
Correct |
25 ms |
193880 KB |
Output is correct |
9 |
Correct |
24 ms |
193764 KB |
Output is correct |
10 |
Correct |
24 ms |
193880 KB |
Output is correct |
11 |
Correct |
27 ms |
193728 KB |
Output is correct |
12 |
Correct |
26 ms |
193884 KB |
Output is correct |
13 |
Correct |
27 ms |
193876 KB |
Output is correct |
14 |
Correct |
29 ms |
193812 KB |
Output is correct |
15 |
Correct |
34 ms |
193876 KB |
Output is correct |
16 |
Correct |
25 ms |
193880 KB |
Output is correct |
17 |
Correct |
36 ms |
193884 KB |
Output is correct |
18 |
Correct |
33 ms |
193952 KB |
Output is correct |
19 |
Correct |
69 ms |
194128 KB |
Output is correct |
20 |
Correct |
96 ms |
193876 KB |
Output is correct |
21 |
Correct |
98 ms |
193872 KB |
Output is correct |
22 |
Correct |
175 ms |
193756 KB |
Output is correct |
23 |
Correct |
122 ms |
193992 KB |
Output is correct |
24 |
Correct |
113 ms |
193992 KB |
Output is correct |
25 |
Correct |
93 ms |
193876 KB |
Output is correct |
26 |
Correct |
184 ms |
193872 KB |
Output is correct |
27 |
Correct |
142 ms |
193876 KB |
Output is correct |
28 |
Correct |
95 ms |
194008 KB |
Output is correct |
29 |
Correct |
109 ms |
193876 KB |
Output is correct |
30 |
Correct |
110 ms |
194012 KB |
Output is correct |
31 |
Correct |
142 ms |
193916 KB |
Output is correct |
32 |
Correct |
96 ms |
193996 KB |
Output is correct |
33 |
Correct |
115 ms |
194008 KB |
Output is correct |
34 |
Correct |
130 ms |
193988 KB |
Output is correct |
35 |
Correct |
117 ms |
193996 KB |
Output is correct |
36 |
Correct |
101 ms |
194012 KB |
Output is correct |
37 |
Correct |
110 ms |
193872 KB |
Output is correct |
38 |
Correct |
144 ms |
193996 KB |
Output is correct |
39 |
Correct |
185 ms |
194012 KB |
Output is correct |
40 |
Correct |
27 ms |
193852 KB |
Output is correct |
41 |
Correct |
98 ms |
193884 KB |
Output is correct |
42 |
Correct |
26 ms |
193884 KB |
Output is correct |
43 |
Correct |
393 ms |
194180 KB |
Output is correct |
44 |
Correct |
33 ms |
193880 KB |
Output is correct |
45 |
Execution timed out |
1088 ms |
194176 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |