hdu2544 spfa 第一弹 如果让我说:我只能说,实力决定一切
来源:互联网 发布:视频后期软件免费 编辑:程序博客网 时间:2024/04/30 11:06
#include<iostream>#include<algorithm>#include<stack>#include<cstdio>#include<queue>#include<string.h>#define inf 1<<30using namespace std;int N,M,T;int dis[201];int vis[201];int head[201];queue<int>S;struct Edge{ int to,w,next;}e[20010];void init(){ memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); for(int i=0; i<=N; i++) dis[i]=inf; dis[1]=0;}void Build_Tree(int u,int v,int weight){ e[T].to=v; e[T].w=weight; e[T].next=head[u]; head[u]=T++;}int spfa(int pox){ int tem; S.push(pox); while(!S.empty()) { tem=S.front(); S.pop(); vis[tem]=0; for(int i=head[tem]; i!=-1; i=e[i].next) { int t=e[i].to; if(dis[t]>dis[tem]+e[i].w) { dis[t]=dis[tem]+e[i].w; if(!vis[t]) { S.push(t); vis[t]=1; } } } } return dis[N];}int main(){ int Tu,Tv,Tweight; while(scanf("%d%d",&N,&M),N+M) { T=0; init(); for(int i=0; i<M; i++) { scanf("%d%d%d",&Tu,&Tv,&Tweight); Build_Tree(Tu,Tv,Tweight); Build_Tree(Tv,Tu,Tweight); } printf("%d\n",spfa(1)); } return 0;}