PAT (Top Level) Practise 1003. Universal Travel Sites (35)

1003. Universal Travel Sites (35)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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:

sourcei destinationi capacityi

where sourcei and destinationi are the names of the satellites and the two involved planets, and capacityi > 0 is the maximum number of passengers that can be transported at one pass from sourcei to destinationi. 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
#include<iostream>
#include<map>
#include<string>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 510 * 2;
const int inf = 0x3f3f3f3f;
int n, tot = 3, ss = 1, dd = 2;
int e[maxn][maxn], Layer[maxn];
bool vis[maxn];
bool CountLayer() {
	queue<int> q;
	memset(Layer, 0xff, sizeof(Layer));
	q.push(ss);
	Layer[ss] = 0;
	while (!q.empty()) {
		int u = q.front(); q.pop();
		for(int i=1;i<tot;++i)
			if (Layer[i] == -1 && e[u][i] > 0) {
				Layer[i] = Layer[u] + 1;
				if (i == dd) return true;
				else q.push(i);
			}
	}
	return false;
}
int Dinic() {
	int flow = 0;
	deque<int> q;
	while (CountLayer()) {
		q.push_back(ss);
		memset(vis, false, sizeof(vis));
		vis[ss] = true;
		int i;
		while (!q.empty()) {
			int nd = q.back();
			if (nd == dd) {
				int minn = inf, minst;
				for (int i = 1; i < q.size(); ++i) {
					int vs = q[i - 1], ve = q[i];
					if (e[vs][ve] > 0 && e[vs][ve] < minn) {
						minn = e[vs][ve];
						minst = vs;
					}
				}
				flow += minn;
				for (int i = 1; i < q.size(); ++i) {
					e[q[i - 1]][q[i]] -= minn;
					e[q[i]][q[i - 1]] += minn;
				}
				while (!q.empty() && q.back()!= minst) {
					vis[q.back()] = false;
					q.pop_back();
				}
			}
			else {
				for (i = 1; i <tot; ++i)
					if (e[nd][i]>0 && Layer[i] == Layer[nd] + 1 && !vis[i]) {
						vis[i] = true;
						q.push_back(i);
						break;
					}
				if (i>=tot)q.pop_back();
			}
		}
	}
	return flow;
}
int main() {
	ios::sync_with_stdio(false);
	map<string, int> getid;
	string st, des;
	cin >> st >> des >> n;
	getid[st] = 1; getid[des] = 2;
	for (int i = 0; i < n; ++i) {
		string a, b;
		int d;
		cin >> a >> b >> d;
		if (!getid[a])getid[a] = tot++;
		if (!getid[b])getid[b] = tot++;
		e[getid[a]][getid[b]] += d;
	}
	cout << Dinic() << "\n";
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值