#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
int edge[71][71];
vector<pair<int,int>> adj[71];
ll dp[71][71][70*70+1];
int K;
ll solve(int i,int j,int k){
if(i == j) return 0;
if(k == K) return 1e18;
ll &ret = dp[i][j][k];
if(~ret) return ret;
ret = 1e18;
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 q;
cin>>K>>q;
K = min(K,n);
memset(dp,-1,sizeof(dp));
while(q--){
int a,b;
cin>>a>>b;
ll ans = solve(a,b,0);
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:15:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
15 | for(auto [ch,c] : adj[i]) ret = min(ret,solve(ch,j,k+1)+c);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
193768 KB |
Output is correct |
2 |
Correct |
23 ms |
193628 KB |
Output is correct |
3 |
Correct |
23 ms |
193628 KB |
Output is correct |
4 |
Correct |
22 ms |
193624 KB |
Output is correct |
5 |
Correct |
22 ms |
193740 KB |
Output is correct |
6 |
Correct |
22 ms |
193616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
193872 KB |
Output is correct |
2 |
Correct |
24 ms |
193884 KB |
Output is correct |
3 |
Correct |
24 ms |
193780 KB |
Output is correct |
4 |
Correct |
24 ms |
193860 KB |
Output is correct |
5 |
Correct |
25 ms |
193644 KB |
Output is correct |
6 |
Correct |
26 ms |
193884 KB |
Output is correct |
7 |
Correct |
27 ms |
193880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
193768 KB |
Output is correct |
2 |
Correct |
23 ms |
193628 KB |
Output is correct |
3 |
Correct |
23 ms |
193628 KB |
Output is correct |
4 |
Correct |
22 ms |
193624 KB |
Output is correct |
5 |
Correct |
22 ms |
193740 KB |
Output is correct |
6 |
Correct |
22 ms |
193616 KB |
Output is correct |
7 |
Correct |
29 ms |
193884 KB |
Output is correct |
8 |
Correct |
36 ms |
193884 KB |
Output is correct |
9 |
Correct |
25 ms |
193884 KB |
Output is correct |
10 |
Correct |
39 ms |
193620 KB |
Output is correct |
11 |
Correct |
35 ms |
193884 KB |
Output is correct |
12 |
Correct |
71 ms |
193872 KB |
Output is correct |
13 |
Correct |
94 ms |
193784 KB |
Output is correct |
14 |
Correct |
98 ms |
193772 KB |
Output is correct |
15 |
Correct |
192 ms |
193756 KB |
Output is correct |
16 |
Correct |
120 ms |
193752 KB |
Output is correct |
17 |
Correct |
97 ms |
193876 KB |
Output is correct |
18 |
Correct |
94 ms |
193740 KB |
Output is correct |
19 |
Correct |
193 ms |
193872 KB |
Output is correct |
20 |
Correct |
157 ms |
193756 KB |
Output is correct |
21 |
Correct |
94 ms |
193740 KB |
Output is correct |
22 |
Correct |
111 ms |
193744 KB |
Output is correct |
23 |
Correct |
108 ms |
193872 KB |
Output is correct |
24 |
Correct |
145 ms |
193744 KB |
Output is correct |
25 |
Correct |
96 ms |
193876 KB |
Output is correct |
26 |
Correct |
115 ms |
193876 KB |
Output is correct |
27 |
Correct |
125 ms |
193740 KB |
Output is correct |
28 |
Correct |
119 ms |
193872 KB |
Output is correct |
29 |
Correct |
97 ms |
193872 KB |
Output is correct |
30 |
Correct |
116 ms |
194248 KB |
Output is correct |
31 |
Correct |
144 ms |
193876 KB |
Output is correct |
32 |
Correct |
192 ms |
193760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
193768 KB |
Output is correct |
2 |
Correct |
23 ms |
193628 KB |
Output is correct |
3 |
Correct |
23 ms |
193628 KB |
Output is correct |
4 |
Correct |
22 ms |
193624 KB |
Output is correct |
5 |
Correct |
22 ms |
193740 KB |
Output is correct |
6 |
Correct |
22 ms |
193616 KB |
Output is correct |
7 |
Correct |
24 ms |
193872 KB |
Output is correct |
8 |
Correct |
24 ms |
193884 KB |
Output is correct |
9 |
Correct |
24 ms |
193780 KB |
Output is correct |
10 |
Correct |
24 ms |
193860 KB |
Output is correct |
11 |
Correct |
25 ms |
193644 KB |
Output is correct |
12 |
Correct |
26 ms |
193884 KB |
Output is correct |
13 |
Correct |
27 ms |
193880 KB |
Output is correct |
14 |
Correct |
29 ms |
193884 KB |
Output is correct |
15 |
Correct |
36 ms |
193884 KB |
Output is correct |
16 |
Correct |
25 ms |
193884 KB |
Output is correct |
17 |
Correct |
39 ms |
193620 KB |
Output is correct |
18 |
Correct |
35 ms |
193884 KB |
Output is correct |
19 |
Correct |
71 ms |
193872 KB |
Output is correct |
20 |
Correct |
94 ms |
193784 KB |
Output is correct |
21 |
Correct |
98 ms |
193772 KB |
Output is correct |
22 |
Correct |
192 ms |
193756 KB |
Output is correct |
23 |
Correct |
120 ms |
193752 KB |
Output is correct |
24 |
Correct |
97 ms |
193876 KB |
Output is correct |
25 |
Correct |
94 ms |
193740 KB |
Output is correct |
26 |
Correct |
193 ms |
193872 KB |
Output is correct |
27 |
Correct |
157 ms |
193756 KB |
Output is correct |
28 |
Correct |
94 ms |
193740 KB |
Output is correct |
29 |
Correct |
111 ms |
193744 KB |
Output is correct |
30 |
Correct |
108 ms |
193872 KB |
Output is correct |
31 |
Correct |
145 ms |
193744 KB |
Output is correct |
32 |
Correct |
96 ms |
193876 KB |
Output is correct |
33 |
Correct |
115 ms |
193876 KB |
Output is correct |
34 |
Correct |
125 ms |
193740 KB |
Output is correct |
35 |
Correct |
119 ms |
193872 KB |
Output is correct |
36 |
Correct |
97 ms |
193872 KB |
Output is correct |
37 |
Correct |
116 ms |
194248 KB |
Output is correct |
38 |
Correct |
144 ms |
193876 KB |
Output is correct |
39 |
Correct |
192 ms |
193760 KB |
Output is correct |
40 |
Correct |
27 ms |
193876 KB |
Output is correct |
41 |
Correct |
37 ms |
193880 KB |
Output is correct |
42 |
Correct |
29 ms |
193880 KB |
Output is correct |
43 |
Correct |
51 ms |
193860 KB |
Output is correct |
44 |
Correct |
34 ms |
193724 KB |
Output is correct |
45 |
Correct |
110 ms |
193888 KB |
Output is correct |
46 |
Correct |
100 ms |
199864 KB |
Output is correct |
47 |
Correct |
147 ms |
200316 KB |
Output is correct |
48 |
Correct |
114 ms |
199996 KB |
Output is correct |
49 |
Correct |
194 ms |
200020 KB |
Output is correct |
50 |
Correct |
98 ms |
199968 KB |
Output is correct |
51 |
Correct |
128 ms |
200016 KB |
Output is correct |
52 |
Correct |
95 ms |
200020 KB |
Output is correct |
53 |
Correct |
196 ms |
200020 KB |
Output is correct |
54 |
Correct |
98 ms |
200124 KB |
Output is correct |
55 |
Correct |
100 ms |
199996 KB |
Output is correct |
56 |
Correct |
185 ms |
199996 KB |
Output is correct |
57 |
Correct |
203 ms |
200272 KB |
Output is correct |
58 |
Correct |
98 ms |
200016 KB |
Output is correct |
59 |
Correct |
137 ms |
200020 KB |
Output is correct |
60 |
Correct |
176 ms |
200120 KB |
Output is correct |
61 |
Correct |
197 ms |
200016 KB |
Output is correct |
62 |
Correct |
95 ms |
199868 KB |
Output is correct |
63 |
Correct |
118 ms |
200020 KB |
Output is correct |
64 |
Correct |
133 ms |
200144 KB |
Output is correct |
65 |
Correct |
194 ms |
200020 KB |
Output is correct |