zoukankan      html  css  js  c++  java
  • uva 544 dijkstra

      Heavy Cargo 

    Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the truck itself. It is only limited by the weight restrictions that apply for the roads along the path you want to drive.


    Given start and destination city, your job is to determine the maximum load of the Godzilla V12 so that there still exists a path between the two specified cities.

    Input 

    The input file will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n ( $2 le n le 200$) and the number of road segments r ( $1 le r le 19900$) making up the street network.

    Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters. Weight limits are integers in the range 0 - 10000. Roads can always be travelled in both directions.

    The last line of the test case contains two city names: start and destination.

    Input will be terminated by two values of 0 for n and r.

    Output 

    For each test case, print three lines:

    • a line saying ``Scenario #x" where x is the number of the test case
    • a line saying ``y tons" where y is the maximum possible load
    • a blank line

    Sample Input 

    4 3
    Karlsruhe Stuttgart 100
    Stuttgart Ulm 80
    Ulm Muenchen 120
    Karlsruhe Muenchen
    5 5
    Karlsruhe Stuttgart 100
    Stuttgart Ulm 80
    Ulm Muenchen 120
    Karlsruhe Hamburg 220
    Hamburg Muenchen 170
    Muenchen Karlsruhe
    0 0
    

    Sample Output 

    Scenario #1
    80 tons
    
    Scenario #2
    170 tons
    

    Miguel A. Revilla
    1999-01-11
     
    dijkstra变形,应该也可以n^2算法dfs。另外,最大流貌似也可做。。。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<stack>
    
    using namespace std;
    
    #define LL long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define cint const int
    #define INF 100000000
    #define MAXN 220
    #define MAXM 40000
    
    struct edge{
        int u, v, w, nxt;
    }e[MAXM];
    int h[MAXN], cc, n, m;
    map<string, int> mp;
    
    int cnt;
    int U(string str){
        if(mp.count(str)) return mp[str];
        else return mp[str] = cnt++;
    }
    
    void add(int u, int v, int w){
        e[cc]=(edge){u, v, w, h[u]};
        h[u]=cc++;
        e[cc]=(edge){v, u, w, h[v]};
        h[v]=cc++;
    }
    
    struct node{
        int u, d;
        bool operator < (const node &rhs)const{
            return d < rhs.d;
        }
    };
    
    int d[MAXN];
    bool done[MAXN];
    int Dijkstra(cint s, cint t){
        priority_queue<node> q;
        fill_n(d, n, 0);        d[s]=INF;
        fill_n(done, n, false); q.push((node){s, d[s]});
        while(!q.empty()){
            node ut = q.top();  q.pop();
            int u = ut.u;   if(t==u) return d[t];
            if(done[u]) continue;       done[u]=true;
            for(int i=h[u]; i!=-1; i=e[i].nxt){
                int v = e[i].v, w = e[i].w;
                d[v] = max(d[v], min(d[u], w));
                q.push((node){v, d[v]});
            }
        }
        return d[t];
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int kase=1;
        while(scanf(" %d %d", &n, &m)==2 && (n || m)){
            int i, w;
            fill_n(h, n, -1);       cc=0;
            mp.clear();             cnt=0;
            char s1[33], s2[33];
            for(i=0; i<m; i++){
                scanf(" %s %s %d", s1, s2, &w);
                add(U(string(s1)), U(string(s2)), w);
            }
            scanf(" %s %s", s1, s2);
            printf("Scenario #%d
    %d tons
    
    ", kase++, Dijkstra(U(s1), U(s2)));
        }
        return 0;
    }
    
  • 相关阅读:
    mongoDB在windows下安装和配置.
    node 中的定时器, nextTick()和setImmediate()的使用
    node 通过指令创建一个package.json文件
    node 中 npm报错 Error: ENOENT, stat 'C:UsersAdministratorAppDataRoaming pm'
    canvas之太阳系效果
    canvas之抒写文字
    canvas之绘制一张图片
    canvas之画一个三角形
    canvas之旋转一条线段
    unity3d 2d游戏制作的模式
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3375335.html
Copyright © 2011-2022 走看看