zoukankan      html  css  js  c++  java
  • 7-33 Universal Travel Sites (35分)

    After finishing her tour around the Earth, CYLL is now planning a universal travel sites development project. After a careful investigation, she has a list of capacities of all the satellite transportation stations in hand. To estimate a budget, she must know the minimum capacity that a planet station must have to guarantee that every space vessel can dock and download its passengers on arrival.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains the names of the source and the destination planets, and a positive integer N (≤500). Then N lines follow, each in the format: source[i] destination[i] capacity[i] where source[i] and destination[i] are the names of the satellites and the two involved planets, and capacity[i] > 0 is the maximum number of passengers that can be transported at one pass from source[i] to destination[i]. Each name is a string of 3 uppercase characters chosen from {A-Z}, e.g., ZJU.

    Note that the satellite transportation stations have no accommodation facilities for the passengers. Therefore none of the passengers can stay. Such a station will not allow arrivals of space vessels that contain more than its own capacity. It is guaranteed that the list contains neither the routes to the source planet nor that from the destination planet.

    Output Specification:

    For each test case, just print in one line the minimum capacity that a planet station must have to guarantee that every space vessel can dock and download its passengers on arrival.

    Sample Input:

    EAR MAR 11
    EAR AAA 300
    EAR BBB 400
    AAA BBB 100
    AAA CCC 400
    AAA MAR 300
    BBB DDD 400
    AAA DDD 400
    DDD AAA 100
    CCC MAR 400
    DDD CCC 200
    DDD MAR 300
    
     

    Sample Output:

    700
    最大流问题。

    题意大概是说给出了起点和终点,起点往终点去游客,终点一次最多去多少游客,路上有限制人数,任何一条路径到达终点的游客数实际是路径上限制的最小值,比如从a到b一次限制最多5人,从b到c限制一次最多3人,从c到d限制一次最多4人,那么经过a-b-c-d,最后到d一次最多去3人。
    用dinic算法。
    代码:
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define inf 0x3f3f3f3f
    using namespace std;
    int n,p[1050],ind,to[70000],c,f;
    int first[1050],nex[1050],u[1050],v[1050],w[1050],work[1050],dis[1050];
    int chan(char *s) {///字符串转换为数字 方便建图
        int d = 0;
        while(*s) {
            d = d * 26 + *s - 'A';
            s ++;
        }
        return to[d] ? to[d] : (to[d] = ++ ind);
    }
    bool bfs(int s,int t) {
        memset(dis,-1,sizeof(dis));
        int head = 0,tail = 0,q[1050];
        dis[s] = 0;
        q[tail ++] = s;
        while(head < tail) {
            int tt = q[head ++],k = first[tt];
            while(k != -1) {
                if(w[k ^ 1] && dis[v[k]] == -1) {
                    q[tail ++] = v[k];
                    dis[v[k]] = dis[tt] + 1;
                    if(dis[t] != -1) return true;
                }
                k = nex[k];
            }
        }
        return false;
    }
    int dfs(int s,int t,int low) {
        if(s == t) return low;
        int &k = work[s],x;
        while(k != -1) {
            if(dis[s] + 1 == dis[v[k]] && w[k ^ 1] && (x = dfs(v[k],t,min(low,w[k ^ 1])))) {
                w[k] += x;
                w[k ^ 1] -= x;
                return x;
            }
            k = nex[k];
        }
        return 0;
    }
    int dinic(int s,int t) {
        int maxflow = 0,subf = 0;
        while(bfs(s,t)) {
            for(int i = 0;i <= n;i ++) work[i] = first[i];
            while((subf = dfs(s,t,inf)) != 0) maxflow += subf;
        }
        return maxflow;
    }
    int main() {
        char s[4],t[4];
        memset(first,-1,sizeof(first));
        scanf("%s%s%d",s,t,&n);
        chan(s);chan(t);
        for(int i = 0;i < n;i ++) {
            scanf("%s%s%d",s,t,&f);
            int ss = chan(s),tt = chan(t);
            u[c] = ss,v[c] = tt,w[c] = 0;
            nex[c] = first[ss],first[ss] = c ++;
            u[c] = tt,v[c] = ss,w[c] = f;
            nex[c] = first[tt],first[tt] = c ++;
        }
        n = n * 2 + 2;
        printf("%d",dinic(1,2));
        return 0;
    }
  • 相关阅读:
    游戏UI框架设计(三) : 窗体的层级管理
    游戏UI框架设计(二) : 最简版本设计
    游戏UI框架设计(一) : 架构设计理论篇
    天启:如何从零开始建设数据中台? | 数智加速度04课回顾
    何夕:数据战略不仅是技术问题,更是业务和组织问题 | 数智加速度03课回顾
    才言:中台战略下,企业组织如何顺应商业变迁 | 数智加速度02回顾
    行在:数据中台的最终目的是给企业带来降本增效 | 数智加速度01课回顾
    腰部零售企业需要数据中台吗?
    硬核直播 | 全面解析数据中台,点燃「数智加速度」
    南京新百 × 奇点云 | 老字号百货购物中心运营的数智化转型破局
  • 原文地址:https://www.cnblogs.com/8023spz/p/12268842.html
Copyright © 2011-2022 走看看