1003 Universal Travel Sites (35 分)(C++)

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

題目大意:給出起點和終點,和相關的路徑,其中每條路徑有容量限制,現在要求能從起點運送到終點的最大容量,要保證不超過每條路徑的容量。

解題思路:最大流問題。節點的index用unordered_map進行映射。

關於最大流的幾種算法可以參考:

https://blog.csdn.net/smartxxyx/article/details/9293665

https://blog.csdn.net/CY05627/article/details/90552189

https://blog.csdn.net/jinli_/article/details/88563542

Edmonds_Karp算法

#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const int inf = 0x3f3f3f3f;
struct Edge{
    int from, to, cap, flow;
    Edge(int a, int b, int c, int d): from(a), to(b), cap(c), flow(d){}
}; 
unordered_map<string, int>Index;
vector<Edge> edges;
vector<int> graph[N];
int a[N], p[N]; // a記錄起點到每個節點的殘量,p最短路樹上到節點i的邊的序號

//在圖中添加邊,注意由a到b的邊序號爲i,則b到a的邊的序號爲i+1。在p[i]^1中藥用到這一點 
void addedge(string from ,string to,int cap){   
    if(Index.find(from)==Index.end())
        Index.insert({from,Index.size()});
    if(Index.find(to)==Index.end())
        Index.insert({to,Index.size()});
    int f=Index[from],t=Index[to];
    graph[f].push_back(edges.size());
    edges.push_back(Edge(f,t,cap,0));
    graph[t].push_back(edges.size());
    edges.push_back(Edge(t,f,0,0));
}

int Edmonds_Karp(int s, int t){
    int flow = 0;
    while(true){
        memset(a, 0, sizeof(a));
        queue<int> q;
        q.push(s);
        a[s] = inf;
        //BFS更新殘量 
        while(!q.empty()){
            int temp = q.front();
            q.pop();
            for(int x : graph[temp]){
                Edge &e = edges[x];
                if(a[e.to] == 0 && e.cap > e.flow){
                    p[e.to] = x;
                    a[e.to] = min(a[temp], e.cap - e.flow);
                    q.push(e.to);
                }
            }
            if(a[t] != 0)
                break;
        }
        if(a[t] == 0)
            break;
        for(int u = t; u != s; u = edges[p[u]].from){
            edges[p[u]].flow += a[t];
            edges[p[u]^1].flow -= a[t];
        }
        flow += a[t];
    }
    return flow; 
}
int main(){
    string from, to;
    int n, cap; 
    cin >> from >> to >> n;
    Index[from] = 0;
    Index[to] = 1;
    while(n--){
        cin >> from >> to >> cap;
        addedge(from,to,cap);
    }
    printf("%d",Edmonds_Karp(0,1));
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章