标签:iostream ios sizeof push color namespace str front cstring
#include<iostream> #include<cstring> #include<queue> using namespace std; const int N=1e6; int h[N],e[N],ne[N],w[N]; int n,m; int idx; bool st[N]; int dist[N]; void add(int a,int b,int c) { e[idx]=b; w[idx]=c; ne[idx]=h[a]; h[a]=idx++; } void spfa() { memset(dist,0x3f,sizeof dist); memset(st,0,sizeof st); queue<int>q; q.push(1); dist[1]=0; st[1]=true; while(q.size()) { int t=q.front(); q.pop(); st[t]=false; for(int i=h[t];~i;i=ne[i]) { int j=e[i]; if(dist[j]>dist[t]+w[i]) { dist[j]=dist[t]+w[i]; if(!st[j]) { q.push(j); st[j]=true; } } } } } int main() { cin>>n>>m; memset(h,-1,sizeof h); for(int i=1;i<=n;i++) { int a,b,c; cin>>a>>b>>c; add(a,b,c); add(b,a,c); } spfa(); cout<<dist[m]<<endl; return 0; }
POJ 2387 Til the Cows Come Home
标签:iostream ios sizeof push color namespace str front cstring
原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12239874.html