zoukankan      html  css  js  c++  java
  • POJ

    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运送货物,有M条道路,每条道路都有它的最大载重量,问从城市1到城市N运送最多的重量是多少,找一条1–>N的路径,在不超过每条路径的最大载重量的情况下,使得运送的货物最多。一条路径上的最大载重量为这个路径上权值最小的边;

    在dijkstra求最短路中我们每次选取的是离原点的最短边来松弛剩下的节点。
    而在这里我们要找到到达终点的的一条路使其路径上的最小边尽量大,才能使其载重量较大。
    也就是说每次选择一个从源点到某个载重量最大的点,来松弛其他点,dist将记录到达每个点的路径上的最小载重。选择最大的最小载重能让整条路径的最小载重变大。利用这个路径上最大的较小载重去更新其他点能使到达其他点的最小载重变大。最终使到达每个点最小载重都变大。

    松弛的过程中的比较,如果我从源点到达一个节点i的最小载重小于从当前选取的j节点到达i载重,那么从源点到达j的最小承重将被更新成一个较大值,而这个更新的意义是到达j点的路径中最小的承重使其最大。如果从maxi到达j的承重大于到达maxi的承重,那么经过maxi走到j的最小承重将是maxn保持不变,否则最小承重比maxn更小,从maxi到达j的最小承重被maxi—j这条边所压低。如果没有判断dist[j]小于min【maxn,ma[ maxi ] [ j ]】才做更新,dist【j】,也就是经过其他地方或从源点直接到达j的最小承重更大的情况会被忽略,dist【j】会有被更新成较小值的危险。
    最后输出dist【n】即从源点到n点的某条路径上最大的最小载重,且这个路径比经过其他边所遇到的最小载重更大

    代码如下:

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    int dist[1008];
    int ma[1008][1008];
    bool vis[1008];
    int n,m;
    void dijkstra(int s)
    {
        memset(vis,false,sizeof(vis));
        memset(dist,0,sizeof(dist));
        for(int i=1; i<=n; i++)
            dist[i]=ma[s][i];
        dist[s]=0;
        vis[s]=true;
        for(int i=2;i<=n;i++)
        {
            int maxn=0,maxi=1;
            for(int j=1;j<=n;j++)///记录和寻找的不再是累加的最短路径,而是dist记录每条路径上经过的最小承重,每次遍历收集到最大承重
            {
                if(!vis[j]&&dist[j]>maxn)///用最大承重更新路径上可到达的较小承重
                {
                    maxn=dist[j];
                    maxi=j;
                }
            }
            vis[maxi]=true;
            for(int j=1;j<=n;j++)
                if(!vis[j]&&dist[j]<min(maxn,ma[maxi][j]))///注意更新dist的条件,当dist[j]较小时,才能用一个maxn或ma上的较大值来更新
                    dist[j]=min(maxn,ma[maxi][j]);///没有这个判断直接赋值会导致dist被更新得越来越小
        }
    }
    int main()
    {
        int t,x,y,val,cas=0;
        scanf("%d",&t);
        while(t--)
        {
            memset(ma,0,sizeof(ma));///ma中无法到达的点应该被标记为0承重,被标为最大值会导致最大值被直接选用,无法被更新
            scanf("%d%d",&n,&m);
            for(int i=0;i<m;i++)
            {
                scanf("%d%d%d",&x,&y,&val);
                ma[x][y]=val;
                ma[y][x]=val;
            }
            dijkstra(1);
            printf("Scenario #%d:
    ",++cas);
            printf("%d
    
    ",dist[n]);///注意额外空行
        }
    }
    
  • 相关阅读:
    django之表设计、路由层等
    django之三剑客、静态文件配置、请求响应对象、数据库操作
    djang小项目过程中的小问题 02(跳转界面)
    生鲜超市项目错误及解决办法(crispy_forms、外键指向自己、class嵌套访问父类、meta类及各种字段参数)
    生鲜超市项目错误及解决办法(安装mysqlclient)
    djang小项目过程中的小问题 01(django中的configrarion配置、django自带命名规范)
    react-Hook
    react中登录注册 使用验证码验证
    react状态管理器之mobx
    react中的虚拟DOM,jsx,diff算法。让代码更高效
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135845.html
Copyright © 2011-2022 走看看