zoukankan      html  css  js  c++  java
  • POJ 1797 Heavy Transportation(Dijkstra运用)

    Description
    Background 
    Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight. 
    Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know. 

    Problem 
    You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

    Input

    The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

    Output

    The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

    Sample Input

    1
    3 3
    1 2 3
    1 3 4
    2 3 5
    

    Sample Output

    Scenario #1:
    4

    题意:这个题目呢,跟上一篇的题意有点相反,这一次是求在街道1到达街道n的路径所能承受的最大重量。也就是求能从1到达n的路径上的最小承重的最大值。
    思路:Dijkstra运用,我们知道dijkstra是每一次将离源点最近的那一一个点进行松弛,而我们现在要求最小承重的最大值,那我们就应该将离源点承重最大的那个点进行松弛。
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 int n, m, dis[1010], mp[1010][1010], vis[1010];
     7 void Dijkstra()
     8 {
     9     for (int i = 1; i <= n; i++) {
    10         vis[i] = 0; dis[i] = mp[1][i];//初始化为1到i的最大承重
    11     }
    12     for (int i = 1; i <= n; i++) {
    13         int cnt = 0, k;
    14         for (int j = 1; j <= n; j++) {
    15             if (!vis[j] && dis[j] > cnt) {
    16                 cnt = dis[j];
    17                 k = j;
    18             }
    19         }
    20         vis[k] = 1;
    21         for (int j = 1; j <= n; j++) {
    22             if (!vis[j] && dis[j] < min(dis[k], mp[k][j]))
    23                 dis[j] = min(dis[k], mp[k][j]);
    24         }
    25     }
    26 }
    27 int main()
    28 {
    29     ios::sync_with_stdio(false);
    30     int T;
    31     cin >> T;
    32     for(int t=1;t<=T;t++){
    33         cin >> n >> m;
    34         memset(mp, 0, sizeof(mp));
    35         for (int a, b, c, i = 0; i < m; i++) {
    36             cin >> a >> b >> c;
    37             mp[a][b] = mp[b][a] = c;
    38         }
    39         Dijkstra();
    40         cout << "Scenario #" << t << ":" << endl;
    41         cout << dis[n] << endl << endl;
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    主从复制(多台服务器搭建环境)【十五】
    Linux中kill,pkill,killall和xkill命令汇总讲解
    redis持久化机制【十三】
    守护进程详解及创建,daemon()使用
    【small项目】MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
    Redis事务【十二】
    乐观锁和悲观锁的区别
    Redis 命令与连接【十一】
    Redis 配置【十】
    DB_Links创建际删除
  • 原文地址:https://www.cnblogs.com/wangrunhu/p/9497529.html
Copyright © 2011-2022 走看看