#include <bits/stdc++.h>
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb(x) push_back(x)
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define Unique(v) sort(all(v));v.erase(unique(all(v)),v.end());
#define sz(v) (int)v.size()
//#define int long long
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
#define left sefude
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl;
typedef long long ll;
typedef pair<int,int> pii;
inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll a,ll b,ll m){
if(b==0LL) return 1LL;
if(b==1LL) return mod(a,m);
ll k = mod(exp(a,b/2,m),m);
if(b&1LL){
return mod(a*mod(k*k,m),m);
}
else return mod(k*k,m);
}
const int N = 50100;
const int L = 17;
vector<pii> vai[N][L];
vector<pii> g[N];
int k,n,m,q;
void preprocess(){
for(int i=0;i<n;i++){
for(auto it : g[i]){
vai[i][0].pb(it);
}
}
for(int j=1;j<L;j++){
for(int i=0;i<n;i++){
map<int,int> dist;
for(auto it : vai[i][j-1]){
for(auto ind : vai[it.ff][j-1]){
if(dist.count(ind.ff)){
dist[ind.ff] = min(dist[ind.ff],ind.ss + it.ss);
}else{
dist[ind.ff] = ind.ss + it.ss;
}
}
}
for(auto it : dist){
vai[i][j].pb(it);
}
}
}
}
const int inf = 1e9;
int32_t main(){
fastio;
cin >> k >> n >> m >> q;
for(int i=0;i<m;i++){
int a,b,t;
cin>>a>>b>>t;
g[a].pb(pii(b,t));
}
preprocess();
while(q--){
int a,b;
cin>>a>>b;
vector<pii> act;
act.pb(pii(a,0));
int l = a/k;
int need = b/k;
int dif = need - l;
for(int j=L-1;j>=0;j--){
if(dif&(1<<j)){
map<int,int>dist;
for(auto it : act){
for(auto go : vai[it.ff][j]){
if(dist.count(go.ff)){
dist[go.ff] = min(dist[go.ff],it.ss + go.ss);
}else dist[go.ff] = go.ss + it.ss;
}
}
act.clear();
for(auto it : dist){
act.pb(it);
}
}
}
int mn = inf;
for(auto it : act){
if(it.ff == b)mn = min(mn,it.ss);
}
if(mn != inf)cout << mn << endl;
else cout << -1 << endl;
}
// math -> gcd it all
// Did u check N=1? Did you switch N,M?
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
46156 KB |
Output is correct |
2 |
Correct |
15 ms |
21504 KB |
Output is correct |
3 |
Correct |
14 ms |
21504 KB |
Output is correct |
4 |
Correct |
14 ms |
21504 KB |
Output is correct |
5 |
Correct |
15 ms |
21632 KB |
Output is correct |
6 |
Correct |
15 ms |
21632 KB |
Output is correct |
7 |
Correct |
15 ms |
21760 KB |
Output is correct |
8 |
Correct |
106 ms |
38920 KB |
Output is correct |
9 |
Correct |
72 ms |
30200 KB |
Output is correct |
10 |
Correct |
29 ms |
21760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
235 ms |
43000 KB |
Output is correct |
2 |
Correct |
14 ms |
21504 KB |
Output is correct |
3 |
Correct |
14 ms |
21536 KB |
Output is correct |
4 |
Correct |
15 ms |
21632 KB |
Output is correct |
5 |
Correct |
14 ms |
21504 KB |
Output is correct |
6 |
Correct |
13 ms |
21504 KB |
Output is correct |
7 |
Correct |
19 ms |
21888 KB |
Output is correct |
8 |
Correct |
28 ms |
22008 KB |
Output is correct |
9 |
Correct |
105 ms |
38904 KB |
Output is correct |
10 |
Correct |
388 ms |
56568 KB |
Output is correct |
11 |
Correct |
265 ms |
46328 KB |
Output is correct |
12 |
Correct |
123 ms |
32760 KB |
Output is correct |
13 |
Correct |
397 ms |
54136 KB |
Output is correct |
14 |
Correct |
224 ms |
41976 KB |
Output is correct |
15 |
Correct |
319 ms |
46968 KB |
Output is correct |
16 |
Correct |
318 ms |
46968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21504 KB |
Output is correct |
2 |
Correct |
13 ms |
21504 KB |
Output is correct |
3 |
Correct |
14 ms |
21504 KB |
Output is correct |
4 |
Correct |
14 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 ms |
21504 KB |
Output is correct |
6 |
Correct |
15 ms |
21760 KB |
Output is correct |
7 |
Correct |
17 ms |
21760 KB |
Output is correct |
8 |
Correct |
21 ms |
22144 KB |
Output is correct |
9 |
Correct |
20 ms |
21888 KB |
Output is correct |
10 |
Correct |
102 ms |
39032 KB |
Output is correct |
11 |
Correct |
232 ms |
45952 KB |
Output is correct |
12 |
Correct |
357 ms |
56312 KB |
Output is correct |
13 |
Correct |
365 ms |
57208 KB |
Output is correct |
14 |
Correct |
332 ms |
54264 KB |
Output is correct |
15 |
Correct |
300 ms |
46968 KB |
Output is correct |
16 |
Correct |
301 ms |
46968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21504 KB |
Output is correct |
2 |
Correct |
13 ms |
21504 KB |
Output is correct |
3 |
Correct |
14 ms |
21504 KB |
Output is correct |
4 |
Correct |
14 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 ms |
21504 KB |
Output is correct |
6 |
Correct |
15 ms |
21760 KB |
Output is correct |
7 |
Correct |
17 ms |
21760 KB |
Output is correct |
8 |
Correct |
21 ms |
22144 KB |
Output is correct |
9 |
Correct |
20 ms |
21888 KB |
Output is correct |
10 |
Correct |
102 ms |
39032 KB |
Output is correct |
11 |
Correct |
232 ms |
45952 KB |
Output is correct |
12 |
Correct |
357 ms |
56312 KB |
Output is correct |
13 |
Correct |
365 ms |
57208 KB |
Output is correct |
14 |
Correct |
332 ms |
54264 KB |
Output is correct |
15 |
Correct |
300 ms |
46968 KB |
Output is correct |
16 |
Correct |
301 ms |
46968 KB |
Output is correct |
17 |
Correct |
247 ms |
46136 KB |
Output is correct |
18 |
Correct |
15 ms |
21504 KB |
Output is correct |
19 |
Correct |
13 ms |
21504 KB |
Output is correct |
20 |
Correct |
14 ms |
21504 KB |
Output is correct |
21 |
Correct |
13 ms |
21504 KB |
Output is correct |
22 |
Correct |
14 ms |
21504 KB |
Output is correct |
23 |
Correct |
16 ms |
21760 KB |
Output is correct |
24 |
Correct |
20 ms |
21888 KB |
Output is correct |
25 |
Correct |
29 ms |
22144 KB |
Output is correct |
26 |
Correct |
25 ms |
22016 KB |
Output is correct |
27 |
Correct |
100 ms |
38776 KB |
Output is correct |
28 |
Correct |
370 ms |
56432 KB |
Output is correct |
29 |
Correct |
378 ms |
57464 KB |
Output is correct |
30 |
Correct |
324 ms |
52088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
46156 KB |
Output is correct |
2 |
Correct |
15 ms |
21504 KB |
Output is correct |
3 |
Correct |
14 ms |
21504 KB |
Output is correct |
4 |
Correct |
14 ms |
21504 KB |
Output is correct |
5 |
Correct |
15 ms |
21632 KB |
Output is correct |
6 |
Correct |
15 ms |
21632 KB |
Output is correct |
7 |
Correct |
15 ms |
21760 KB |
Output is correct |
8 |
Correct |
106 ms |
38920 KB |
Output is correct |
9 |
Correct |
72 ms |
30200 KB |
Output is correct |
10 |
Correct |
29 ms |
21760 KB |
Output is correct |
11 |
Correct |
235 ms |
43000 KB |
Output is correct |
12 |
Correct |
14 ms |
21504 KB |
Output is correct |
13 |
Correct |
14 ms |
21536 KB |
Output is correct |
14 |
Correct |
15 ms |
21632 KB |
Output is correct |
15 |
Correct |
14 ms |
21504 KB |
Output is correct |
16 |
Correct |
13 ms |
21504 KB |
Output is correct |
17 |
Correct |
19 ms |
21888 KB |
Output is correct |
18 |
Correct |
28 ms |
22008 KB |
Output is correct |
19 |
Correct |
105 ms |
38904 KB |
Output is correct |
20 |
Correct |
388 ms |
56568 KB |
Output is correct |
21 |
Correct |
265 ms |
46328 KB |
Output is correct |
22 |
Correct |
123 ms |
32760 KB |
Output is correct |
23 |
Correct |
397 ms |
54136 KB |
Output is correct |
24 |
Correct |
224 ms |
41976 KB |
Output is correct |
25 |
Correct |
319 ms |
46968 KB |
Output is correct |
26 |
Correct |
318 ms |
46968 KB |
Output is correct |
27 |
Correct |
13 ms |
21504 KB |
Output is correct |
28 |
Correct |
13 ms |
21504 KB |
Output is correct |
29 |
Correct |
14 ms |
21504 KB |
Output is correct |
30 |
Correct |
14 ms |
21504 KB |
Output is correct |
31 |
Correct |
14 ms |
21504 KB |
Output is correct |
32 |
Correct |
15 ms |
21760 KB |
Output is correct |
33 |
Correct |
17 ms |
21760 KB |
Output is correct |
34 |
Correct |
21 ms |
22144 KB |
Output is correct |
35 |
Correct |
20 ms |
21888 KB |
Output is correct |
36 |
Correct |
102 ms |
39032 KB |
Output is correct |
37 |
Correct |
232 ms |
45952 KB |
Output is correct |
38 |
Correct |
357 ms |
56312 KB |
Output is correct |
39 |
Correct |
365 ms |
57208 KB |
Output is correct |
40 |
Correct |
332 ms |
54264 KB |
Output is correct |
41 |
Correct |
300 ms |
46968 KB |
Output is correct |
42 |
Correct |
301 ms |
46968 KB |
Output is correct |
43 |
Correct |
247 ms |
46136 KB |
Output is correct |
44 |
Correct |
15 ms |
21504 KB |
Output is correct |
45 |
Correct |
13 ms |
21504 KB |
Output is correct |
46 |
Correct |
14 ms |
21504 KB |
Output is correct |
47 |
Correct |
13 ms |
21504 KB |
Output is correct |
48 |
Correct |
14 ms |
21504 KB |
Output is correct |
49 |
Correct |
16 ms |
21760 KB |
Output is correct |
50 |
Correct |
20 ms |
21888 KB |
Output is correct |
51 |
Correct |
29 ms |
22144 KB |
Output is correct |
52 |
Correct |
25 ms |
22016 KB |
Output is correct |
53 |
Correct |
100 ms |
38776 KB |
Output is correct |
54 |
Correct |
370 ms |
56432 KB |
Output is correct |
55 |
Correct |
378 ms |
57464 KB |
Output is correct |
56 |
Correct |
324 ms |
52088 KB |
Output is correct |
57 |
Correct |
540 ms |
57464 KB |
Output is correct |
58 |
Correct |
104 ms |
38904 KB |
Output is correct |
59 |
Correct |
268 ms |
46328 KB |
Output is correct |