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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM