#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int maxn = 50006;
const int maxq = 10006;
int i,j,p,q,n,m,k,raz[50006],K,Q,ans[maxq];
struct Put
{
int v,d;
};
vector <Put> v[50006],obr[50006];
bool operator<(Put a,Put b)
{
return a.d>b.d;
}
priority_queue <Put> pq;
pair <int,int> quer[maxq];
void dijkstra(int u,int l,int r)
{
// cout<<"dijkstra ot "<<u<<endl;
pq.push({u,0});
while(!pq.empty())
{
auto t = pq.top();
pq.pop();
if(raz[t.v]==-1)
{
raz[t.v] = t.d;
// cout<<t.v<<" "<<t.d<<endl;
for(auto i:v[t.v])
{
if(raz[i.v]==-1 && i.v/K<=r)
pq.push({i.v,i.d+t.d});
}
}
}
pq.push({u,0});
raz[u]=-1;
while(!pq.empty())
{
auto t = pq.top();
pq.pop();
if(raz[t.v]==-1)
{
raz[t.v] = t.d;
// cout<<t.v<<" "<<t.d<<endl;
for(auto i:obr[t.v])
{
if(raz[i.v]==-1 && i.v/K>=l)
pq.push({i.v,i.d+t.d});
}
}
}
}
void solve(int l,int r,vector <int> zaqvki)
{
if(l>=r)return ;
int mid = (l+r)/2;
vector <int> todo[2];
for(int j=mid*K;j<(mid+1)*K;j++)
{
fill(raz,raz+n+1,-1);
dijkstra(j,l,r);
for(auto t:zaqvki)
{
p = quer[t].first;
q = quer[t].second;
if(p/K<=mid && q/K>=mid)
{
// cout<<p<<" zaqvka "<<q<<" "<<raz[p]<<" "<<raz[q]<<endl;
if(raz[p]!=-1 && raz[q]!=-1)
ans[t] = min(ans[t],raz[p]+raz[q]);
continue;
}
todo[p/K>mid].push_back(t);
}
}
if(!todo[0].empty())solve(l,mid-1,todo[0]);
if(!todo[1].empty())solve(mid+1,r,todo[1]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>K>>n>>m>>Q;
for(i=1;i<=m;i++)
{
cin>>p>>q>>k;
v[p].push_back({q,k});
obr[q].push_back({p,k});
}
for(i=1;i<=Q;i++)
{
cin>>quer[i].first>>quer[i].second;
ans[i] = INT_MAX;
}
vector <int> tt;
for(i=1;i<=Q;i++)
tt.push_back(i);
solve(0,n/K,tt);
for(i=1;i<=Q;i++)
if(ans[i]==INT_MAX)cout<<-1<<endl;
else cout<<ans[i]<<endl;
return 0;
}
/**
5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13
3 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 3 4
3 2
0 12
0 5
0 7
7 12
0 13
1 6 5 4
1 2 3
2 3 5
3 4 1
4 5 4
5 6 2
1 4
2 6
3 5
4 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
6228 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2740 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
33 ms |
7124 KB |
Output is correct |
9 |
Correct |
23 ms |
6992 KB |
Output is correct |
10 |
Correct |
6 ms |
3156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
7224 KB |
Output is correct |
2 |
Correct |
2 ms |
2676 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
5 ms |
3028 KB |
Output is correct |
8 |
Correct |
6 ms |
3456 KB |
Output is correct |
9 |
Correct |
23 ms |
7172 KB |
Output is correct |
10 |
Correct |
193 ms |
40912 KB |
Output is correct |
11 |
Correct |
64 ms |
8700 KB |
Output is correct |
12 |
Correct |
144 ms |
30732 KB |
Output is correct |
13 |
Correct |
1672 ms |
233700 KB |
Output is correct |
14 |
Correct |
81 ms |
12108 KB |
Output is correct |
15 |
Correct |
492 ms |
70416 KB |
Output is correct |
16 |
Correct |
1354 ms |
191644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2684 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2692 KB |
Output is correct |
7 |
Correct |
3 ms |
2696 KB |
Output is correct |
8 |
Correct |
7 ms |
2968 KB |
Output is correct |
9 |
Correct |
5 ms |
2856 KB |
Output is correct |
10 |
Correct |
23 ms |
6796 KB |
Output is correct |
11 |
Correct |
77 ms |
7516 KB |
Output is correct |
12 |
Correct |
135 ms |
10060 KB |
Output is correct |
13 |
Correct |
153 ms |
10604 KB |
Output is correct |
14 |
Correct |
112 ms |
8860 KB |
Output is correct |
15 |
Correct |
100 ms |
6732 KB |
Output is correct |
16 |
Correct |
107 ms |
7560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2684 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2692 KB |
Output is correct |
7 |
Correct |
3 ms |
2696 KB |
Output is correct |
8 |
Correct |
7 ms |
2968 KB |
Output is correct |
9 |
Correct |
5 ms |
2856 KB |
Output is correct |
10 |
Correct |
23 ms |
6796 KB |
Output is correct |
11 |
Correct |
77 ms |
7516 KB |
Output is correct |
12 |
Correct |
135 ms |
10060 KB |
Output is correct |
13 |
Correct |
153 ms |
10604 KB |
Output is correct |
14 |
Correct |
112 ms |
8860 KB |
Output is correct |
15 |
Correct |
100 ms |
6732 KB |
Output is correct |
16 |
Correct |
107 ms |
7560 KB |
Output is correct |
17 |
Correct |
117 ms |
7696 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2684 KB |
Output is correct |
23 |
Correct |
3 ms |
2772 KB |
Output is correct |
24 |
Correct |
6 ms |
2900 KB |
Output is correct |
25 |
Correct |
52 ms |
4560 KB |
Output is correct |
26 |
Correct |
18 ms |
3396 KB |
Output is correct |
27 |
Correct |
32 ms |
6868 KB |
Output is correct |
28 |
Correct |
224 ms |
12864 KB |
Output is correct |
29 |
Correct |
262 ms |
13352 KB |
Output is correct |
30 |
Correct |
146 ms |
9444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
6228 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2740 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
33 ms |
7124 KB |
Output is correct |
9 |
Correct |
23 ms |
6992 KB |
Output is correct |
10 |
Correct |
6 ms |
3156 KB |
Output is correct |
11 |
Correct |
66 ms |
7224 KB |
Output is correct |
12 |
Correct |
2 ms |
2676 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
5 ms |
3028 KB |
Output is correct |
18 |
Correct |
6 ms |
3456 KB |
Output is correct |
19 |
Correct |
23 ms |
7172 KB |
Output is correct |
20 |
Correct |
193 ms |
40912 KB |
Output is correct |
21 |
Correct |
64 ms |
8700 KB |
Output is correct |
22 |
Correct |
144 ms |
30732 KB |
Output is correct |
23 |
Correct |
1672 ms |
233700 KB |
Output is correct |
24 |
Correct |
81 ms |
12108 KB |
Output is correct |
25 |
Correct |
492 ms |
70416 KB |
Output is correct |
26 |
Correct |
1354 ms |
191644 KB |
Output is correct |
27 |
Correct |
1 ms |
2644 KB |
Output is correct |
28 |
Correct |
1 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2684 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2692 KB |
Output is correct |
33 |
Correct |
3 ms |
2696 KB |
Output is correct |
34 |
Correct |
7 ms |
2968 KB |
Output is correct |
35 |
Correct |
5 ms |
2856 KB |
Output is correct |
36 |
Correct |
23 ms |
6796 KB |
Output is correct |
37 |
Correct |
77 ms |
7516 KB |
Output is correct |
38 |
Correct |
135 ms |
10060 KB |
Output is correct |
39 |
Correct |
153 ms |
10604 KB |
Output is correct |
40 |
Correct |
112 ms |
8860 KB |
Output is correct |
41 |
Correct |
100 ms |
6732 KB |
Output is correct |
42 |
Correct |
107 ms |
7560 KB |
Output is correct |
43 |
Correct |
117 ms |
7696 KB |
Output is correct |
44 |
Correct |
2 ms |
2644 KB |
Output is correct |
45 |
Correct |
2 ms |
2644 KB |
Output is correct |
46 |
Correct |
2 ms |
2644 KB |
Output is correct |
47 |
Correct |
2 ms |
2644 KB |
Output is correct |
48 |
Correct |
2 ms |
2684 KB |
Output is correct |
49 |
Correct |
3 ms |
2772 KB |
Output is correct |
50 |
Correct |
6 ms |
2900 KB |
Output is correct |
51 |
Correct |
52 ms |
4560 KB |
Output is correct |
52 |
Correct |
18 ms |
3396 KB |
Output is correct |
53 |
Correct |
32 ms |
6868 KB |
Output is correct |
54 |
Correct |
224 ms |
12864 KB |
Output is correct |
55 |
Correct |
262 ms |
13352 KB |
Output is correct |
56 |
Correct |
146 ms |
9444 KB |
Output is correct |
57 |
Correct |
1651 ms |
64688 KB |
Output is correct |
58 |
Correct |
32 ms |
7164 KB |
Output is correct |
59 |
Correct |
104 ms |
8280 KB |
Output is correct |