This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
unsigned int inf = -1;
void dijkstra_pref(int beg, int i, int lvl, int l, int r)
{
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)
{
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});
}
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 (stderr)
toll.cpp: In function 'void dijkstra_pref(int, int, int, int, int)':
toll.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
37 | if(e.dist<=pref[e.ver][i][lvl])
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp:41:60: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
41 | if(l<=nb.ver/k&&nb.ver/k<=r&&nb.dist+e.dist<pref[nb.ver][i][lvl])
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp: In function 'void dijkstra_suff(int, int, int, int, int)':
toll.cpp:61:18: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
61 | if(e.dist<=suff[e.ver][i][lvl])
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp:65:60: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
65 | if(l<=nb.ver/k&&nb.ver/k<=r&&nb.dist+e.dist<suff[nb.ver][i][lvl])
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |