天天看点

hdu 1874 畅通工程续(floyd

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

题意:题意??最短路呀!

思路:数据那么少直接floyd秒

有坑⊙﹏⊙b汗,1、两个村庄有多条路(不存在的,根本不跳进去 2、开始城镇和结束相同(直接跳进去,扫了一下discuss才知道有这个操作,而且floyd不会判这种情况距离为0。

代码:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<stack>
#include<ctime>
#include<queue>
#define LOCAL
#define mst(a,b) memset(a,b,sizeof(a))
const int  INF = ;
//const int maxn = 30000;
using namespace std;

int main(){
    /*#ifdef local
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    #endif*/
    int n, m, z, G[][], s, t, x, y;
    while(~scanf("%d%d",&n,&m)){
        mst(G , INF);
        while(m--){
            scanf("%d%d%d",&x,&y,&z);
            if(z < G[x][y]) G[x][y] = G[y][x] = z;
        }
        for(int k = ; k < n; k++)
         for(int i = ; i < n; i++)
          for(int j = ; j < n; j++){
              if(G[i][j] > G[i][k] +G[k][j]) 
              G[i][j] = G[i][k] +G[k][j];
            if(i == j) G[i][j] = ;  
          }
        scanf("%d%d",&s,&t);
        if(G[s][t] == INF) printf("-1\n");
        else printf("%d\n",G[s][t]);
    }
    return ;
}