hdu2544最短路

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<map>
#include<queue>
#include <deque>
#include <list>
#include <ctime>
#include <stack>
#include <vector>
#include<set>
#define Maxn 128
#define MOD
typedef long long ll;
#define FOR(i,j,n) for(int i=j;i<=n;i++)
#define DFR(i,j,k) for(int i=j;i>=k;--i)
#define lowbit(a) a&-a
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
using namespace std;
int n,m;
int map_p[Maxn][Maxn];
void Init()
{   FOR(i,1,n)FOR(j,1,n)i==j?map_p[i][j]=0:map_p[i][j]=inf;

}
void Floyd()
{   FOR(k,1,n)FOR(i,1,n)FOR(j,1,n)map_p[i][j]>map_p[i][k]+map_p[k][j]?map_p[i][j]=map_p[i][k]+map_p[k][j]:map_p[i][j]=map_p[i][j];
}
int main()
{   while(~scanf("%d%d",&n,&m)&&n&&m)
    {   Init();
        int a,b,x;
        FOR(i,1,m)
        {scanf("%d%d%d",&a,&b,&x);map_p[a][b]=map_p[b][a]=x;}
        Floyd();
        printf("%d\n",map_p[1][n]);
    }
    return 0;
}





你可能感兴趣的:(图论,最短路)