#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
struct edge
{
int ver,dist;
edge(){};
edge(int vi, int di)
{
ver = vi;
dist = di;
}
bool operator<(const edge&ed) const
{
return dist>ed.dist;
}
};
const int maxn = 50005;
int k,n,m,o;
vector<edge>g[maxn],v[maxn];
long long pref[maxn][5][22],suff[maxn][5][22];
unsigned int inf = -1;
void dijkstra_pref(int beg, int i, int lvl, int l, int r)
{
for(int x=l*k;x<min(n,(r+1)*k);x++) pref[x][i][lvl] = inf;
pref[beg][i][lvl] = 0;
priority_queue<edge>q;
q.push({beg,0});
while(!q.empty())
{
edge e = q.top();
q.pop();
if(e.dist<=pref[e.ver][i][lvl])
{
for(edge nb:v[e.ver])
{
if(l<=nb.ver/k&&nb.ver/k<=r&&nb.dist+e.dist<pref[nb.ver][i][lvl])
{
nb.dist += e.dist;
pref[nb.ver][i][lvl] = nb.dist;
q.push(nb);
}
}
}
}
}
void dijkstra_suff(int beg, int i, int lvl, int l, int r)
{
for(int x=l*k;x<min(n,(r+1)*k);x++) suff[x][i][lvl] = inf;
suff[beg][i][lvl] = 0;
priority_queue<edge>q;
q.push({beg,0});
while(!q.empty())
{
edge e = q.top();
q.pop();
if(e.dist<=suff[e.ver][i][lvl])
{
for(edge nb:g[e.ver])
{
if(l<=nb.ver/k&&nb.ver/k<=r&&nb.dist+e.dist<suff[nb.ver][i][lvl])
{
nb.dist += e.dist;
suff[nb.ver][i][lvl] = nb.dist;
q.push(nb);
}
}
}
}
}
void divide(int l, int r, int lvl)
{
if(l>=r) return;
int mid = (l+r)/2;
for(int i=0;i<k;i++)
{
int beg = mid*k + i;
dijkstra_pref(beg,i,lvl,l,r);
dijkstra_suff(beg,i,lvl,l,r);
}
divide(l,mid-1,lvl+1);
divide(mid+1,r,lvl+1);
}
int query(int l, int r, int lvl, int a, int b)
{
int mid = (l+r)/2;
if(a/k<=mid&&mid<=b/k)
{
long long ans = inf;
for(int i=0;i<k;i++)
{
if(pref[a][i][lvl]!=inf&&suff[b][i][lvl]!=inf)
ans = min(ans,(long long)pref[a][i][lvl]+suff[b][i][lvl]);
}
if(ans==inf) return -1;
return ans;
}
if(b/k<mid) return query(l,mid-1,lvl+1,a,b);
return query(mid+1,r,lvl+1,a,b);
}
void read()
{
cin >> k >> n >> m >> o;
int a,b,t;
for(int i=1;i<=m;i++)
{
cin >> a >> b >> t;
g[a].push_back({b,t});
v[b].push_back({a,t});
}
divide(0,n/k,0);
for(int i=1;i<=o;i++)
{
cin >> a >> b;
cout << query(0,n/k,0,a,b) << endl;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
read();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
92760 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
3 ms |
11032 KB |
Output is correct |
8 |
Correct |
51 ms |
92968 KB |
Output is correct |
9 |
Correct |
48 ms |
92756 KB |
Output is correct |
10 |
Correct |
23 ms |
88924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
93048 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
92760 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
3 ms |
11032 KB |
Output is correct |
8 |
Correct |
51 ms |
92968 KB |
Output is correct |
9 |
Correct |
48 ms |
92756 KB |
Output is correct |
10 |
Correct |
23 ms |
88924 KB |
Output is correct |
11 |
Correct |
122 ms |
93048 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |