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 = 0x3f3f3f3f;
//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[210][210], 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 = 0; k < n; k++)
         for(int i = 0; i < n; i++)
          for(int j = 0; 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] = 0;  
          }
        scanf("%d%d",&s,&t);
        if(G[s][t] == INF) printf("-1\n");
        else printf("%d\n",G[s][t]);
    }
    return 0;
} 
發佈了37 篇原創文章 · 獲贊 7 · 訪問量 8720
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章