最短路-hdu 2544

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2544

水题,最短路floyd

inf开小了会wa

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int maxn=105;
const int inf=0x3f3f3f3f;
int mp[maxn][maxn];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)&&n+m)
    {
        //if(m==0&&n==0)break;
        memset(mp,inf,sizeof(mp));
        while(m--)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            mp[u][v]=mp[v][u]=w;
        }
         for(int k = 1;k <= n;k++){
            for(int i = 1;i <= n;i++){
                for(int j = 1;j <= n;j++){
                    if(  mp[i][k] + mp[k][j]<mp[i][j]){
                        mp[i][j] = mp[i][k] + mp[k][j];
                    }
                }
            }
        }
        printf("%d\n",mp[1][n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41568836/article/details/81365138