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>
using namespace std;
vector<pair<int, int> > adj[50005], rev[50005];
map<pair<int, int>, int> mp;
int dist[50005];
void recur(int l, int r, int c)
{
int mid=(l+r)/2;
for (int i=c*mid; i<c*(mid+1); i++)
{
for (int j=c*l; j<c*(r+1); j++)
dist[j]=1e9;
dist[i]=0;
for (int j=c*mid-1; j>=c*l; j--)
{
for (int k=0; k<adj[j].size(); k++)
dist[j]=min(dist[j], dist[adj[j][k].first]+adj[j][k].second);
mp[{j, i}]=dist[j];
}
for (int j=c*(mid+1); j<c*(r+1); j++)
{
for (int k=0; k<rev[j].size(); k++)
dist[j]=min(dist[j], dist[rev[j][k].first]+rev[j][k].second);
mp[{i, j}]=dist[j];
}
}
if (l+2<=mid)
recur(l, mid-1, c);
if (mid+2<=r)
recur(mid+1, r, c);
}
int dfs(int l, int r, int u, int v)
{
int mid=(l+r)/2;
if (v<mid)
return dfs(l, mid-1, u, v);
if (u>mid)
return dfs(mid+1, r, u, v);
return mid;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int c, n, m, q;
cin >> c >> n >> m >> q;
for (int i=0; i<m; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
rev[v].push_back({u, w});
}
recur(0, (n-1)/c, c);
for (int i=0; i<q; i++)
{
int u, v;
cin >> u >> v;
int x=dfs(0, (n-1)/c, u/c, v/c);
if (u/c==x && v/c==x)
cout << -1 << '\n';
else if (u/c==x || v/c==x)
cout << mp[{u, v}] << '\n';
else
{
int ans=1e9;
for (int j=c*x; j<c*(x+1); j++)
ans=min(ans, mp[{u, j}]+mp[{j, v}]);
cout << (ans==1e9?-1:ans) << '\n';
}
}
}
Compilation message (stderr)
toll.cpp: In function 'void recur(int, int, int)':
toll.cpp:16:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (int k=0; k<adj[j].size(); k++)
| ~^~~~~~~~~~~~~~
toll.cpp:22:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int k=0; k<rev[j].size(); k++)
| ~^~~~~~~~~~~~~~
# | 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... |