pat-top 1003 Universal Travel Sites (最大流)

1003 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

作者: CHEN, Yue

单位: 浙江大学

时间限制: 200 ms

内存限制: 64 MB

代码长度限制: 16 KB

解题思路

把每个顶点看作26进制数,转化为int,然后就是最大流的模板题了

代码如下

#include <iostream>
#include <vector> 
#include <cmath>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
int to_int(string str)
{
	int ans = 0;
	for(int i = 0; i < str.size(); i ++){
		ans = ans * 26 + str[i] - 'A';
	}
	return ans;
}
int s, t;
struct Line{
	int r, w;
	Line() = default;
	Line(int r, int w): r(r), w(w){	}
}line[1005];
vector<int> g[30000];
int deep[30000];
bool bfs()
{
	queue<int> que;
	memset(deep, 0, sizeof(deep));
	que.push(s);
	deep[s] = 1;
	while(!que.empty()){
		int top = que.front();
		que.pop();
		for(int i = 0; i < g[top].size(); i ++){
			int z = g[top][i];
			if(!deep[line[z].r] && line[z].w){   //分层 
				deep[line[z].r] = deep[top] + 1;
				que.push(line[z].r);
				if(deep[t])
					return true;
			}
		}
	}
	return false;
}
int dfs(int x, int mix)
{
	if(x == t || !mix)
		return mix;
	int ap = 0;
	for(int i = 0; i < g[x].size(); i ++){
		int z = g[x][i];
		if(deep[x] < deep[line[z].r] && line[z].w){    //从低层到高层,要进过非饱和弧 
			int p = dfs(line[z].r, min(mix, line[z].w));
			ap += p;
			mix -= p;
			line[z].w -= p;
			line[z^1].w += p;
			if(!mix)
				return ap;
		}			
	}
	return ap;
}
int dinic()
{
	int ans = 0;
	while(bfs()){
		ans += dfs(s, INF);
	} 
	return ans;
}
int main()
{
	string str1, str2;
	int m;
	while(cin >> str1 >> str2 >> m){
		s = to_int(str1);
		t = to_int(str2);
		for(int i = 0; i < m; i ++){
			string strx, stry;
			int w;
			cin >> strx >> stry >> w;
			line[2 * i] = Line(to_int(stry), w);
			line[2 * i + 1] = Line(to_int(strx), 0);
			g[to_int(strx)].push_back(2 * i);
			g[to_int(stry)].push_back(2 * i + 1);
		}
		cout << dinic() << endl;
		for(int i = 0; i <= 30000; i ++)
			g[i].clear();
	}
	return 0;
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值