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 ll long long
#define endl '\n'
#define pb push_back
using namespace std;
const ll mx=100006;
ll INF = 100000000000000008;
ll n,m;
vector<pair<ll,ll>>v[mx];
ll d[mx];
void dij(ll x)
{
ll dis[n+1];
bool st[n+1]={};
for(int i=1;i<=n;i++)dis[i]=INF;
priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>> > pq;
pq.push({0,x});
dis[x]=0;
ll cnt=0;
while(cnt!=n)
{
while(st[pq.top().second])pq.pop();
cnt++;
ll ve=pq.top().second;
pq.pop();
st[ve]=1;
for(auto u:v[ve])
{
if(st[u.first])continue;
dis[u.first]=min(dis[u.first],dis[ve]+u.second);
pq.push({dis[u.first],u.first});
}
}
for(int i=1;i<=n;i++)d[i]=min(d[i],dis[i]);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)d[i]=INF;
while(m--)
{
ll a,b,w;
cin>>a>>b>>w;
v[a].push_back({b,w});
v[b].push_back({a,w});
}
ll k;
cin>>k;
for(int i=0;i<k;i++)
{
ll x;
cin>>x;
dij(x);
}
ll q;
cin>>q;
while(q--)
{
ll a,b;
cin>>a>>b;
cout<<min(d[a],d[b])<<endl;
}
}
/*
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
*/
# | 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... |