#include<bits/stdc++.h>
#define el '\n'
#define fi first
#define sc second
#define int ll
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
const int mod=1e9+7;
const int N=81;
int n, m, q, k, adj[N][N], ans[N][N], dp[N][N][N];
void sol()
{
cin >> n >> m;
for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) if(i!=j) adj[i][j]=1e9;
for(int i=0; i<=n; i++) for(int j=0; j<=n; j++) for(int l=0;l<=n;l++) dp[i][j][l]=1e9;
for(int i=1; i<=m; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj[u][v]=min(adj[u][v], w);
}
for(int i=1; i<=n; i++) dp[0][i][i]=0;
for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) dp[1][i][j] = adj[i][j];
cin >> k >> q;
for(int i=2; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
for(int x=1; x<=n; x++)
{
for(int y=1; y<=n; y++)
{
dp[i][j][x]=min(dp[i][j][x], dp[i-1][j][y]+adj[y][x]);
}
}
}
}
for(int x=1; x<=n; x++)
{
for(int y=1; y<=n; y++)
{
if(x!=y) ans[x][y]=1e9;
for(int i=0; i<=min(n, k); i++)
{
ans[x][y]=min(ans[x][y], dp[i][x][y]);
}
}
}
while(q--)
{
int u, v;
cin >> u >> v;
if(ans[u][v]==1e9) cout << -1 << el;
else cout << ans[u][v] << el;
}
}
signed main()
{
// freopen("divisor.INP", "r", stdin);
// freopen("divisor.OUT", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
while(t--)
{
sol();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2512 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
3932 KB |
Output is correct |
2 |
Correct |
18 ms |
4104 KB |
Output is correct |
3 |
Correct |
19 ms |
4188 KB |
Output is correct |
4 |
Correct |
23 ms |
3932 KB |
Output is correct |
5 |
Correct |
19 ms |
3932 KB |
Output is correct |
6 |
Correct |
24 ms |
4184 KB |
Output is correct |
7 |
Correct |
19 ms |
4160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2512 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
19 ms |
4176 KB |
Output is correct |
8 |
Correct |
20 ms |
4188 KB |
Output is correct |
9 |
Correct |
19 ms |
4116 KB |
Output is correct |
10 |
Correct |
19 ms |
3932 KB |
Output is correct |
11 |
Correct |
19 ms |
4148 KB |
Output is correct |
12 |
Correct |
19 ms |
4184 KB |
Output is correct |
13 |
Correct |
84 ms |
10344 KB |
Output is correct |
14 |
Correct |
87 ms |
10068 KB |
Output is correct |
15 |
Correct |
85 ms |
10200 KB |
Output is correct |
16 |
Correct |
83 ms |
10204 KB |
Output is correct |
17 |
Correct |
87 ms |
10320 KB |
Output is correct |
18 |
Correct |
91 ms |
10316 KB |
Output is correct |
19 |
Correct |
87 ms |
10324 KB |
Output is correct |
20 |
Correct |
87 ms |
10188 KB |
Output is correct |
21 |
Correct |
85 ms |
10316 KB |
Output is correct |
22 |
Correct |
89 ms |
10180 KB |
Output is correct |
23 |
Correct |
80 ms |
10324 KB |
Output is correct |
24 |
Correct |
90 ms |
10200 KB |
Output is correct |
25 |
Correct |
88 ms |
10320 KB |
Output is correct |
26 |
Correct |
86 ms |
10316 KB |
Output is correct |
27 |
Correct |
85 ms |
10576 KB |
Output is correct |
28 |
Correct |
82 ms |
10320 KB |
Output is correct |
29 |
Correct |
101 ms |
10180 KB |
Output is correct |
30 |
Correct |
85 ms |
10064 KB |
Output is correct |
31 |
Correct |
83 ms |
10200 KB |
Output is correct |
32 |
Correct |
83 ms |
10064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2512 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
19 ms |
3932 KB |
Output is correct |
8 |
Correct |
18 ms |
4104 KB |
Output is correct |
9 |
Correct |
19 ms |
4188 KB |
Output is correct |
10 |
Correct |
23 ms |
3932 KB |
Output is correct |
11 |
Correct |
19 ms |
3932 KB |
Output is correct |
12 |
Correct |
24 ms |
4184 KB |
Output is correct |
13 |
Correct |
19 ms |
4160 KB |
Output is correct |
14 |
Correct |
19 ms |
4176 KB |
Output is correct |
15 |
Correct |
20 ms |
4188 KB |
Output is correct |
16 |
Correct |
19 ms |
4116 KB |
Output is correct |
17 |
Correct |
19 ms |
3932 KB |
Output is correct |
18 |
Correct |
19 ms |
4148 KB |
Output is correct |
19 |
Correct |
19 ms |
4184 KB |
Output is correct |
20 |
Correct |
84 ms |
10344 KB |
Output is correct |
21 |
Correct |
87 ms |
10068 KB |
Output is correct |
22 |
Correct |
85 ms |
10200 KB |
Output is correct |
23 |
Correct |
83 ms |
10204 KB |
Output is correct |
24 |
Correct |
87 ms |
10320 KB |
Output is correct |
25 |
Correct |
91 ms |
10316 KB |
Output is correct |
26 |
Correct |
87 ms |
10324 KB |
Output is correct |
27 |
Correct |
87 ms |
10188 KB |
Output is correct |
28 |
Correct |
85 ms |
10316 KB |
Output is correct |
29 |
Correct |
89 ms |
10180 KB |
Output is correct |
30 |
Correct |
80 ms |
10324 KB |
Output is correct |
31 |
Correct |
90 ms |
10200 KB |
Output is correct |
32 |
Correct |
88 ms |
10320 KB |
Output is correct |
33 |
Correct |
86 ms |
10316 KB |
Output is correct |
34 |
Correct |
85 ms |
10576 KB |
Output is correct |
35 |
Correct |
82 ms |
10320 KB |
Output is correct |
36 |
Correct |
101 ms |
10180 KB |
Output is correct |
37 |
Correct |
85 ms |
10064 KB |
Output is correct |
38 |
Correct |
83 ms |
10200 KB |
Output is correct |
39 |
Correct |
83 ms |
10064 KB |
Output is correct |
40 |
Correct |
21 ms |
4176 KB |
Output is correct |
41 |
Correct |
18 ms |
4180 KB |
Output is correct |
42 |
Correct |
20 ms |
3928 KB |
Output is correct |
43 |
Correct |
19 ms |
4188 KB |
Output is correct |
44 |
Correct |
19 ms |
4188 KB |
Output is correct |
45 |
Correct |
20 ms |
4188 KB |
Output is correct |
46 |
Correct |
82 ms |
10064 KB |
Output is correct |
47 |
Correct |
84 ms |
10076 KB |
Output is correct |
48 |
Correct |
105 ms |
10060 KB |
Output is correct |
49 |
Correct |
81 ms |
10072 KB |
Output is correct |
50 |
Correct |
114 ms |
10320 KB |
Output is correct |
51 |
Correct |
93 ms |
10196 KB |
Output is correct |
52 |
Correct |
83 ms |
10196 KB |
Output is correct |
53 |
Correct |
95 ms |
10204 KB |
Output is correct |
54 |
Correct |
83 ms |
10328 KB |
Output is correct |
55 |
Correct |
85 ms |
10320 KB |
Output is correct |
56 |
Correct |
99 ms |
10324 KB |
Output is correct |
57 |
Correct |
87 ms |
10324 KB |
Output is correct |
58 |
Correct |
91 ms |
10328 KB |
Output is correct |
59 |
Correct |
83 ms |
10316 KB |
Output is correct |
60 |
Correct |
97 ms |
10452 KB |
Output is correct |
61 |
Correct |
107 ms |
10320 KB |
Output is correct |
62 |
Correct |
83 ms |
10064 KB |
Output is correct |
63 |
Correct |
105 ms |
10184 KB |
Output is correct |
64 |
Correct |
106 ms |
10204 KB |
Output is correct |
65 |
Correct |
83 ms |
10068 KB |
Output is correct |