zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    使用自定义RadioButton和ViewPager实现TabHost效果和带滑动的页卡效果
    Android 实现文件上传功能(upload)
    Hibernate配置文件
    ICMP报文分析
    AVC1与H264的差别
    内存泄漏以及常见的解决方法
    数据挖掘十大经典算法
    关于java的JIT知识
    Ubuntu安装二:在VM中安装Ubuntu
    hdu 1520Anniversary party(简单树形dp)
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7278691.html
Copyright © 2011-2022 走看看