• POJ 1797 Heavy Transportation


    Heavy Transportation

    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 #include <iostream>
     2 #include <string.h>
     3 #include <stdio.h>
     4 using namespace std;
     5 const int N = 1010;
     6 int n, m, G[N][N], dis[N];
     7 bool vis[N];
     8 void dij(int s) {
     9     memset(vis, false, sizeof(vis));
    10     vis[s] = true;
    11     int pos = s, MAX = -1;
    12     for(int i = 1; i <= n; i ++) dis[i] = G[s][i];
    13     for(int i = 1; i < n; i ++) {
    14         MAX = -1;
    15         for(int j = 1; j <= n; j ++) {
    16             if(MAX < dis[j] && !vis[j]) {
    17                 MAX = dis[j];
    18                 pos = j;
    19             }
    20         }
    21         vis[pos] = true;
    22         for(int j = 1; j <= n; j ++) {
    23             if(!vis[j] && G[j][pos] != -1) {
    24                 if(dis[j] == -1) dis[j] = min(dis[pos], G[pos][j]);
    25                 else dis[j] = max(dis[j], min(dis[pos], G[pos][j]));
    26             }
    27         }
    28     }
    29 }
    30 int main() {
    31     int t, k = 1;
    32     scanf("%d",&t);
    33     while(t--) {
    34         scanf("%d%d",&n,&m);
    35         for(int i = 1; i <= n; i ++) {
    36             for(int j = 1; j <= n; j ++)
    37             G[i][j] = -1;
    38         }
    39         for(int i = 1; i <= m; i ++) {
    40             int u, v, w;
    41             scanf("%d%d%d",&u,&v,&w);
    42             G[u][v] = max(G[u][v], w);
    43             G[v][u] = G[u][v];
    44         }
    45         dij(1);
    46         printf("Scenario #%d:
    %d
    
    ",k++,dis[n]);
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    [PTA练习] 愿天下有情人都是失散多年的兄妹(25分)
    sql server远程连接非1433端口
    java把double转化为long型
    StringUtils工具类
    JfreeChart折线图
    Log4j配置
    Ibatis,Spring整合(注解方式注入)
    Spring中的autowire属性(转)
    MyBatis3入门样例
    struts2 ibatis Spring系统架构图
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7278691.html
Copyright © 2020-2023  润新知