标签:blog http io for 2014 amp log ef
//2544 G++
#include <stdio.h>
#define N 201
#define INF 100000
int g[N][N],bz[N],dist[N],s,t,n,m;
void Dijkstra(int s)
{ int i,j,min,k,x;
for (i=1;i<=n;i++)
{ dist[i]=g[s][i]; bz[i]=0; }
bz[s]=1;
for (i=1;i<n;i++)
{
min=INF;
j=s ;
for (k=1;k<=n;k++)
if ( dist[k]<min && bz[k]==0 ) { min=dist[k]; j=k; }
bz[j]=1;
for (k=1;k<=n;k++)
if ( min+g[j][k]<dist[k] && bz[k]==0) dist[k]=min+g[j][k];
}
}
int main()
{ int i,j,x,y,w;
while(scanf("%d%d",&n,&m)==2)
{ if(n==0 && m==0) break;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if(i==j) g[i][j]=0 ; else g[i][j]=INF ;
for (i=1; i<=m; i++)
{
scanf("%d%d%d",&x,&y,&w) ; g[x][y]=g[y][x]=w; }
Dijkstra(1);
printf("%d\n",dist[n]);
}
}
*************************************************************************
//2544 G++
#include <stdio.h>
#define N 201
#define INF 100000
int g[N][N],bz[N],dist[N],s,t,n,m;
void Dijkstra(int s)
{ int i,j,min,k,x;
for (i=1;i<=n;i++)
{ dist[i]=g[s][i]; bz[i]=0; }
bz[s]=1;
for (i=1;i<=n;i++)
{
min=INF;
for (k=1;k<=n;k++)
if ( dist[k]<min && bz[k]==0 ) { min=dist[k]; j=k; }
bz[j]=1;
for (k=1;k<=n;k++)
if ( min+g[j][k]<dist[k] && bz[k]==0) dist[k]=min+g[j][k];
}
}
int main()
{ int i,j,x,y,w;
while(scanf("%d%d",&n,&m)==2)
{ if(n==0 && m==0) break;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if(i==j) g[i][j]=0 ; else g[i][j]=INF ;
for (i=1; i<=m; i++)
{
scanf("%d%d%d",&x,&y,&w) ; g[x][y]=g[y][x]=w; }
Dijkstra(1);
printf("%d\n",dist[n]);
}
}
标签:blog http io for 2014 amp log ef
原文地址:http://www.cnblogs.com/2014acm/p/3905630.html