#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=100;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=20;
int t[N],odl[N][N],dp[N][N][N];
void solve(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) odl[i][j]=-1;
for(int a,b,c,i=1;i<=m;i++){
cin>>a>>b>>c;
if(odl[a][b]==-1) odl[a][b]=c;
else odl[a][b]=min(odl[a][b],c);
}
for(int i=1;i<=n;i++) odl[i][i]=0;
int k,q;
cin>>k>>q;
k=min(k,n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) dp[i][j][0]=odl[i][j];
for(int il=1;il<=n;il++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dp[i][j][il]=-1;
for(int sr=1;sr<=n;sr++){
if(dp[i][sr][il-1]!=-1 and odl[sr][j]!=-1){
if(dp[i][j][il]==-1) dp[i][j][il]=dp[i][sr][il-1]+odl[sr][j];
else dp[i][j][il]=min(dp[i][j][il],dp[i][sr][il-1]+odl[sr][j]);
}
}
}
}
}
while(q--){
int a,b;
cin>>a>>b;
cout<<dp[a][b][k-1]<<"\n";
}
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int tt=1;
while(tt--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
2556 KB |
Output is correct |
2 |
Correct |
68 ms |
2568 KB |
Output is correct |
3 |
Correct |
57 ms |
2548 KB |
Output is correct |
4 |
Correct |
72 ms |
2572 KB |
Output is correct |
5 |
Correct |
69 ms |
2680 KB |
Output is correct |
6 |
Correct |
68 ms |
2620 KB |
Output is correct |
7 |
Correct |
69 ms |
2596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
48 ms |
2572 KB |
Output is correct |
8 |
Correct |
48 ms |
2516 KB |
Output is correct |
9 |
Correct |
72 ms |
2560 KB |
Output is correct |
10 |
Correct |
74 ms |
2644 KB |
Output is correct |
11 |
Correct |
71 ms |
2616 KB |
Output is correct |
12 |
Correct |
68 ms |
2620 KB |
Output is correct |
13 |
Correct |
167 ms |
8664 KB |
Output is correct |
14 |
Correct |
170 ms |
8656 KB |
Output is correct |
15 |
Correct |
176 ms |
8676 KB |
Output is correct |
16 |
Correct |
163 ms |
8648 KB |
Output is correct |
17 |
Correct |
164 ms |
8660 KB |
Output is correct |
18 |
Correct |
184 ms |
8652 KB |
Output is correct |
19 |
Correct |
172 ms |
8648 KB |
Output is correct |
20 |
Correct |
173 ms |
8744 KB |
Output is correct |
21 |
Correct |
183 ms |
8780 KB |
Output is correct |
22 |
Correct |
195 ms |
8896 KB |
Output is correct |
23 |
Correct |
169 ms |
8776 KB |
Output is correct |
24 |
Correct |
188 ms |
8788 KB |
Output is correct |
25 |
Correct |
202 ms |
8784 KB |
Output is correct |
26 |
Correct |
165 ms |
8776 KB |
Output is correct |
27 |
Correct |
169 ms |
8912 KB |
Output is correct |
28 |
Correct |
166 ms |
8872 KB |
Output is correct |
29 |
Correct |
176 ms |
8616 KB |
Output is correct |
30 |
Correct |
177 ms |
8532 KB |
Output is correct |
31 |
Correct |
174 ms |
8520 KB |
Output is correct |
32 |
Correct |
169 ms |
8616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
55 ms |
2556 KB |
Output is correct |
8 |
Correct |
68 ms |
2568 KB |
Output is correct |
9 |
Correct |
57 ms |
2548 KB |
Output is correct |
10 |
Correct |
72 ms |
2572 KB |
Output is correct |
11 |
Correct |
69 ms |
2680 KB |
Output is correct |
12 |
Correct |
68 ms |
2620 KB |
Output is correct |
13 |
Correct |
69 ms |
2596 KB |
Output is correct |
14 |
Correct |
48 ms |
2572 KB |
Output is correct |
15 |
Correct |
48 ms |
2516 KB |
Output is correct |
16 |
Correct |
72 ms |
2560 KB |
Output is correct |
17 |
Correct |
74 ms |
2644 KB |
Output is correct |
18 |
Correct |
71 ms |
2616 KB |
Output is correct |
19 |
Correct |
68 ms |
2620 KB |
Output is correct |
20 |
Correct |
167 ms |
8664 KB |
Output is correct |
21 |
Correct |
170 ms |
8656 KB |
Output is correct |
22 |
Correct |
176 ms |
8676 KB |
Output is correct |
23 |
Correct |
163 ms |
8648 KB |
Output is correct |
24 |
Correct |
164 ms |
8660 KB |
Output is correct |
25 |
Correct |
184 ms |
8652 KB |
Output is correct |
26 |
Correct |
172 ms |
8648 KB |
Output is correct |
27 |
Correct |
173 ms |
8744 KB |
Output is correct |
28 |
Correct |
183 ms |
8780 KB |
Output is correct |
29 |
Correct |
195 ms |
8896 KB |
Output is correct |
30 |
Correct |
169 ms |
8776 KB |
Output is correct |
31 |
Correct |
188 ms |
8788 KB |
Output is correct |
32 |
Correct |
202 ms |
8784 KB |
Output is correct |
33 |
Correct |
165 ms |
8776 KB |
Output is correct |
34 |
Correct |
169 ms |
8912 KB |
Output is correct |
35 |
Correct |
166 ms |
8872 KB |
Output is correct |
36 |
Correct |
176 ms |
8616 KB |
Output is correct |
37 |
Correct |
177 ms |
8532 KB |
Output is correct |
38 |
Correct |
174 ms |
8520 KB |
Output is correct |
39 |
Correct |
169 ms |
8616 KB |
Output is correct |
40 |
Correct |
61 ms |
2692 KB |
Output is correct |
41 |
Correct |
50 ms |
2636 KB |
Output is correct |
42 |
Correct |
67 ms |
2564 KB |
Output is correct |
43 |
Correct |
68 ms |
2572 KB |
Output is correct |
44 |
Correct |
66 ms |
2516 KB |
Output is correct |
45 |
Correct |
66 ms |
2516 KB |
Output is correct |
46 |
Correct |
189 ms |
8796 KB |
Output is correct |
47 |
Correct |
172 ms |
8648 KB |
Output is correct |
48 |
Correct |
169 ms |
8656 KB |
Output is correct |
49 |
Correct |
169 ms |
8908 KB |
Output is correct |
50 |
Correct |
170 ms |
8652 KB |
Output is correct |
51 |
Correct |
195 ms |
8756 KB |
Output is correct |
52 |
Correct |
171 ms |
8652 KB |
Output is correct |
53 |
Correct |
169 ms |
8656 KB |
Output is correct |
54 |
Correct |
167 ms |
8776 KB |
Output is correct |
55 |
Correct |
172 ms |
8784 KB |
Output is correct |
56 |
Correct |
210 ms |
8780 KB |
Output is correct |
57 |
Correct |
172 ms |
8784 KB |
Output is correct |
58 |
Correct |
195 ms |
8872 KB |
Output is correct |
59 |
Correct |
189 ms |
8876 KB |
Output is correct |
60 |
Correct |
171 ms |
8880 KB |
Output is correct |
61 |
Correct |
181 ms |
8788 KB |
Output is correct |
62 |
Correct |
183 ms |
8660 KB |
Output is correct |
63 |
Correct |
180 ms |
8528 KB |
Output is correct |
64 |
Correct |
172 ms |
8532 KB |
Output is correct |
65 |
Correct |
184 ms |
8672 KB |
Output is correct |