原题地址:http://acm.uestc.edu.cn/#/problem/show/702
题意
略
题解
#include<bits/stdc++.h> #define clr(x) memset(x,0,sizeof(x)) using namespace std; typedef long long LL; struct edge{int to,cost;}; const int maxn=1e5; const int inf=1e9; int S[maxn+5]; int dist[maxn+5]; int query[maxn+5]; int vis[maxn+5]; queue <int> Q; vector <edge> G[maxn+5]; int main(void) { #ifdef ex freopen ("in.txt","r",stdin); //freopen ("out.txt","w",stdout); #endif int T; int n,m,q,k; int u,v,w; scanf("%d",&T); while (T--) { fill(dist,dist+sizeof(dist)/sizeof(int),inf); clr(vis); for (int i=1;i<=maxn;++i) G[i].clear(); scanf("%d%d",&n,&m); for (int i=1;i<=m;++i) { scanf("%d%d%d",&u,&v,&w); G[u].push_back((edge){v,w}); G[v].push_back((edge){u,w}); } scanf("%d",&k); for (int i=1;i<=k;++i) { scanf("%d",&S[i]); } scanf("%d",&q); for (int i=1;i<=q;++i) { scanf("%d",&query[i]); } for (int i=1;i<=k;++i) { Q.push(S[i]); dist[S[i]]=0; vis[S[i]]=1; } while (!Q.empty()) { int x=Q.front(); Q.pop(); vis[x]=0; for (int i=0;i<G[x].size();++i) { edge& e=G[x][i]; if (dist[x]<inf && dist[e.to]>e.cost+dist[x]) { dist[e.to]=e.cost+dist[x]; if (!vis[e.to]) { Q.push(e.to); vis[e.to]=1; } } } } for (int i=1;i<=q;++i) { if (dist[query[i]]<inf) printf("%d ",dist[query[i]]); else printf("-1 "); //printf("%d ",dist[i]); } printf(" "); } }