#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n"
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
void solve(){
int n,m;
cin >> n >> m;
int dp[n+5][n+5][n+5];
int matr[n+5][n+5];
memset(dp,-1,sizeof(dp));
memset(matr,-1,sizeof(matr));
for(int i=1;i<=m;i++){
int a,b,c;
cin >> a >> b >> c;
if(matr[a][b]==-1) matr[a][b]=c;
else matr[a][b]=min(matr[a][b],c);
}
for(int i=1;i<=n;i++) matr[i][i]=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
dp[i][j][1]=matr[i][j];
int k,q;
cin >> k >> q;
for(int a=2;a<=min(k,n);a++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int u=1;u<=n;u++){
if(dp[i][u][a-1] == -1 || matr[u][j] == -1) continue;
if(dp[i][j][a]==-1) dp[i][j][a]=dp[i][u][a-1]+matr[u][j];
else dp[i][j][a]=min(dp[i][j][a],dp[i][u][a-1]+matr[u][j]);
}
}
}
}
while(q--){
int a,b;
cin >> a >> b;
int ans=-1;
for(int i=1;i<=min(k,n);i++){
if(dp[a][b][i]==-1) continue;
if(ans==-1) ans=dp[a][b][i];
else ans=min(ans,dp[a][b][i]);
}
cout << ans << endl;
}
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int t=1;//cin >> t;
while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3676 KB |
Output is correct |
2 |
Correct |
4 ms |
3816 KB |
Output is correct |
3 |
Correct |
6 ms |
3676 KB |
Output is correct |
4 |
Correct |
4 ms |
3676 KB |
Output is correct |
5 |
Correct |
5 ms |
3836 KB |
Output is correct |
6 |
Correct |
5 ms |
3676 KB |
Output is correct |
7 |
Correct |
7 ms |
3796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
23 ms |
3676 KB |
Output is correct |
8 |
Correct |
59 ms |
3788 KB |
Output is correct |
9 |
Correct |
7 ms |
3676 KB |
Output is correct |
10 |
Correct |
45 ms |
3676 KB |
Output is correct |
11 |
Correct |
16 ms |
3676 KB |
Output is correct |
12 |
Correct |
60 ms |
3852 KB |
Output is correct |
13 |
Correct |
74 ms |
9920 KB |
Output is correct |
14 |
Correct |
75 ms |
9908 KB |
Output is correct |
15 |
Correct |
104 ms |
9780 KB |
Output is correct |
16 |
Correct |
88 ms |
9804 KB |
Output is correct |
17 |
Correct |
88 ms |
9784 KB |
Output is correct |
18 |
Correct |
76 ms |
9812 KB |
Output is correct |
19 |
Correct |
145 ms |
9808 KB |
Output is correct |
20 |
Correct |
94 ms |
9784 KB |
Output is correct |
21 |
Correct |
84 ms |
10036 KB |
Output is correct |
22 |
Correct |
83 ms |
9916 KB |
Output is correct |
23 |
Correct |
83 ms |
10052 KB |
Output is correct |
24 |
Correct |
93 ms |
9812 KB |
Output is correct |
25 |
Correct |
88 ms |
10068 KB |
Output is correct |
26 |
Correct |
84 ms |
9908 KB |
Output is correct |
27 |
Correct |
88 ms |
10048 KB |
Output is correct |
28 |
Correct |
86 ms |
9912 KB |
Output is correct |
29 |
Correct |
82 ms |
9908 KB |
Output is correct |
30 |
Correct |
81 ms |
9812 KB |
Output is correct |
31 |
Correct |
94 ms |
9780 KB |
Output is correct |
32 |
Correct |
114 ms |
9868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
3676 KB |
Output is correct |
8 |
Correct |
4 ms |
3816 KB |
Output is correct |
9 |
Correct |
6 ms |
3676 KB |
Output is correct |
10 |
Correct |
4 ms |
3676 KB |
Output is correct |
11 |
Correct |
5 ms |
3836 KB |
Output is correct |
12 |
Correct |
5 ms |
3676 KB |
Output is correct |
13 |
Correct |
7 ms |
3796 KB |
Output is correct |
14 |
Correct |
23 ms |
3676 KB |
Output is correct |
15 |
Correct |
59 ms |
3788 KB |
Output is correct |
16 |
Correct |
7 ms |
3676 KB |
Output is correct |
17 |
Correct |
45 ms |
3676 KB |
Output is correct |
18 |
Correct |
16 ms |
3676 KB |
Output is correct |
19 |
Correct |
60 ms |
3852 KB |
Output is correct |
20 |
Correct |
74 ms |
9920 KB |
Output is correct |
21 |
Correct |
75 ms |
9908 KB |
Output is correct |
22 |
Correct |
104 ms |
9780 KB |
Output is correct |
23 |
Correct |
88 ms |
9804 KB |
Output is correct |
24 |
Correct |
88 ms |
9784 KB |
Output is correct |
25 |
Correct |
76 ms |
9812 KB |
Output is correct |
26 |
Correct |
145 ms |
9808 KB |
Output is correct |
27 |
Correct |
94 ms |
9784 KB |
Output is correct |
28 |
Correct |
84 ms |
10036 KB |
Output is correct |
29 |
Correct |
83 ms |
9916 KB |
Output is correct |
30 |
Correct |
83 ms |
10052 KB |
Output is correct |
31 |
Correct |
93 ms |
9812 KB |
Output is correct |
32 |
Correct |
88 ms |
10068 KB |
Output is correct |
33 |
Correct |
84 ms |
9908 KB |
Output is correct |
34 |
Correct |
88 ms |
10048 KB |
Output is correct |
35 |
Correct |
86 ms |
9912 KB |
Output is correct |
36 |
Correct |
82 ms |
9908 KB |
Output is correct |
37 |
Correct |
81 ms |
9812 KB |
Output is correct |
38 |
Correct |
94 ms |
9780 KB |
Output is correct |
39 |
Correct |
114 ms |
9868 KB |
Output is correct |
40 |
Correct |
17 ms |
3672 KB |
Output is correct |
41 |
Correct |
66 ms |
3788 KB |
Output is correct |
42 |
Correct |
11 ms |
3676 KB |
Output is correct |
43 |
Correct |
78 ms |
3812 KB |
Output is correct |
44 |
Correct |
16 ms |
3676 KB |
Output is correct |
45 |
Correct |
87 ms |
3676 KB |
Output is correct |
46 |
Correct |
76 ms |
9812 KB |
Output is correct |
47 |
Correct |
91 ms |
9796 KB |
Output is correct |
48 |
Correct |
90 ms |
10036 KB |
Output is correct |
49 |
Correct |
113 ms |
9912 KB |
Output is correct |
50 |
Correct |
75 ms |
9920 KB |
Output is correct |
51 |
Correct |
84 ms |
9908 KB |
Output is correct |
52 |
Correct |
75 ms |
9812 KB |
Output is correct |
53 |
Correct |
111 ms |
9808 KB |
Output is correct |
54 |
Correct |
79 ms |
9916 KB |
Output is correct |
55 |
Correct |
80 ms |
10064 KB |
Output is correct |
56 |
Correct |
112 ms |
10040 KB |
Output is correct |
57 |
Correct |
111 ms |
10032 KB |
Output is correct |
58 |
Correct |
80 ms |
10068 KB |
Output is correct |
59 |
Correct |
91 ms |
10048 KB |
Output is correct |
60 |
Correct |
105 ms |
10068 KB |
Output is correct |
61 |
Correct |
119 ms |
10168 KB |
Output is correct |
62 |
Correct |
74 ms |
9812 KB |
Output is correct |
63 |
Correct |
82 ms |
9656 KB |
Output is correct |
64 |
Correct |
93 ms |
9868 KB |
Output is correct |
65 |
Correct |
114 ms |
10068 KB |
Output is correct |