zoukankan      html  css  js  c++  java
  • hdu 1874 畅通工程续 (floyd)

    畅通工程续
    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 69194    Accepted Submission(s): 26739

    Problem Description
    某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。
    现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
     
    Input
    本题目包含多组数据,请处理到文件结束。
    每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
    接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
    再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
     
    Output
    对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
     
    Sample Input
    3 3
    0 1 1
    0 2 3
    1 2 1
    0 2
    3 1
    0 1 1
    1 2
     
    Sample Output
    2
    -1

    C/C++:

     1 #include <cmath>
     2 #include <cstdio>
     3 #include <climits>
     4 #include <algorithm>
     5 #define INF 0xffffff
     6 using namespace std;
     7 
     8 int n, m, my_map[210][210], my_start, my_end;
     9 
    10 int my_floyd()
    11 {
    12     for (int i = 0; i < n; ++ i)
    13         for (int j = 0; j < n; ++ j)
    14             for (int k = 0; k < n; ++ k)
    15                 my_map[i][j] = min(my_map[i][j], my_map[i][k] + my_map[k][j]);
    16 
    17     if (my_map[my_start][my_end] == INF) return -1;
    18     return my_map[my_start][my_end];
    19 }
    20 
    21 int main()
    22 {
    23     /**
    24         Date Input Initialize
    25     */
    26     while (~scanf("%d%d", &n, &m))
    27     {
    28         for (int i = 0; i < n; ++ i)
    29             for (int j = 0; j < n; ++ j)
    30                 my_map[i][j] = i == j ? 0 : INF;
    31 
    32         for (int i = 0; i < m; ++ i)
    33         {
    34             int a, b, a_b_dis;
    35             scanf("%d%d%d", &a, &b, &a_b_dis);
    36             if (my_map[a][b] > a_b_dis)
    37                 my_map[a][b] = my_map[b][a] = a_b_dis;
    38         }
    39         scanf("%d%d", &my_start, &my_end);
    40         printf("%d
    ", my_floyd());
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    VS2010 正则批量替换头文件路径
    VC++ 内存泄露与检测的一种方法
    命令行编译C++程序
    Link1123:转换到COFF期间失败:文件无效或损坏
    C语言基础知识
    PCL深度图像(2)
    PCL关键点(1)
    PCL采样一致性算法
    PCL滤波介绍(3)
    PCL滤波介绍(2)
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9435494.html
Copyright © 2011-2022 走看看