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 maxn 100010
#define ff first
#define ss second
using namespace std;
int n,m,k,q;
bool is_npp[maxn];
vector<pair<int,int>> edges[maxn];
vector<long long> sp(maxn,INT_MAX);
vector<int> npp;
int parent[maxn];
struct Query
{
int qu,qv,idx;
};
queue<Query> quer[maxn];
void dijkstra()
{
priority_queue<pair<long long,int>> q;
vector<long long> dist(n+1,INT_MAX);
for(int i=0;i<npp.size();i++)
{
q.push({0,npp[i]});
dist[npp[i]]=0;
sp[npp[i]]=0;
}
while(!q.empty())
{
int curr=q.top().second;
long long currDist=-q.top().first;
q.pop();
if(dist[curr]<currDist) continue;
for(auto edge:edges[curr])
{
int next=edge.first;
int weight=edge.second;
if(dist[curr]+weight<dist[next])
{
dist[next]=dist[curr]+weight;
sp[next]=min(sp[next],dist[next]);
q.push({-dist[next],next});
}
}
}
}
int findd(int u)
{
if(parent[u]==0) return u;
return parent[u]=findd(parent[u]);
}
int ans[maxn];
void unite(int u,int v,int mn)
{
u=findd(u);
v=findd(v);
if(u==v) return;
if(quer[u].size()<quer[v].size()) swap(u,v);
parent[v]=u;
while(!quer[v].empty())
{
Query curr=quer[v].front();
quer[v].pop();
if(findd(curr.qu)==findd(curr.qv) && ans[curr.idx]==-1) {ans[curr.idx]=mn;continue;}
if(ans[curr.idx]==-1) quer[u].push(curr);
}
}
vector<pair<int,int>> finale;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
int ai,bi,wi;
for(int i=0;i<m;i++)
{
cin>>ai>>bi>>wi;
edges[ai].push_back({bi,wi});
edges[bi].push_back({ai,wi});
}
cin>>k;
for(int i=0;i<k;i++)
{
int npp1;
cin>>npp1;
is_npp[npp1]=true;
npp.push_back(npp1);
}
dijkstra();
cin>>q;
for(int i=0;i<q;i++)
{
ans[i]=-1;
Query curr;
cin>>curr.qu>>curr.qv;
curr.idx=i;
quer[curr.qu].push(curr);
quer[curr.qv].push(curr);
}
for(int i=1;i<=n;i++)
{
finale.push_back({sp[i],i});
}
sort(finale.begin(),finale.end());
reverse(finale.begin(),finale.end());
for(int i=0;i<finale.size();i++)
{
auto curr=finale[i];
for(auto e:edges[curr.ss])
{
if(curr.ff<=sp[e.ff])
{
unite(curr.ss,e.ff,curr.ff);
}
}
}
for(int i=0;i<q;i++)
{
cout<<ans[i]<<"\n";
}
}
/*
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 dijkstra()':
plan.cpp:22:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i=0;i<npp.size();i++)
| ~^~~~~~~~~~~
plan.cpp: In function 'int main()':
plan.cpp:106:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(int i=0;i<finale.size();i++)
| ~^~~~~~~~~~~~~~
# | 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... |