AC代码
#include<stdio.h> #include<string.h> #include<queue> #include<vector> #include<iostream> #include<algorithm> using namespace std; using namespace std; const int maxn=2*50000; #define inf 99999999 struct node { int v; int u; int w; int next; }V[maxn]; int head[maxn]; int d[maxn]; int n,m,s,t; int tol; int done[maxn]; typedef pair<int,int>pii; priority_queue<pii,vector<pii>,greater<pii> >q; void init() { tol=0; memset(head,-1,sizeof(head)); } void dijk() { memset(done,0,sizeof(done)); for(int i=0;i<=n;i++) d[i]=inf; d[s]=0; q.push(make_pair(d[s],s)); while(!q.empty()) { pii u=q.top(); q.pop(); int x=u.second; if(done[x]) continue; done[x]=1; for(int e=head[x];e!=-1;e=V[e].next) { int v=V[e].v; int w=V[e].w; if(d[x]+w<d[v]) { d[v]=d[x]+w; q.push(make_pair(d[v],v)); } } } if(d[t]==inf) printf("unreachable "); else printf("%d ",d[t]); } int main() { int T; int tmp=0; scanf("%d",&T); while(T--) { scanf("%d %d %d %d",&n,&m,&s,&t); init(); m*=2; int u,v,w; for(int i=0;i<m;i+=2) { scanf("%d %d %d",&u,&v,&w); V[i].u=u; V[i].v=v; V[i].w=w; V[i].next=head[u]; head[u]=i; V[i+1].u=v; V[i+1].v=u; V[i+1].next=head[v]; V[i+1].w=w; head[v]=i+1; } printf("Case #%d: ",++tmp); dijk(); } return 0; }