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;
}
原文地址:https://www.cnblogs.com/8023spz/p/12268842.html