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

PAT顶级题解目录​​​​​​​

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

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值