HDU 最短路

#include<iostream>
#define INF 200000
using namespace std;
int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
int map[1002][1002],N,M;
int findmap(int sta,int end){
    int t;
    if(sta>end){t=end;end=sta;sta=t;}
    return map[sta][end];
}
void setmap(int sta,int end,int val){
    int t;
    if(sta>end){t=end;end=sta;sta=t;}
    if(sta==end){map[sta][end]=0;return;}
    map[sta][end]=val;
}
int main(){
    int a,b,c;
    cin>>N>>M;
    while(N!=0){
        for(int yy=0;yy<=N+5;yy++){
            for(int xx=0;xx<=N+5;xx++){
                setmap(xx,yy,INF);
            }
        }
        for(int i=0;i<M;i++){
            cin>>a>>b>>c;
            setmap(a,b,c);
        }
        for(int mid=1;mid<=N;mid++){
            for(int sta=1;sta<=N;sta++){
                for(int end=1;end<=N;end++){
                    a=findmap(sta,end);
                    b=findmap(sta,mid)+findmap(mid,end);
                    setmap(sta,end,min(a,b));
                }
            }
        }
        
        cout<<map[1][N]<<endl;
        cin>>N>>M;
    }
    return 0;

}

http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1001&cid=36602

原文地址:https://www.cnblogs.com/forwhat00/p/13245914.html