#pragma GCC optimize("O3", "inline")
#include <bits/stdc++.h>
using namespace std;
#define ins insert
#define pb push_back
#define int long long int
#define pii pair<int, int>
#define endl '\n'
#define drop(x) cout<<(x)<<endl; return;
#define all(x) x.begin(),x.end()
const int mod = 1e9 +7, sze = 2e5+10, inf = INT_MAX, prime = 1453;
int yol[80][80];
int dp[80][80][80];
void mal(){
int n,m;
cin>>n>>m;
for(int i =0;i<=n;i++){
for(int j=0;j<=n;j++){
fill(dp[i][j],dp[i][j]+80,inf);
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i!=j){
yol[i][j]=inf;
dp[0][i][j]=inf;
}
else{
for(int k=0;k<=n;k++){
dp[k][i][j]=0;
}
}
}
}
for(int i=1;i<=m;i++){
int u,v,t;
cin>>u>>v>>t;
yol[u][v]=min(yol[u][v],t);
}
int k,q;
cin>>k>>q;
k=min(k,n-1);
for(int d=0;d<=k;d++){
for(int x=1;x<=n;x++){
for(int y=1;y<=n;y++){
for(int z=1;z<=n;z++){
// cout<<d<<" "<<x<<" "<<y<<" "<<dp[d][x][z]<<" "<<yol[z][y]<<endl;
dp[d+1][x][y]=min(dp[d+1][x][y],dp[d][x][z]+yol[z][y]);
}
}
}
}
while(q--){
int u,v;
cin>>u>>v;
cout<<(dp[k][u][v]>=inf?-1:dp[k][u][v])<<endl;
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
clock_t z = clock();
int tt = 1;
// cin>>tt;
while(tt--){
mal();
}
// cerr<<(double)(clock() - z) / CLOCKS_PER_SEC <<endl;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:66:13: warning: unused variable 'z' [-Wunused-variable]
66 | clock_t z = clock();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3932 KB |
Output is correct |
2 |
Correct |
2 ms |
4028 KB |
Output is correct |
3 |
Correct |
3 ms |
3932 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
5 ms |
3932 KB |
Output is correct |
6 |
Correct |
3 ms |
4060 KB |
Output is correct |
7 |
Correct |
3 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
16 ms |
3932 KB |
Output is correct |
8 |
Correct |
24 ms |
4016 KB |
Output is correct |
9 |
Correct |
3 ms |
3928 KB |
Output is correct |
10 |
Correct |
11 ms |
4052 KB |
Output is correct |
11 |
Correct |
5 ms |
3932 KB |
Output is correct |
12 |
Correct |
13 ms |
4088 KB |
Output is correct |
13 |
Correct |
87 ms |
10128 KB |
Output is correct |
14 |
Correct |
64 ms |
9984 KB |
Output is correct |
15 |
Correct |
75 ms |
9988 KB |
Output is correct |
16 |
Correct |
99 ms |
10004 KB |
Output is correct |
17 |
Correct |
91 ms |
10068 KB |
Output is correct |
18 |
Correct |
66 ms |
10128 KB |
Output is correct |
19 |
Correct |
80 ms |
10064 KB |
Output is correct |
20 |
Correct |
98 ms |
10132 KB |
Output is correct |
21 |
Correct |
63 ms |
10068 KB |
Output is correct |
22 |
Correct |
66 ms |
10068 KB |
Output is correct |
23 |
Correct |
66 ms |
10136 KB |
Output is correct |
24 |
Correct |
85 ms |
10068 KB |
Output is correct |
25 |
Correct |
64 ms |
10112 KB |
Output is correct |
26 |
Correct |
66 ms |
10320 KB |
Output is correct |
27 |
Correct |
70 ms |
10268 KB |
Output is correct |
28 |
Correct |
68 ms |
10208 KB |
Output is correct |
29 |
Correct |
96 ms |
10068 KB |
Output is correct |
30 |
Correct |
64 ms |
9868 KB |
Output is correct |
31 |
Correct |
67 ms |
10068 KB |
Output is correct |
32 |
Correct |
71 ms |
9864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 ms |
3932 KB |
Output is correct |
8 |
Correct |
2 ms |
4028 KB |
Output is correct |
9 |
Correct |
3 ms |
3932 KB |
Output is correct |
10 |
Correct |
2 ms |
3932 KB |
Output is correct |
11 |
Correct |
5 ms |
3932 KB |
Output is correct |
12 |
Correct |
3 ms |
4060 KB |
Output is correct |
13 |
Correct |
3 ms |
3932 KB |
Output is correct |
14 |
Correct |
16 ms |
3932 KB |
Output is correct |
15 |
Correct |
24 ms |
4016 KB |
Output is correct |
16 |
Correct |
3 ms |
3928 KB |
Output is correct |
17 |
Correct |
11 ms |
4052 KB |
Output is correct |
18 |
Correct |
5 ms |
3932 KB |
Output is correct |
19 |
Correct |
13 ms |
4088 KB |
Output is correct |
20 |
Correct |
87 ms |
10128 KB |
Output is correct |
21 |
Correct |
64 ms |
9984 KB |
Output is correct |
22 |
Correct |
75 ms |
9988 KB |
Output is correct |
23 |
Correct |
99 ms |
10004 KB |
Output is correct |
24 |
Correct |
91 ms |
10068 KB |
Output is correct |
25 |
Correct |
66 ms |
10128 KB |
Output is correct |
26 |
Correct |
80 ms |
10064 KB |
Output is correct |
27 |
Correct |
98 ms |
10132 KB |
Output is correct |
28 |
Correct |
63 ms |
10068 KB |
Output is correct |
29 |
Correct |
66 ms |
10068 KB |
Output is correct |
30 |
Correct |
66 ms |
10136 KB |
Output is correct |
31 |
Correct |
85 ms |
10068 KB |
Output is correct |
32 |
Correct |
64 ms |
10112 KB |
Output is correct |
33 |
Correct |
66 ms |
10320 KB |
Output is correct |
34 |
Correct |
70 ms |
10268 KB |
Output is correct |
35 |
Correct |
68 ms |
10208 KB |
Output is correct |
36 |
Correct |
96 ms |
10068 KB |
Output is correct |
37 |
Correct |
64 ms |
9868 KB |
Output is correct |
38 |
Correct |
67 ms |
10068 KB |
Output is correct |
39 |
Correct |
71 ms |
9864 KB |
Output is correct |
40 |
Correct |
6 ms |
3932 KB |
Output is correct |
41 |
Correct |
19 ms |
4000 KB |
Output is correct |
42 |
Correct |
4 ms |
3808 KB |
Output is correct |
43 |
Correct |
18 ms |
3932 KB |
Output is correct |
44 |
Correct |
5 ms |
3932 KB |
Output is correct |
45 |
Correct |
18 ms |
4088 KB |
Output is correct |
46 |
Correct |
64 ms |
9996 KB |
Output is correct |
47 |
Correct |
65 ms |
9996 KB |
Output is correct |
48 |
Correct |
66 ms |
10000 KB |
Output is correct |
49 |
Correct |
109 ms |
10120 KB |
Output is correct |
50 |
Correct |
67 ms |
10132 KB |
Output is correct |
51 |
Correct |
64 ms |
10128 KB |
Output is correct |
52 |
Correct |
64 ms |
10004 KB |
Output is correct |
53 |
Correct |
73 ms |
10008 KB |
Output is correct |
54 |
Correct |
70 ms |
10132 KB |
Output is correct |
55 |
Correct |
65 ms |
10064 KB |
Output is correct |
56 |
Correct |
105 ms |
10252 KB |
Output is correct |
57 |
Correct |
86 ms |
10240 KB |
Output is correct |
58 |
Correct |
64 ms |
10324 KB |
Output is correct |
59 |
Correct |
74 ms |
10320 KB |
Output is correct |
60 |
Correct |
79 ms |
10320 KB |
Output is correct |
61 |
Correct |
80 ms |
10332 KB |
Output is correct |
62 |
Correct |
63 ms |
10084 KB |
Output is correct |
63 |
Correct |
72 ms |
10068 KB |
Output is correct |
64 |
Correct |
100 ms |
10068 KB |
Output is correct |
65 |
Correct |
83 ms |
10004 KB |
Output is correct |