#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ll,ii>
#define endl '\n'
const ll inf=1e15;
const ll mod=1e9+7;
const ll maxn=71;
ll adj[maxn][maxn];
ll dis[maxn][maxn][maxn];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i=0;i<maxn;i++){
for (int j=0;j<maxn;j++){
adj[i][j]=inf;
}
adj[i][i]=0;
}
ll n,e,a,b,w;
cin>>n>>e;
for (int i=0;i<e;i++){
cin>>a>>b>>w;
adj[a][b]=min(adj[a][b],w);
}
ll k,y,u,x;
cin>>k>>y;
k=min(k,69LL);
queue<ii> q;
memset(dis,-1,sizeof(dis));
for (int a=1;a<=n;a++){
q.emplace(a,0);
dis[a][a][0]=0;
while (q.size()){
u=q.front().first;
x=q.front().second;
q.pop();
if (x>k) continue;
for (int i=1;i<=n;i++){
if (dis[a][i][x+1]!=-1){
dis[a][i][x+1]=min(dis[a][i][x+1],dis[a][u][x]+adj[u][i]);
} else{
dis[a][i][x+1]=dis[a][u][x]+adj[u][i];
q.emplace(i,x+1);
} //n
}
}//n2
}
for (int i=0;i<y;i++){
ll ans=inf;
cin>>a>>b;
for (int i=0;i<=k;i++){
if (dis[a][b][i]!=-1) ans=min(ans,dis[a][b][i]);
}
cout<<(ans==inf?-1:ans)<<endl;
}//n2
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3148 KB |
Output is correct |
2 |
Correct |
1 ms |
3148 KB |
Output is correct |
3 |
Correct |
1 ms |
3148 KB |
Output is correct |
4 |
Correct |
2 ms |
3148 KB |
Output is correct |
5 |
Correct |
2 ms |
3148 KB |
Output is correct |
6 |
Correct |
2 ms |
3148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3148 KB |
Output is correct |
2 |
Correct |
3 ms |
3184 KB |
Output is correct |
3 |
Correct |
5 ms |
3192 KB |
Output is correct |
4 |
Correct |
4 ms |
3148 KB |
Output is correct |
5 |
Correct |
4 ms |
3148 KB |
Output is correct |
6 |
Correct |
5 ms |
3148 KB |
Output is correct |
7 |
Correct |
7 ms |
3156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3148 KB |
Output is correct |
2 |
Correct |
1 ms |
3148 KB |
Output is correct |
3 |
Correct |
1 ms |
3148 KB |
Output is correct |
4 |
Correct |
2 ms |
3148 KB |
Output is correct |
5 |
Correct |
2 ms |
3148 KB |
Output is correct |
6 |
Correct |
2 ms |
3148 KB |
Output is correct |
7 |
Correct |
18 ms |
3160 KB |
Output is correct |
8 |
Correct |
37 ms |
3148 KB |
Output is correct |
9 |
Correct |
4 ms |
3148 KB |
Output is correct |
10 |
Correct |
19 ms |
3164 KB |
Output is correct |
11 |
Correct |
8 ms |
3152 KB |
Output is correct |
12 |
Correct |
22 ms |
3168 KB |
Output is correct |
13 |
Correct |
83 ms |
3140 KB |
Output is correct |
14 |
Correct |
91 ms |
3152 KB |
Output is correct |
15 |
Correct |
118 ms |
3140 KB |
Output is correct |
16 |
Correct |
91 ms |
3024 KB |
Output is correct |
17 |
Correct |
95 ms |
3028 KB |
Output is correct |
18 |
Correct |
102 ms |
3028 KB |
Output is correct |
19 |
Correct |
117 ms |
3024 KB |
Output is correct |
20 |
Correct |
98 ms |
3036 KB |
Output is correct |
21 |
Correct |
89 ms |
3152 KB |
Output is correct |
22 |
Correct |
96 ms |
3152 KB |
Output is correct |
23 |
Correct |
94 ms |
3152 KB |
Output is correct |
24 |
Correct |
97 ms |
3164 KB |
Output is correct |
25 |
Correct |
87 ms |
3028 KB |
Output is correct |
26 |
Correct |
91 ms |
3028 KB |
Output is correct |
27 |
Correct |
96 ms |
3028 KB |
Output is correct |
28 |
Correct |
93 ms |
3028 KB |
Output is correct |
29 |
Correct |
88 ms |
3140 KB |
Output is correct |
30 |
Correct |
91 ms |
3156 KB |
Output is correct |
31 |
Correct |
95 ms |
3152 KB |
Output is correct |
32 |
Correct |
108 ms |
3156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3148 KB |
Output is correct |
2 |
Correct |
1 ms |
3148 KB |
Output is correct |
3 |
Correct |
1 ms |
3148 KB |
Output is correct |
4 |
Correct |
2 ms |
3148 KB |
Output is correct |
5 |
Correct |
2 ms |
3148 KB |
Output is correct |
6 |
Correct |
2 ms |
3148 KB |
Output is correct |
7 |
Correct |
3 ms |
3148 KB |
Output is correct |
8 |
Correct |
3 ms |
3184 KB |
Output is correct |
9 |
Correct |
5 ms |
3192 KB |
Output is correct |
10 |
Correct |
4 ms |
3148 KB |
Output is correct |
11 |
Correct |
4 ms |
3148 KB |
Output is correct |
12 |
Correct |
5 ms |
3148 KB |
Output is correct |
13 |
Correct |
7 ms |
3156 KB |
Output is correct |
14 |
Correct |
18 ms |
3160 KB |
Output is correct |
15 |
Correct |
37 ms |
3148 KB |
Output is correct |
16 |
Correct |
4 ms |
3148 KB |
Output is correct |
17 |
Correct |
19 ms |
3164 KB |
Output is correct |
18 |
Correct |
8 ms |
3152 KB |
Output is correct |
19 |
Correct |
22 ms |
3168 KB |
Output is correct |
20 |
Correct |
83 ms |
3140 KB |
Output is correct |
21 |
Correct |
91 ms |
3152 KB |
Output is correct |
22 |
Correct |
118 ms |
3140 KB |
Output is correct |
23 |
Correct |
91 ms |
3024 KB |
Output is correct |
24 |
Correct |
95 ms |
3028 KB |
Output is correct |
25 |
Correct |
102 ms |
3028 KB |
Output is correct |
26 |
Correct |
117 ms |
3024 KB |
Output is correct |
27 |
Correct |
98 ms |
3036 KB |
Output is correct |
28 |
Correct |
89 ms |
3152 KB |
Output is correct |
29 |
Correct |
96 ms |
3152 KB |
Output is correct |
30 |
Correct |
94 ms |
3152 KB |
Output is correct |
31 |
Correct |
97 ms |
3164 KB |
Output is correct |
32 |
Correct |
87 ms |
3028 KB |
Output is correct |
33 |
Correct |
91 ms |
3028 KB |
Output is correct |
34 |
Correct |
96 ms |
3028 KB |
Output is correct |
35 |
Correct |
93 ms |
3028 KB |
Output is correct |
36 |
Correct |
88 ms |
3140 KB |
Output is correct |
37 |
Correct |
91 ms |
3156 KB |
Output is correct |
38 |
Correct |
95 ms |
3152 KB |
Output is correct |
39 |
Correct |
108 ms |
3156 KB |
Output is correct |
40 |
Correct |
10 ms |
3148 KB |
Output is correct |
41 |
Correct |
32 ms |
3152 KB |
Output is correct |
42 |
Correct |
6 ms |
3156 KB |
Output is correct |
43 |
Correct |
34 ms |
3148 KB |
Output is correct |
44 |
Correct |
8 ms |
3152 KB |
Output is correct |
45 |
Correct |
32 ms |
3148 KB |
Output is correct |
46 |
Correct |
86 ms |
3156 KB |
Output is correct |
47 |
Correct |
100 ms |
3044 KB |
Output is correct |
48 |
Correct |
86 ms |
3044 KB |
Output is correct |
49 |
Correct |
109 ms |
3048 KB |
Output is correct |
50 |
Correct |
86 ms |
3156 KB |
Output is correct |
51 |
Correct |
92 ms |
3024 KB |
Output is correct |
52 |
Correct |
82 ms |
3032 KB |
Output is correct |
53 |
Correct |
116 ms |
3140 KB |
Output is correct |
54 |
Correct |
84 ms |
3156 KB |
Output is correct |
55 |
Correct |
114 ms |
3268 KB |
Output is correct |
56 |
Correct |
110 ms |
3160 KB |
Output is correct |
57 |
Correct |
111 ms |
3168 KB |
Output is correct |
58 |
Correct |
85 ms |
3024 KB |
Output is correct |
59 |
Correct |
95 ms |
3040 KB |
Output is correct |
60 |
Correct |
125 ms |
3136 KB |
Output is correct |
61 |
Correct |
144 ms |
3040 KB |
Output is correct |
62 |
Correct |
117 ms |
3164 KB |
Output is correct |
63 |
Correct |
90 ms |
3152 KB |
Output is correct |
64 |
Correct |
96 ms |
3136 KB |
Output is correct |
65 |
Correct |
117 ms |
3164 KB |
Output is correct |