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;
const int maxn = 100005;
const long long inf = (1LL << 61);
int n, m, k, q;
int startCities[maxn];
vector<pair<int, int>> g[maxn];
long long dist[maxn];
vector<long long> toTry;
struct Query
{
int from;
int to;
int idx;
Query()
{
}
Query(int _from, int _to, int _idx)
{
from = _from;
to = _to;
idx = _idx;
}
};
struct roll_back
{
long long a;
long long b;
long long rnka,rnkb;
long long where;
};
stack <roll_back> s;
long long par[maxn],rnk[maxn];
void roll_back(long long pos)
{
while(!s.empty() && s.top().where<=pos)
{
auto t = s.top();
s.pop();
rnk[t.a] = t.rnka;
rnk[t.b] = t.rnkb;
par[t.a] = t.a;
par[t.b] = t.b;
}
}
long long Find(long long p)
{
if(par[p]==p)
return p;
return Find(par[p]);
}
void Union(long long p,long long q,long long where)
{
// cout<<"Union "<<p<<" "<<q<<" "<<where<<endl;
long long parp,parq;
p = Find(p);
q = Find(q);
if(p==q)return ;
//cout<<"svurzvame "<<p<<" "<<q<<endl;
s.push({p,q,rnk[p],rnk[q],where});
if(rnk[p]<rnk[q])swap(p,q);
if(rnk[p]==rnk[q])
rnk[p]++;
par[q] = p;
}
vector<Query> queries;
long long answers[maxn];
vector<pair<int, int>> byValue[maxn];
void parallelBinarySearch(int level, int l, int r, vector<int>& curr)
{
if (l > r)
{
return;
}
if (curr.empty())
{
return;
}
if (l == r)
{
for (int i = 0; i < curr.size(); ++i)
{
answers[queries[curr[i]].idx] = toTry[l];
}
return;
}
int mid = (l + r) / 2;
for (int i = toTry.size() - 1; i >= mid; --i)
{
for (int j = 0; j < byValue[i].size(); ++j)
{
Union(byValue[i][j].first, byValue[i][j].second, i);
}
}
roll_back(mid);
vector<int> toLeft;
vector<int> toRight;
for (int i = 0; i < curr.size(); ++i)
{
if (Find(queries[curr[i]].from) == Find(queries[curr[i]].to))
{
toRight.push_back(curr[i]);
}
else
{
toLeft.push_back(curr[i]);
}
}
parallelBinarySearch(level + 1, l, mid, toLeft);
parallelBinarySearch(level + 1, mid + 1, r, toRight);
}
vector<long long> uniqueDistances()
{
set<long long> s;
vector<long long> res;
for (int i = 1; i <= n; ++i)
{
s.insert(dist[i]);
}
for (auto element : s)
{
res.push_back(element);
}
return res;
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n >> m;
for (int i = 1; i <= m; ++i)
{
int x, y, w;
cin >> x >> y >> w;
g[x].push_back(make_pair(y, w));
g[y].push_back(make_pair(x, w));
}
cin >> k;
for (int i = 1; i <= k; ++i)
{
cin >> startCities[i];
}
for (int i = 1; i <= n; ++i)
{
dist[i] = inf;
}
priority_queue<pair<long long, int>> pq;
for (int i = 1; i <= k; ++i)
{
pq.push(make_pair(0, startCities[i]));
dist[startCities[i]] = 0;
}
while (!pq.empty())
{
int node = pq.top().second;
long long curr = -pq.top().first;
pq.pop();
for (int i = 0; i < g[node].size(); ++i)
{
long long path = g[node][i].second + curr;
int to = g[node][i].first;
if (dist[to] > path)
{
dist[to] = path;
pq.push(make_pair(-path, to));
}
}
}
/*cout << "check " << endl;
for (int i = 1; i <= n; ++i)
{
cout << dist[i] << ' ';
}
cout << endl;*/
toTry = uniqueDistances();
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j < g[i].size(); ++j)
{
if (i < g[i][j].first)
{
long long d = min(dist[i], dist[g[i][j].first]);
int l = 0;
int r = toTry.size() - 1;
while (l <= r)
{
int mid = (l + r) / 2;
if (toTry[mid] >= d)
{
r = mid - 1;
}
else
{
l = mid + 1;
}
}
byValue[l].push_back(make_pair(i, g[i][j].first));
}
}
}
cin >> q;
for (int i = 1; i <= q; ++i)
{
int from, to;
cin >> from >> to;
queries.push_back(Query(from, to, i));
}
for(int i=1;i<=n;i++)
par[i] = i,rnk[i] = 1;
vector<int> starting;
for (int i = 0; i < q; ++i)
{
starting.push_back(i);
}
parallelBinarySearch(1, 0, toTry.size() - 1, starting);
for (int i = 1; i <= q; ++i)
{
cout << answers[i] << "\n";
}
return 0;
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
*/
Compilation message (stderr)
plan.cpp: In function 'void Union(long long int, long long int, long long int)':
plan.cpp:69:15: warning: unused variable 'parp' [-Wunused-variable]
69 | long long parp,parq;
| ^~~~
plan.cpp:69:20: warning: unused variable 'parq' [-Wunused-variable]
69 | long long parp,parq;
| ^~~~
plan.cpp: In function 'void parallelBinarySearch(int, int, int, std::vector<int>&)':
plan.cpp:100:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for (int i = 0; i < curr.size(); ++i)
| ~~^~~~~~~~~~~~~
plan.cpp:112:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for (int j = 0; j < byValue[i].size(); ++j)
| ~~^~~~~~~~~~~~~~~~~~~
plan.cpp:123:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for (int i = 0; i < curr.size(); ++i)
| ~~^~~~~~~~~~~~~
plan.cpp: In function 'int main()':
plan.cpp:205:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
205 | for (int i = 0; i < g[node].size(); ++i)
| ~~^~~~~~~~~~~~~~~~
plan.cpp:231:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
231 | for (int j = 0; j < g[i].size(); ++j)
| ~~^~~~~~~~~~~~~
# | 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... |