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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值