pat-top 1003. Universal Travel Sites (35)

https://www.patest.cn/contests/pat-t-practise/1003


参考了:

http://blog.sina.com.cn/s/blog_6cf509db0100uy5n.html

http://blog.csdn.net/zorelemn/article/details/50410853

http://blog.csdn.net/jtjy568805874/article/details/50759478


#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <climits>
#include <algorithm>
using namespace std;

/*
* Find a augmented path
*/
bool bfs(vector< vector<int> >& g,vector<int> &pre, int np, int src, int dist) {
	fill(pre.begin(),pre.end(), -1);
	queue<int> q;
	q.push(src);
	while (!q.empty()) {
		int t = q.front();
		q.pop();
		for (int i = 1; i < np; i++)
		{
			if (g[t][i] != 0 && pre[i] == -1) {
				pre[i] = t;
				if (i == dist) return true;
				q.push(i);
			}
		}
	}
	return false;
}

int maxFlow(vector<vector<int> > g, int np, int src, int dist) {
	int flow = 0, d;
	vector<int> pre(1005,-1);
	while (bfs(g, pre, np, src, dist)) {//find an augmented path
		d = INT_MAX;
		//get the max_flow of the augmented path
		for (int i = dist; i != src; i = pre[i])
		{
			d = min(d, g[pre[i]][i]);
		}
		for (int i = dist; i != src; i = pre[i])
		{
			g[pre[i]][i] -= d;
			g[i][pre[i]] += d;
		}
		flow += d;
	}
	return flow;

}
int main()
{
	int  n, c, np = 1;
	char a[4], b[4];
	map<string,int> idsM;
	vector< vector<int> > G(1005, vector<int>(1005, 0));
	scanf("%s %s %d", a, b, &n);
	if (!idsM[a]) idsM[a] = np++;
	if (!idsM[b]) idsM[b] = np++;
	for (int i = 0; i < n; i++)
	{
		scanf("%s %s %d", a, b, &c);
		if (!idsM[a]) idsM[a] = np++;
		if (!idsM[b]) idsM[b] = np++;
		G[idsM[a]][idsM[b]] = c;
	}
	int maxF = maxFlow(G,np,1,2);
	printf("%d\n", maxF);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值