#include<bits/stdc++.h>
using namespace std;
struct points{
int64_t node,dist,kk;
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;cin>>n>>m;
vector<vector<pair<int64_t,int64_t>>>adj(n);
vector<vector<int64_t>>dist(n,vector<int64_t>(n,1e15));
//vector<vector<vector<pair<int64_t,int64_t>>>>arr(n,vector<vector<pair<int64_t,int64_t>>>(n));
for (int i = 0;i<m;++i){
int64_t x,y,z;cin>>x>>y>>z;
--x;
--y;
dist[x][y] = min(dist[x][y],z);
}
int k,q;cin>>k>>q;
k = min(k,n*n - n);
vector<vector<vector<int64_t>>>distt(n,vector<vector<int64_t>>(n,vector<int64_t>(k+1,1e15)));
for (int i = 0;i<n;++i){
for (int j =0;j<n;++j){
if (i!=j){
if (dist[i][j]!=1e15){
adj[i].push_back({j,dist[i][j]});
}
}
}
}
function<void(int)> bfs = [&](int u){
queue<points>q;
q.push({u,0,0});
while(!q.empty()){
auto v = q.front();
q.pop();
for (int i = 1;i<=k;++i){
distt[u][v.node][i] = min(distt[u][v.node][i-1],distt[u][v.node][i]);
}
for (auto x:adj[v.node]){
if (v.kk+1<=k&&distt[u][x.first][v.kk+1]>x.second + v.dist){
distt[u][x.first][v.kk+1] = x.second + v.dist;
if (v.kk+1<k)
q.push({x.first,x.second + v.dist,v.kk + 1});
}
}
}
};
for (int i = 0;i<n;++i){
bfs(i);
}
for (int i = 0;i<q;++i){
int x,y;cin>>x>>y;
--x;
--y;
if (x==y){
cout<<0<<'\n';
continue;
}
int64_t ans = 1e15;
for (int i = 0;i<=k;++i){
ans = min(distt[x][y][i],ans);
}
if (ans==1e15){
cout<<"-1"<<'\n';
}
else cout<<ans<<'\n';
}
return 0;}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
588 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
716 KB |
Output is correct |
4 |
Correct |
2 ms |
644 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
6 ms |
784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
4 ms |
1484 KB |
Output is correct |
8 |
Correct |
9 ms |
3192 KB |
Output is correct |
9 |
Correct |
4 ms |
716 KB |
Output is correct |
10 |
Correct |
6 ms |
1996 KB |
Output is correct |
11 |
Correct |
9 ms |
1020 KB |
Output is correct |
12 |
Correct |
15 ms |
2380 KB |
Output is correct |
13 |
Correct |
99 ms |
836 KB |
Output is correct |
14 |
Correct |
86 ms |
856 KB |
Output is correct |
15 |
Correct |
95 ms |
2912 KB |
Output is correct |
16 |
Correct |
90 ms |
1484 KB |
Output is correct |
17 |
Correct |
93 ms |
836 KB |
Output is correct |
18 |
Correct |
90 ms |
732 KB |
Output is correct |
19 |
Correct |
93 ms |
3284 KB |
Output is correct |
20 |
Correct |
90 ms |
2132 KB |
Output is correct |
21 |
Correct |
82 ms |
720 KB |
Output is correct |
22 |
Correct |
89 ms |
1240 KB |
Output is correct |
23 |
Correct |
96 ms |
1104 KB |
Output is correct |
24 |
Correct |
91 ms |
2012 KB |
Output is correct |
25 |
Correct |
90 ms |
852 KB |
Output is correct |
26 |
Correct |
92 ms |
1220 KB |
Output is correct |
27 |
Correct |
94 ms |
1504 KB |
Output is correct |
28 |
Correct |
93 ms |
1604 KB |
Output is correct |
29 |
Correct |
88 ms |
940 KB |
Output is correct |
30 |
Correct |
91 ms |
1240 KB |
Output is correct |
31 |
Correct |
95 ms |
2116 KB |
Output is correct |
32 |
Correct |
105 ms |
3276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
644 KB |
Output is correct |
11 |
Correct |
2 ms |
716 KB |
Output is correct |
12 |
Correct |
3 ms |
716 KB |
Output is correct |
13 |
Correct |
6 ms |
784 KB |
Output is correct |
14 |
Correct |
4 ms |
1484 KB |
Output is correct |
15 |
Correct |
9 ms |
3192 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
6 ms |
1996 KB |
Output is correct |
18 |
Correct |
9 ms |
1020 KB |
Output is correct |
19 |
Correct |
15 ms |
2380 KB |
Output is correct |
20 |
Correct |
99 ms |
836 KB |
Output is correct |
21 |
Correct |
86 ms |
856 KB |
Output is correct |
22 |
Correct |
95 ms |
2912 KB |
Output is correct |
23 |
Correct |
90 ms |
1484 KB |
Output is correct |
24 |
Correct |
93 ms |
836 KB |
Output is correct |
25 |
Correct |
90 ms |
732 KB |
Output is correct |
26 |
Correct |
93 ms |
3284 KB |
Output is correct |
27 |
Correct |
90 ms |
2132 KB |
Output is correct |
28 |
Correct |
82 ms |
720 KB |
Output is correct |
29 |
Correct |
89 ms |
1240 KB |
Output is correct |
30 |
Correct |
96 ms |
1104 KB |
Output is correct |
31 |
Correct |
91 ms |
2012 KB |
Output is correct |
32 |
Correct |
90 ms |
852 KB |
Output is correct |
33 |
Correct |
92 ms |
1220 KB |
Output is correct |
34 |
Correct |
94 ms |
1504 KB |
Output is correct |
35 |
Correct |
93 ms |
1604 KB |
Output is correct |
36 |
Correct |
88 ms |
940 KB |
Output is correct |
37 |
Correct |
91 ms |
1240 KB |
Output is correct |
38 |
Correct |
95 ms |
2116 KB |
Output is correct |
39 |
Correct |
105 ms |
3276 KB |
Output is correct |
40 |
Correct |
4 ms |
1208 KB |
Output is correct |
41 |
Correct |
415 ms |
188480 KB |
Output is correct |
42 |
Correct |
4 ms |
844 KB |
Output is correct |
43 |
Correct |
477 ms |
188480 KB |
Output is correct |
44 |
Correct |
8 ms |
972 KB |
Output is correct |
45 |
Correct |
721 ms |
188604 KB |
Output is correct |
46 |
Correct |
88 ms |
856 KB |
Output is correct |
47 |
Correct |
91 ms |
2004 KB |
Output is correct |
48 |
Correct |
91 ms |
1240 KB |
Output is correct |
49 |
Correct |
737 ms |
188376 KB |
Output is correct |
50 |
Correct |
86 ms |
836 KB |
Output is correct |
51 |
Correct |
89 ms |
1488 KB |
Output is correct |
52 |
Correct |
86 ms |
728 KB |
Output is correct |
53 |
Correct |
587 ms |
188480 KB |
Output is correct |
54 |
Correct |
95 ms |
948 KB |
Output is correct |
55 |
Correct |
88 ms |
940 KB |
Output is correct |
56 |
Correct |
94 ms |
3148 KB |
Output is correct |
57 |
Correct |
615 ms |
188508 KB |
Output is correct |
58 |
Correct |
87 ms |
732 KB |
Output is correct |
59 |
Correct |
95 ms |
1740 KB |
Output is correct |
60 |
Correct |
95 ms |
2784 KB |
Output is correct |
61 |
Correct |
659 ms |
188484 KB |
Output is correct |
62 |
Correct |
86 ms |
852 KB |
Output is correct |
63 |
Correct |
94 ms |
1368 KB |
Output is correct |
64 |
Correct |
92 ms |
1612 KB |
Output is correct |
65 |
Correct |
990 ms |
188496 KB |
Output is correct |