#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];
unsigned int pref[maxn][5][22],suff[maxn][5][22], inf = -1;
void dijkstra_pref(int beg, int i, int lvl)
{
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)
{
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});
}
memset(pref,-1,sizeof(pref));
memset(suff,-1,sizeof(suff));
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;
}
Compilation message
toll.cpp: In function 'void dijkstra_pref(int, int, int)':
toll.cpp:36:18: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
36 | if(e.dist<=pref[e.ver][i][lvl])
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp:40:34: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
40 | if(nb.dist+e.dist<pref[nb.ver][i][lvl])
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp: In function 'void dijkstra_suff(int, int, int)':
toll.cpp:60:18: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
60 | if(e.dist<=suff[e.ver][i][lvl])
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp:64:34: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
64 | if(nb.dist+e.dist<suff[nb.ver][i][lvl])
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3041 ms |
49752 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3015 ms |
50512 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
45656 KB |
Output is correct |
2 |
Incorrect |
6 ms |
45660 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
45656 KB |
Output is correct |
2 |
Incorrect |
6 ms |
45660 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3041 ms |
49752 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |