#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
#define clr(a, x) memset(a, x, sizeof(a))
#define dbg(x) cout<<"("<<#x<<"): "<<x<<endl;
#define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl;
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b)/__gcd(a, b)
#define int long long int
#define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl;
#define endl '\n'
#define float long double
const int MOD = 1e9 + 7;
const int INF = 2e15;
const int MAXN = 74;
int dp[MAXN][MAXN][MAXN];
int adj[MAXN][MAXN];
vector<pair<int, int>> dest[MAXN];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin>>n>>m;
clr(adj, -1);
for(int i= 0;i<m;i++){
int t1, t2, t3;
cin>>t1>>t2>>t3;
if(adj[t1][t2] == -1){
adj[t1][t2] = t3;
} else {
adj[t1][t2] = min(adj[t1][t2], t3);
}
}
for(int i= 1;i<=n;i++){
for(int j= 1;j<=n;j++){
if(adj[j][i] != -1){
dest[i].pb({j, adj[j][i]});
}
}
}
int q, K;
cin>>K>>q;
clr(dp, -1);
for(int i = 1;i<=n;i++){
dp[i][i][0] = 0;
for(int j= 1;j<=n;j++){
dp[i][j][1] = adj[i][j];
}
dp[i][i][1] = 0;
}
for(int k = 2;k<=min(n+1, K);k++){
for(int i = 1;i<=n;i++){
for(int j= 1;j<=n;j++){
if(i == j) {
dp[i][j][k] = 0;
continue;
}
dp[i][j][k] = INF;
for(auto x: dest[j]){
if(dp[i][x.fr][k-1] == -1) continue;
dp[i][j][k] = min(dp[i][j][k], dp[i][x.fr][k-1]+x.sc);
}
if(dp[i][j][k] == INF){
dp[i][j][k] = -1;
}
}
}
}
while(q--){
int t1, t2;
cin>>t1>>t2;
int ans = INF;
for(int i= 1;i<=min(n+1, K);i++){
if(dp[t1][t2][i] == -1) continue;
ans = min(ans, dp[t1][t2][i]);
}
if(ans == INF) ans = -1;
cout<<ans<<endl;
}
return 0;
}
// At most 70*70 bus routes
// q = 70*70
// DP[i][j][k] = min(DP[i][a][x] + DP[b][j][k-x] + T(ab))
// dp[i][j][k] = min(dp[i][a][k-1] + T(aj))
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3532 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
3 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3540 KB |
Output is correct |
2 |
Correct |
3 ms |
3608 KB |
Output is correct |
3 |
Correct |
3 ms |
3540 KB |
Output is correct |
4 |
Correct |
3 ms |
3540 KB |
Output is correct |
5 |
Correct |
4 ms |
3548 KB |
Output is correct |
6 |
Correct |
5 ms |
3672 KB |
Output is correct |
7 |
Correct |
5 ms |
3668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3532 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
3 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
7 |
Correct |
5 ms |
3540 KB |
Output is correct |
8 |
Correct |
9 ms |
3584 KB |
Output is correct |
9 |
Correct |
4 ms |
3548 KB |
Output is correct |
10 |
Correct |
9 ms |
3540 KB |
Output is correct |
11 |
Correct |
10 ms |
3672 KB |
Output is correct |
12 |
Correct |
28 ms |
3668 KB |
Output is correct |
13 |
Correct |
117 ms |
9944 KB |
Output is correct |
14 |
Correct |
101 ms |
9820 KB |
Output is correct |
15 |
Correct |
129 ms |
9808 KB |
Output is correct |
16 |
Correct |
112 ms |
9820 KB |
Output is correct |
17 |
Correct |
93 ms |
9884 KB |
Output is correct |
18 |
Correct |
93 ms |
9788 KB |
Output is correct |
19 |
Correct |
136 ms |
9900 KB |
Output is correct |
20 |
Correct |
120 ms |
9824 KB |
Output is correct |
21 |
Correct |
94 ms |
9940 KB |
Output is correct |
22 |
Correct |
103 ms |
9932 KB |
Output is correct |
23 |
Correct |
105 ms |
9948 KB |
Output is correct |
24 |
Correct |
115 ms |
9932 KB |
Output is correct |
25 |
Correct |
99 ms |
9932 KB |
Output is correct |
26 |
Correct |
102 ms |
10060 KB |
Output is correct |
27 |
Correct |
124 ms |
9948 KB |
Output is correct |
28 |
Correct |
112 ms |
10016 KB |
Output is correct |
29 |
Correct |
99 ms |
9692 KB |
Output is correct |
30 |
Correct |
101 ms |
9692 KB |
Output is correct |
31 |
Correct |
121 ms |
9752 KB |
Output is correct |
32 |
Correct |
140 ms |
9756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3532 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
3 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
7 |
Correct |
3 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3608 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
10 |
Correct |
3 ms |
3540 KB |
Output is correct |
11 |
Correct |
4 ms |
3548 KB |
Output is correct |
12 |
Correct |
5 ms |
3672 KB |
Output is correct |
13 |
Correct |
5 ms |
3668 KB |
Output is correct |
14 |
Correct |
5 ms |
3540 KB |
Output is correct |
15 |
Correct |
9 ms |
3584 KB |
Output is correct |
16 |
Correct |
4 ms |
3548 KB |
Output is correct |
17 |
Correct |
9 ms |
3540 KB |
Output is correct |
18 |
Correct |
10 ms |
3672 KB |
Output is correct |
19 |
Correct |
28 ms |
3668 KB |
Output is correct |
20 |
Correct |
117 ms |
9944 KB |
Output is correct |
21 |
Correct |
101 ms |
9820 KB |
Output is correct |
22 |
Correct |
129 ms |
9808 KB |
Output is correct |
23 |
Correct |
112 ms |
9820 KB |
Output is correct |
24 |
Correct |
93 ms |
9884 KB |
Output is correct |
25 |
Correct |
93 ms |
9788 KB |
Output is correct |
26 |
Correct |
136 ms |
9900 KB |
Output is correct |
27 |
Correct |
120 ms |
9824 KB |
Output is correct |
28 |
Correct |
94 ms |
9940 KB |
Output is correct |
29 |
Correct |
103 ms |
9932 KB |
Output is correct |
30 |
Correct |
105 ms |
9948 KB |
Output is correct |
31 |
Correct |
115 ms |
9932 KB |
Output is correct |
32 |
Correct |
99 ms |
9932 KB |
Output is correct |
33 |
Correct |
102 ms |
10060 KB |
Output is correct |
34 |
Correct |
124 ms |
9948 KB |
Output is correct |
35 |
Correct |
112 ms |
10016 KB |
Output is correct |
36 |
Correct |
99 ms |
9692 KB |
Output is correct |
37 |
Correct |
101 ms |
9692 KB |
Output is correct |
38 |
Correct |
121 ms |
9752 KB |
Output is correct |
39 |
Correct |
140 ms |
9756 KB |
Output is correct |
40 |
Correct |
4 ms |
3548 KB |
Output is correct |
41 |
Correct |
10 ms |
3584 KB |
Output is correct |
42 |
Correct |
4 ms |
3540 KB |
Output is correct |
43 |
Correct |
14 ms |
3540 KB |
Output is correct |
44 |
Correct |
8 ms |
3668 KB |
Output is correct |
45 |
Correct |
35 ms |
3684 KB |
Output is correct |
46 |
Correct |
116 ms |
9940 KB |
Output is correct |
47 |
Correct |
111 ms |
9816 KB |
Output is correct |
48 |
Correct |
97 ms |
9820 KB |
Output is correct |
49 |
Correct |
134 ms |
9808 KB |
Output is correct |
50 |
Correct |
92 ms |
9816 KB |
Output is correct |
51 |
Correct |
103 ms |
9824 KB |
Output is correct |
52 |
Correct |
91 ms |
9816 KB |
Output is correct |
53 |
Correct |
139 ms |
9820 KB |
Output is correct |
54 |
Correct |
91 ms |
9936 KB |
Output is correct |
55 |
Correct |
98 ms |
9944 KB |
Output is correct |
56 |
Correct |
130 ms |
9944 KB |
Output is correct |
57 |
Correct |
134 ms |
9948 KB |
Output is correct |
58 |
Correct |
92 ms |
9948 KB |
Output is correct |
59 |
Correct |
107 ms |
10020 KB |
Output is correct |
60 |
Correct |
125 ms |
9960 KB |
Output is correct |
61 |
Correct |
134 ms |
9956 KB |
Output is correct |
62 |
Correct |
89 ms |
9692 KB |
Output is correct |
63 |
Correct |
105 ms |
9676 KB |
Output is correct |
64 |
Correct |
104 ms |
9676 KB |
Output is correct |
65 |
Correct |
135 ms |
9684 KB |
Output is correct |