HDU 2544 最短路

http://acm.hdu.edu.cn/showproblem.php?pid=2544

在TCO又一次0之后。。。突然会写最短路了,ORZ

View Code
#include <iostream>
#include <string.h>
using namespace std;
const int INF=100000000;
int map[110][110];
int n,m;
int dis[110],vis[110];
int Dijkstra(int s,int t)
{
    for(int i=1;i<=n;i++)
        dis[i]=INF;
    memset(vis,0,sizeof(vis));
    dis[s]=0;
    for(int i=0;i<n;i++)
    {
        int ans=INF,u;
        for(int j=1;j<=n;j++)
            if(!vis[j] && dis[j]<ans)
            {
                ans=dis[j];
                u=j;
            }
        vis[u]=1;
        if(u==t)return ans;
        for(int j=1;j<=n;j++)
            if(map[u][j]!=INF && dis[u]+map[u][j]<dis[j])
                dis[j]=dis[u]+map[u][j];
    }
}
int main()
{
    while(scanf("%d%d",&n,&m),(n||m))
    {
        int s,t,k;
        for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
                map[i][j]=INF;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&s,&t,&k);
            map[s][t]=map[t][s]=k;
        }
        printf("%d\n",Dijkstra(1,n));
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/xiaohongmao/p/2497778.html