/*
In the name of Allah
made by: Ahmed_Solyman
*/
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O1")
//-------------------------------------------------------------//
typedef long long ll;
typedef unsigned long long ull;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sum_to(n) (n*(n+1))/2
#define pb push_back
#define pf push_front
#define fil(arr,x) memset(arr,x,sizeof(arr))
const ll mod=1e9+7;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
//-------------------------------------------------------------//
ll lcm(ll a,ll b)
{
return (max(a,b)/__gcd(a,b))*min(a,b);
}
void person_bool(bool x)
{
cout<<(x?"YES":"NO")<<endl;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#ifndef ONLINE_JUDGE
//freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
#endif
fast
int n,m;cin>>n>>m;
vector<vector<vector<int>>>dist(n+5,vector<vector<int>>(n+5,vector<int>(n+5,1e9)));
for(int i=0;i<m;i++){
int u,v,w;
cin>>u>>v>>w;
dist[u][v][1]=min(dist[u][v][1],w);
}
int k,q;cin>>k>>q;
for(int j=2;j<=min(k,n);j++){
for(int u=1;u<=n;u++){
for(int v=1;v<=n;v++){
dist[u][v][j]=dist[u][v][j-1];
for(int b=1;b<=n;b++){
dist[u][v][j]=min(dist[u][v][j],dist[u][b][j-1]+dist[b][v][1]);
dist[u][v][j]=min(dist[u][v][j],dist[u][b][1]+dist[b][v][j-1]);
}
}
}
}
while(q--){
int u,v;cin>>u>>v;
cout<<(u==v?0:dist[u][v][min(k,n)]==1e9?-1:dist[u][v][min(k,n)])<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2140 KB |
Output is correct |
2 |
Correct |
5 ms |
2396 KB |
Output is correct |
3 |
Correct |
10 ms |
2404 KB |
Output is correct |
4 |
Correct |
5 ms |
2396 KB |
Output is correct |
5 |
Correct |
8 ms |
2548 KB |
Output is correct |
6 |
Correct |
6 ms |
2392 KB |
Output is correct |
7 |
Correct |
9 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
73 ms |
2372 KB |
Output is correct |
8 |
Correct |
217 ms |
2364 KB |
Output is correct |
9 |
Correct |
11 ms |
2396 KB |
Output is correct |
10 |
Correct |
112 ms |
2392 KB |
Output is correct |
11 |
Correct |
30 ms |
2396 KB |
Output is correct |
12 |
Correct |
174 ms |
2432 KB |
Output is correct |
13 |
Correct |
86 ms |
8272 KB |
Output is correct |
14 |
Correct |
92 ms |
8492 KB |
Output is correct |
15 |
Correct |
251 ms |
8484 KB |
Output is correct |
16 |
Correct |
152 ms |
8736 KB |
Output is correct |
17 |
Correct |
87 ms |
8528 KB |
Output is correct |
18 |
Correct |
79 ms |
8528 KB |
Output is correct |
19 |
Correct |
288 ms |
8528 KB |
Output is correct |
20 |
Correct |
194 ms |
8368 KB |
Output is correct |
21 |
Correct |
80 ms |
8480 KB |
Output is correct |
22 |
Correct |
115 ms |
8608 KB |
Output is correct |
23 |
Correct |
116 ms |
8532 KB |
Output is correct |
24 |
Correct |
182 ms |
8608 KB |
Output is correct |
25 |
Correct |
85 ms |
8528 KB |
Output is correct |
26 |
Correct |
126 ms |
8612 KB |
Output is correct |
27 |
Correct |
148 ms |
8692 KB |
Output is correct |
28 |
Correct |
130 ms |
8532 KB |
Output is correct |
29 |
Correct |
89 ms |
8272 KB |
Output is correct |
30 |
Correct |
118 ms |
8236 KB |
Output is correct |
31 |
Correct |
189 ms |
8356 KB |
Output is correct |
32 |
Correct |
293 ms |
8432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
2 ms |
2140 KB |
Output is correct |
8 |
Correct |
5 ms |
2396 KB |
Output is correct |
9 |
Correct |
10 ms |
2404 KB |
Output is correct |
10 |
Correct |
5 ms |
2396 KB |
Output is correct |
11 |
Correct |
8 ms |
2548 KB |
Output is correct |
12 |
Correct |
6 ms |
2392 KB |
Output is correct |
13 |
Correct |
9 ms |
2392 KB |
Output is correct |
14 |
Correct |
73 ms |
2372 KB |
Output is correct |
15 |
Correct |
217 ms |
2364 KB |
Output is correct |
16 |
Correct |
11 ms |
2396 KB |
Output is correct |
17 |
Correct |
112 ms |
2392 KB |
Output is correct |
18 |
Correct |
30 ms |
2396 KB |
Output is correct |
19 |
Correct |
174 ms |
2432 KB |
Output is correct |
20 |
Correct |
86 ms |
8272 KB |
Output is correct |
21 |
Correct |
92 ms |
8492 KB |
Output is correct |
22 |
Correct |
251 ms |
8484 KB |
Output is correct |
23 |
Correct |
152 ms |
8736 KB |
Output is correct |
24 |
Correct |
87 ms |
8528 KB |
Output is correct |
25 |
Correct |
79 ms |
8528 KB |
Output is correct |
26 |
Correct |
288 ms |
8528 KB |
Output is correct |
27 |
Correct |
194 ms |
8368 KB |
Output is correct |
28 |
Correct |
80 ms |
8480 KB |
Output is correct |
29 |
Correct |
115 ms |
8608 KB |
Output is correct |
30 |
Correct |
116 ms |
8532 KB |
Output is correct |
31 |
Correct |
182 ms |
8608 KB |
Output is correct |
32 |
Correct |
85 ms |
8528 KB |
Output is correct |
33 |
Correct |
126 ms |
8612 KB |
Output is correct |
34 |
Correct |
148 ms |
8692 KB |
Output is correct |
35 |
Correct |
130 ms |
8532 KB |
Output is correct |
36 |
Correct |
89 ms |
8272 KB |
Output is correct |
37 |
Correct |
118 ms |
8236 KB |
Output is correct |
38 |
Correct |
189 ms |
8356 KB |
Output is correct |
39 |
Correct |
293 ms |
8432 KB |
Output is correct |
40 |
Correct |
54 ms |
2384 KB |
Output is correct |
41 |
Correct |
216 ms |
2372 KB |
Output is correct |
42 |
Correct |
32 ms |
2392 KB |
Output is correct |
43 |
Correct |
216 ms |
2396 KB |
Output is correct |
44 |
Correct |
33 ms |
2640 KB |
Output is correct |
45 |
Correct |
216 ms |
2396 KB |
Output is correct |
46 |
Correct |
88 ms |
8276 KB |
Output is correct |
47 |
Correct |
176 ms |
8484 KB |
Output is correct |
48 |
Correct |
122 ms |
8272 KB |
Output is correct |
49 |
Correct |
294 ms |
8496 KB |
Output is correct |
50 |
Correct |
107 ms |
8480 KB |
Output is correct |
51 |
Correct |
130 ms |
8532 KB |
Output is correct |
52 |
Correct |
81 ms |
8528 KB |
Output is correct |
53 |
Correct |
289 ms |
8564 KB |
Output is correct |
54 |
Correct |
93 ms |
8480 KB |
Output is correct |
55 |
Correct |
93 ms |
8480 KB |
Output is correct |
56 |
Correct |
274 ms |
8592 KB |
Output is correct |
57 |
Correct |
294 ms |
8640 KB |
Output is correct |
58 |
Correct |
83 ms |
8688 KB |
Output is correct |
59 |
Correct |
165 ms |
8480 KB |
Output is correct |
60 |
Correct |
246 ms |
8688 KB |
Output is correct |
61 |
Correct |
321 ms |
8480 KB |
Output is correct |
62 |
Correct |
82 ms |
8236 KB |
Output is correct |
63 |
Correct |
127 ms |
8356 KB |
Output is correct |
64 |
Correct |
167 ms |
8276 KB |
Output is correct |
65 |
Correct |
288 ms |
8424 KB |
Output is correct |