zoukankan      html  css  js  c++  java
  • 最短路 || POJ 1797 Heavy Transportation

    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

    ┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉┉┉ ∞ ∞ ┉┉┉┉ ∞ ∞ ┉┉┉

    题意:每条边上都有一个权值,表示这条边上最多运w重的物品,问从1到n一次最多能运多重的物品

    思路:其实就是求从1到n的路上的最短边的最大值

    考虑最短路的想法,dist[u]表示从1到u的最短边的最大值,当dist[v] < min(dist[u] , l[i].d)时,表示从u经过这条边到v,最短边的最大值要比直接到v大,所以更新dist[v]

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    using namespace std;
    #define SZ 1000010
    #define INF 1e9+10
    int head[SZ],nxt[SZ],tot = 0, n;
    struct edge{
        int t,d;
    }l[SZ];
    void build(int f,int t,int d){
        l[++ tot] = (edge){t,d};
        nxt[tot] = head[f];
        head[f] = tot;
    }
    int dist[1010], vis[1010];
    struct node
    {
        int u,d;
    }now;
    priority_queue<node> q;
    bool operator < (node a, node b) {return a.d < b.d; }
    int dij(int s, int e)
    {
        for(int i = 0; i <= n; i++) dist[i] = 0, vis[i] = 0;
        while(q.size()) q.pop();
        q.push((node){s,0});
        dist[s] = INF;
        while(q.size())
        {
            int u = q.top().u;
            q.pop();
            if(vis[u]) continue;
            vis[u] = 1;
            for(int i = head[u];i;i = nxt[i])
            {
                int v = l[i].t;
                if(dist[v] < min(dist[u], l[i].d))
                {
                    dist[v] = min(dist[u], l[i].d);
                    q.push((node){v,dist[v]});
                }
            }
        }
        return dist[e];
    }
    
    int main()
    {
        int T, tt = 1;
        scanf("%d", &T);
        while(T--)
        {
            int m;
            scanf("%d %d", &n, &m);
            tot = 0;
            memset(head, 0, sizeof(head));
            memset(nxt, 0, sizeof(nxt));
            while(m--)
            {
                int f, t, d;
                scanf("%d %d %d", &f, &t, &d);
                build(f, t, d);
                build(t, f, d);
            }
            printf("Scenario #%d:
    ", tt++);
            printf("%d
    ", dij(1, n));
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    开发导致的内存泄露问题,运维小伙伴儿这样排查不背锅
    JVM垃圾回收器、内存分配与回收策略
    笔试编程(二) | 7种常见的排序算法解析(附实现代码)
    HBase高级特性、rowkey设计以及热点问题处理
    Geotools创建Feature的两种方式
    geotools实现追加数据到数据库
    Java连接mysql数据库经典代码
    leaflet加载高德地图和Geoserver的WMS服务
    geotools学习之连接数据库并获取数据
    LeaFlet中切片图层使用自定义坐标系
  • 原文地址:https://www.cnblogs.com/pinkglightning/p/9562985.html
Copyright © 2011-2022 走看看