// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
const int inf=1e18;
const int mod=998244353;
const int mod2=1e9+7;
const int maxn=105;
const int maxq=1000005;
const int maxl=20;
const int maxa=1000005;
int power(int a,int n){
int res=1;
while(n){
if(n&1) res=res*a%mod;
a=a*a%mod;n>>=1;
}
return res;
}
int n,m,k,q,dist[2][maxn][maxn],len[maxn][maxn],ans[maxq];
void solve(){
cin >> n >> m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) len[i][j]=dist[0][i][j]=(i==j?0:inf);
}
for(int i=1;i<=m;i++){
int u,v,w;cin >> u >> v >> w;
len[u][v]=min(len[u][v],w);
}
cin >> k >> q;k=min(k,n-1);
int t=0;
for(int i=1;i<=k;i++){
t^=1;
for(int u=1;u<=n;u++){
for(int v=1;v<=n;v++) dist[t][u][v]=dist[t^1][u][v];
}
for(int u=1;u<=n;u++){
for(int v=1;v<=n;v++){
if(dist[t^1][u][v]==inf) continue;
for(int p=1;p<=n;p++) dist[t][u][p]=min(dist[t][u][p],dist[t^1][u][v]+len[v][p]);
}
}
}
for(int i=1;i<=q;i++){
int u,v;cin >> u >> v;
cout << (dist[t][u][v]==inf?-1:dist[t][u][v]) << '\n';
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
10 ms |
468 KB |
Output is correct |
8 |
Correct |
24 ms |
540 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
14 ms |
556 KB |
Output is correct |
11 |
Correct |
6 ms |
600 KB |
Output is correct |
12 |
Correct |
17 ms |
596 KB |
Output is correct |
13 |
Correct |
85 ms |
824 KB |
Output is correct |
14 |
Correct |
110 ms |
832 KB |
Output is correct |
15 |
Correct |
103 ms |
852 KB |
Output is correct |
16 |
Correct |
96 ms |
688 KB |
Output is correct |
17 |
Correct |
83 ms |
720 KB |
Output is correct |
18 |
Correct |
119 ms |
712 KB |
Output is correct |
19 |
Correct |
105 ms |
828 KB |
Output is correct |
20 |
Correct |
102 ms |
812 KB |
Output is correct |
21 |
Correct |
94 ms |
860 KB |
Output is correct |
22 |
Correct |
100 ms |
812 KB |
Output is correct |
23 |
Correct |
95 ms |
824 KB |
Output is correct |
24 |
Correct |
99 ms |
800 KB |
Output is correct |
25 |
Correct |
121 ms |
952 KB |
Output is correct |
26 |
Correct |
105 ms |
840 KB |
Output is correct |
27 |
Correct |
95 ms |
944 KB |
Output is correct |
28 |
Correct |
118 ms |
836 KB |
Output is correct |
29 |
Correct |
82 ms |
704 KB |
Output is correct |
30 |
Correct |
85 ms |
716 KB |
Output is correct |
31 |
Correct |
107 ms |
764 KB |
Output is correct |
32 |
Correct |
113 ms |
684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
10 ms |
468 KB |
Output is correct |
15 |
Correct |
24 ms |
540 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
14 ms |
556 KB |
Output is correct |
18 |
Correct |
6 ms |
600 KB |
Output is correct |
19 |
Correct |
17 ms |
596 KB |
Output is correct |
20 |
Correct |
85 ms |
824 KB |
Output is correct |
21 |
Correct |
110 ms |
832 KB |
Output is correct |
22 |
Correct |
103 ms |
852 KB |
Output is correct |
23 |
Correct |
96 ms |
688 KB |
Output is correct |
24 |
Correct |
83 ms |
720 KB |
Output is correct |
25 |
Correct |
119 ms |
712 KB |
Output is correct |
26 |
Correct |
105 ms |
828 KB |
Output is correct |
27 |
Correct |
102 ms |
812 KB |
Output is correct |
28 |
Correct |
94 ms |
860 KB |
Output is correct |
29 |
Correct |
100 ms |
812 KB |
Output is correct |
30 |
Correct |
95 ms |
824 KB |
Output is correct |
31 |
Correct |
99 ms |
800 KB |
Output is correct |
32 |
Correct |
121 ms |
952 KB |
Output is correct |
33 |
Correct |
105 ms |
840 KB |
Output is correct |
34 |
Correct |
95 ms |
944 KB |
Output is correct |
35 |
Correct |
118 ms |
836 KB |
Output is correct |
36 |
Correct |
82 ms |
704 KB |
Output is correct |
37 |
Correct |
85 ms |
716 KB |
Output is correct |
38 |
Correct |
107 ms |
764 KB |
Output is correct |
39 |
Correct |
113 ms |
684 KB |
Output is correct |
40 |
Correct |
7 ms |
468 KB |
Output is correct |
41 |
Correct |
24 ms |
468 KB |
Output is correct |
42 |
Correct |
4 ms |
468 KB |
Output is correct |
43 |
Correct |
26 ms |
548 KB |
Output is correct |
44 |
Correct |
6 ms |
596 KB |
Output is correct |
45 |
Correct |
25 ms |
572 KB |
Output is correct |
46 |
Correct |
82 ms |
700 KB |
Output is correct |
47 |
Correct |
102 ms |
684 KB |
Output is correct |
48 |
Correct |
91 ms |
696 KB |
Output is correct |
49 |
Correct |
123 ms |
844 KB |
Output is correct |
50 |
Correct |
89 ms |
912 KB |
Output is correct |
51 |
Correct |
92 ms |
1248 KB |
Output is correct |
52 |
Correct |
92 ms |
1076 KB |
Output is correct |
53 |
Correct |
111 ms |
956 KB |
Output is correct |
54 |
Correct |
101 ms |
940 KB |
Output is correct |
55 |
Correct |
91 ms |
972 KB |
Output is correct |
56 |
Correct |
108 ms |
1068 KB |
Output is correct |
57 |
Correct |
113 ms |
948 KB |
Output is correct |
58 |
Correct |
87 ms |
948 KB |
Output is correct |
59 |
Correct |
94 ms |
1076 KB |
Output is correct |
60 |
Correct |
106 ms |
1072 KB |
Output is correct |
61 |
Correct |
116 ms |
932 KB |
Output is correct |
62 |
Correct |
80 ms |
972 KB |
Output is correct |
63 |
Correct |
89 ms |
1044 KB |
Output is correct |
64 |
Correct |
92 ms |
956 KB |
Output is correct |
65 |
Correct |
105 ms |
972 KB |
Output is correct |