hdu 1874 暢通工程續

暢通工程續
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 57350 Accepted Submission(s): 21539

Problem Description

某省自從實行了很多年的暢通工程計劃後,終於修建了很多路。不過路多了也不好,每次要從一個城鎮到另一個城鎮時,都有許多種道路方案可以選擇,而某些方案要比另一些方案行走的距離要短很多。這讓行人很困擾。

現在,已知起點和終點,請你計算出要從起點到終點,最短需要行走多少距離。

Input

本題目包含多組數據,請處理到文件結束。
每組數據第一行包含兩個正整數N和M(0

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define INF 999999999
#define maxn 250
int edge[maxn][maxn];
using namespace std;

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(i == j) edge[i][j] = 0;
                else       edge[i][j] = INF;
            }
        }
        for(int i = 0; i < m; i++)
        {
            int a,b,x;
            scanf("%d%d%d",&a,&b,&x);
            if(x < edge[a][b])
            edge[a][b] = edge[b][a] = x;
        }
        int s,d;
        scanf("%d%d",&s,&d);
        for(int k = 0; k < n; k++)
        {
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < n; j++)
                {
                    if(edge[i][j] > edge[i][k] + edge[k][j])
                    {
                        edge[i][j] = edge[i][k] + edge[k][j];
                    }
                }
            }
        }
        if(edge[s][d] != INF)
        printf("%d\n",edge[s][d]);
        else
            printf("-1\n");
    }
}

Sample Output

2
-1

Author

linle

Source

2008浙大研究生複試熱身賽(2)——全真模擬

Recommend

lcy | We have carefully selected several similar problems for you: 1217 1875 1233 1142 1232

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章