#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)
{
for(int x=0;x<n;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(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)
{
for(int x=0;x<n;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(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);
dijkstra_suff(beg,i,lvl);
}
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 |
Execution timed out |
3024 ms |
91984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3022 ms |
91944 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3024 ms |
91984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |