zoukankan      html  css  js  c++  java
  • POJ 1797 Heavy Transportation (dijkstra 最小边最大)

    Heavy Transportation

    题目链接:

    http://acm.hust.edu.cn/vjudge/contest/66569#problem/A

    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 Input

    Scenario #1:
    4

    题意:

    给出平面上的n个坐标,两两之间可联通;
    求从#1到#n点的一条路径,使得其中最小的边最大;

    题解:

    直接用dijkstra实现即可;
    dis[i]为起点s到当前点i的路径上最大的最小边;
    本质与dijkstra求最短路一致;

    POJ2253:求最大边最小;
    (http://www.cnblogs.com/Sunshine-tcf/p/5693659.html)
    本质一样,更新时存在区别;
    另外,求最大最小边时,不能把dis[s]初始化为0(即循环n-1次),否则更新失败;

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #define mid(a,b) ((a+b)>>1)
    #define LL long long
    #define maxn 1100
    #define inf 0x3f3f3f3f
    #define IN freopen("in.txt","r",stdin);
    using namespace std;
    
    int n, m;
    int value[maxn][maxn];
    int x[maxn],y[maxn];
    int dis[maxn];
    int pre[maxn];
    bool vis[maxn];
    
    void dijkstra(int s) {
        memset(vis, 0, sizeof(vis));
        memset(pre, -1, sizeof(pre));
        for(int i=1; i<=n; i++) dis[i] = value[s][i];
    
        for(int i=1; i<n; i++) {
            int p;
            int mindis = -1;
            for(int j=1; j<=n; j++) {
                if(!vis[j] && dis[j]>mindis)
                    mindis = dis[p=j];
            }
            vis[p] = 1;
            for(int j=1; j<=n; j++) {
                if(!vis[j] && dis[j] < min(dis[p],value[p][j])) {
                    dis[j] = min(dis[p], value[p][j]);
                    pre[j] = p;
                }
            }
        }
    }
    
    int main(int argc, char const *argv[])
    {
        //IN;
    
        int t,ca=1; cin>>t;
        while(t--)
        {
            cin >> n >> m;
            for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) value[i][j]=0;
            while(m--){
                int u,v,w; scanf("%d %d %d",&u,&v,&w);
                value[u][v] = value[v][u] = w;
            }
    
            dijkstra(1);
    
            printf("Scenario #%d:
    %d
    
    ", ca++, dis[n]);
        }
    
        return 0;
    }
    
  • 相关阅读:
    【Spring-MVC】
    【多线程】线程池关闭
    【DDD】基于事件驱动EDA -- 待完成
    【DDD】编码实战
    【Elastic Search】01- 原理
    【DDD】基于DDD的分层设计
    【DDD】Thoughtworks笔记(编码样例) -- 未完成
    【DDD】Thoughtworks笔记(目录划分、异常设计)
    平方和求余
    Factoring a Polynomial
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5693985.html
Copyright © 2011-2022 走看看