#include cstdio
#include cstring
#define INF 0x3f3f3f3f
#define MAXN 1005
struct Edge
{
int v,next,val;
};
int n,m,u,v,val,h[MAXN],dist[MAXN],vis[MAXN],q[MAXN],tot;
Edge edge[MAXN];
void addEdge(int u,int v,int val)
{
tot++;
edge[tot].v=v;
edge[tot].next=h[u];
edge[tot].val=val;
h[u]=tot;
}
void init()
{
freopen("SPFA.in","r",stdin);
freopen("SPFA.out","w",stdout);
scanf("%d %d",&n,&m);
for (int i=1;i<=m;i++)
{
scanf("%d %d %d",&u,&v,&val);
addEdge(u,v,val); addEdge(v,u,val);
}
}
void SPFA(int s)
{
int head=1,tail=2;
memset(dist,INF,sizeof(dist));
memset(vis,0,sizeof(vis));
memset(q,0,sizeof(q));
dist[s]=0; vis[s]=1; q[1]=s;
while (head!=tail)
{
int now=q[head];
for (int x=h[now];x!=0;x=edge[x].next)
{
if (dist[now]+edge[x].val《dist[edge[x].v])
{
dist[edge[x].v]=dist[now]+edge[x].val;
if (vis[edge[x].v]!=1)
{
q[tail++]=edge[x].v;
vis[edge[x].v]=1;
}
}
}
vis[now]=0;
head++;
}
for (int i=2;i<=n;i++) printf("%d-->%d dist=%d\n",s,i,dist[i]);
}
int main()
{
init();
SPFA(1); // 1可以修改,表示源点
return 0;
}
-------------------------------------------------------------------------------------------------------------------