zoukankan      html  css  js  c++  java
  • ZOJ-1952 Heavy Cargo 【Floyd】

    Description

    Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the truck itself. It is only limited by the weight restrictions that apply for the roads along the path you want to drive.

    Given start and destination city, your job is to determine the maximum load of the Godzilla V12 so that there still exists a path between the two specified cities.

    Input

    The input will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n (2<=n<=200) and the number of road segments r (1<=r<=19900) making up the street network.
    Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters. Weight limits are integers in the range 0 - 10000. Roads can always be travelled in both directions.
    The last line of the test case contains two city names: start and destination.
    Input will be terminated by two values of 0 for n and r.

    Output

    For each test case, print three lines:
    • a line saying "Scenario #x" where x is the number of the test case
    • a line saying "y tons" where y is the maximum possible load
    • a blank line

    Sample Input

    4 3
    Karlsruhe Stuttgart 100
    Stuttgart Ulm 80
    Ulm Muenchen 120
    Karlsruhe Muenchen
    5 5
    Karlsruhe Stuttgart 100
    Stuttgart Ulm 80
    Ulm Muenchen 120
    Karlsruhe Hamburg 220
    Hamburg Muenchen 170
    Muenchen Karlsruhe
    0 0

    Sample Output

    Scenario #1
    80 tons
     
    Scenario #2
    170 tons
    
    

    题解:

    Floyd的思想,核心方程为d(i,j) = max(d(i,j), min(d(i,k), d(k,j)));

    代码:


     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<string>
     5 #include<vector>
     6 #include<set>
     7 #include<map>
     8 using namespace std;
     9 #define M(a, b) memset(a, b, sizeof(a))
    10 #define INF 0x3f3f3f3f
    11 const int N = 200 + 5;
    12 int d[N][N], n, r;
    13 map<string, int> mp;
    14 string s1, s2;
    15 
    16 void Floyd() {
    17     int a, b, w;
    18     mp.clear();
    19     M(d, -1);
    20     for (int i = 1; i <= n; ++i) d[i][i] = 0; 
    21     int tot = 0;
    22     for (int i = 0; i < r; ++i) {
    23         cin >> s1 >> s2 >> w;
    24         if (mp[s1] == 0) mp[s1] = ++tot;
    25         if (mp[s2] == 0) mp[s2] = ++tot;
    26         a = mp[s1]; b = mp[s2];
    27         d[a][b] = d[b][a] = w;
    28     }
    29     for (int k = 1; k <= n; ++k)
    30         for (int i = 1; i <= n; ++i)
    31             for (int j = 1; j <= n; ++j)
    32                 d[i][j] = max(d[i][j], min(d[i][k], d[k][j]));
    33 }
    34  
    35 int main() {
    36     int kase = 0;
    37     while (cin >> n >> r) {
    38         if (n == 0 && r == 0) break;
    39         Floyd();
    40         cin >> s1 >> s2;
    41         int x = mp[s1], y = mp[s2];
    42         cout << "Scenario #" << ++kase << endl;
    43         cout << d[x][y] << " tons" << endl;
    44         cout << endl;
    45     }
    46 
    47     return 0;
    48 }
  • 相关阅读:
    [题解] [NOIP2008] 双栈排序——关系的冲突至图论解法
    [搬运] [贪心]NOIP2011 观光公交
    [总结] 最短路径数问题
    [持续更新]一些zyys的题的集合
    [教程]Ubuntu下完整配置自动壁纸切换
    在NOILINUX下的简易VIM配置
    [模板]ST表浅析
    21、Android--RecyclerView
    20、Android--GridView
    19、Android--ListView
  • 原文地址:https://www.cnblogs.com/robin1998/p/6628788.html
Copyright © 2011-2022 走看看