#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,cnt);
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 |
193616 KB |
Output is correct |
2 |
Correct |
22 ms |
193620 KB |
Output is correct |
3 |
Correct |
22 ms |
193704 KB |
Output is correct |
4 |
Correct |
23 ms |
193620 KB |
Output is correct |
5 |
Correct |
22 ms |
193628 KB |
Output is correct |
6 |
Correct |
22 ms |
193624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
193884 KB |
Output is correct |
2 |
Correct |
25 ms |
193760 KB |
Output is correct |
3 |
Correct |
24 ms |
193900 KB |
Output is correct |
4 |
Correct |
24 ms |
193884 KB |
Output is correct |
5 |
Correct |
27 ms |
193708 KB |
Output is correct |
6 |
Correct |
26 ms |
193888 KB |
Output is correct |
7 |
Correct |
28 ms |
193876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
193616 KB |
Output is correct |
2 |
Correct |
22 ms |
193620 KB |
Output is correct |
3 |
Correct |
22 ms |
193704 KB |
Output is correct |
4 |
Correct |
23 ms |
193620 KB |
Output is correct |
5 |
Correct |
22 ms |
193628 KB |
Output is correct |
6 |
Correct |
22 ms |
193624 KB |
Output is correct |
7 |
Correct |
28 ms |
193880 KB |
Output is correct |
8 |
Correct |
36 ms |
193868 KB |
Output is correct |
9 |
Correct |
25 ms |
193880 KB |
Output is correct |
10 |
Correct |
39 ms |
193844 KB |
Output is correct |
11 |
Correct |
35 ms |
193880 KB |
Output is correct |
12 |
Correct |
71 ms |
193876 KB |
Output is correct |
13 |
Correct |
96 ms |
193872 KB |
Output is correct |
14 |
Correct |
107 ms |
193872 KB |
Output is correct |
15 |
Correct |
180 ms |
193924 KB |
Output is correct |
16 |
Correct |
121 ms |
193752 KB |
Output is correct |
17 |
Correct |
103 ms |
193740 KB |
Output is correct |
18 |
Correct |
116 ms |
193768 KB |
Output is correct |
19 |
Correct |
199 ms |
193936 KB |
Output is correct |
20 |
Correct |
163 ms |
193872 KB |
Output is correct |
21 |
Correct |
94 ms |
193744 KB |
Output is correct |
22 |
Correct |
123 ms |
193740 KB |
Output is correct |
23 |
Correct |
116 ms |
193740 KB |
Output is correct |
24 |
Correct |
163 ms |
193740 KB |
Output is correct |
25 |
Correct |
99 ms |
193876 KB |
Output is correct |
26 |
Correct |
150 ms |
193752 KB |
Output is correct |
27 |
Correct |
126 ms |
193816 KB |
Output is correct |
28 |
Correct |
126 ms |
193872 KB |
Output is correct |
29 |
Correct |
97 ms |
193876 KB |
Output is correct |
30 |
Correct |
112 ms |
193936 KB |
Output is correct |
31 |
Correct |
146 ms |
193752 KB |
Output is correct |
32 |
Correct |
193 ms |
193948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
193616 KB |
Output is correct |
2 |
Correct |
22 ms |
193620 KB |
Output is correct |
3 |
Correct |
22 ms |
193704 KB |
Output is correct |
4 |
Correct |
23 ms |
193620 KB |
Output is correct |
5 |
Correct |
22 ms |
193628 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 |
193760 KB |
Output is correct |
9 |
Correct |
24 ms |
193900 KB |
Output is correct |
10 |
Correct |
24 ms |
193884 KB |
Output is correct |
11 |
Correct |
27 ms |
193708 KB |
Output is correct |
12 |
Correct |
26 ms |
193888 KB |
Output is correct |
13 |
Correct |
28 ms |
193876 KB |
Output is correct |
14 |
Correct |
28 ms |
193880 KB |
Output is correct |
15 |
Correct |
36 ms |
193868 KB |
Output is correct |
16 |
Correct |
25 ms |
193880 KB |
Output is correct |
17 |
Correct |
39 ms |
193844 KB |
Output is correct |
18 |
Correct |
35 ms |
193880 KB |
Output is correct |
19 |
Correct |
71 ms |
193876 KB |
Output is correct |
20 |
Correct |
96 ms |
193872 KB |
Output is correct |
21 |
Correct |
107 ms |
193872 KB |
Output is correct |
22 |
Correct |
180 ms |
193924 KB |
Output is correct |
23 |
Correct |
121 ms |
193752 KB |
Output is correct |
24 |
Correct |
103 ms |
193740 KB |
Output is correct |
25 |
Correct |
116 ms |
193768 KB |
Output is correct |
26 |
Correct |
199 ms |
193936 KB |
Output is correct |
27 |
Correct |
163 ms |
193872 KB |
Output is correct |
28 |
Correct |
94 ms |
193744 KB |
Output is correct |
29 |
Correct |
123 ms |
193740 KB |
Output is correct |
30 |
Correct |
116 ms |
193740 KB |
Output is correct |
31 |
Correct |
163 ms |
193740 KB |
Output is correct |
32 |
Correct |
99 ms |
193876 KB |
Output is correct |
33 |
Correct |
150 ms |
193752 KB |
Output is correct |
34 |
Correct |
126 ms |
193816 KB |
Output is correct |
35 |
Correct |
126 ms |
193872 KB |
Output is correct |
36 |
Correct |
97 ms |
193876 KB |
Output is correct |
37 |
Correct |
112 ms |
193936 KB |
Output is correct |
38 |
Correct |
146 ms |
193752 KB |
Output is correct |
39 |
Correct |
193 ms |
193948 KB |
Output is correct |
40 |
Correct |
27 ms |
193884 KB |
Output is correct |
41 |
Correct |
91 ms |
194152 KB |
Output is correct |
42 |
Correct |
27 ms |
193880 KB |
Output is correct |
43 |
Correct |
409 ms |
193940 KB |
Output is correct |
44 |
Correct |
34 ms |
193748 KB |
Output is correct |
45 |
Execution timed out |
1027 ms |
194384 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |