您的位置:首页 > 其它

hdu2544 spfa 第一弹 如果让我说:我只能说,实力决定一切

2013-04-22 12:29 465 查看
#include<iostream>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<queue>
#include<string.h>
#define inf 1<<30
using 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
;
}
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐