码迷,mamicode.com
首页 > 其他好文 > 详细

UVA_753_A Plug for UNIX_WA

时间:2016-05-12 17:18:25      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

<pre name="code" class="cpp">#include<iostream>  
#include<sstream>  
#include<string>  
#include<vector>  
#include<list>  
#include<set>  
#include<map>
#include<stack>  
#include<queue>  
#include<algorithm>  
#include<numeric>  
#include<cmath>  
#pragma warning(disable:4996)  
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
const int infinity =1000000000;
class Network
{
private:
	int vexnum, edgenum, maxflow, source, destination;
	vector<vector<int>>flow, capacity;
	int receptacle, device, adapter;//表示插座数,电器数,转换器种类  
	vector<string>table_receptacle;
	vector<string>table_device;
	vector<pair<string, string>>table_adapter;
public:
	void addEdge()
	{
		int from, to, c; cin >> from >> to >> c;
		capacity[from - 1][to - 1] += c;
	}
	void addEdge(const int &from,const int &to,const int &c)
	{
		capacity[from][to] = c;
	}
	Network()
	{
		source = 0;//源点设置成0  
		//接受插座  
		cin >> receptacle; table_receptacle.resize(1);
		for (int i = 0; i < receptacle; i++)
		{
			string type; cin >> type;//插座的类型  
			table_receptacle.push_back(type);
		}
		//接受电器  
		cin >> device; table_device.resize(1);
		for (int i = 0; i < device; i++)
		{
			string name, type; cin >> name >> type;
			table_device.push_back(type);
		}
		//接受适配器  
		cin >> adapter; table_adapter.resize(1);
		for (int i = 0; i < adapter; i++)
		{
			string type1, type2; cin >> type1 >> type2;
			table_adapter.push_back({ type1,type2 });
		}
		//初始化源点,汇点,邻接表,顶点数  
		source = 0, destination = device + receptacle + adapter + 1, vexnum = 1 + destination;
		flow.resize(vexnum, (vector<int>)vexnum), capacity.resize(vexnum, (vector<int>)vexnum);
		//连边  
		for (int i = 1; i <= device; i++)
		{
			addEdge(source, i, 1);
			for (int j = 1; j <= receptacle; j++)
			{
				if (table_device[i] == table_receptacle[j])
				{
					addEdge(i, device + j, 1);
				}
			}
			for (int j = 1; j <= adapter; j++)
			{
				if (table_device[i] == table_adapter[j].first)
				{
					addEdge(i, device + receptacle + j, infinity);
				}
			}
		}
		for (int i = 1; i <= receptacle; i++)
		{
			addEdge(device + i, destination, 1);
			for (int j = 1; j <= adapter; j++)
			{
				if (table_receptacle[i] == table_adapter[j].second)
				{
					addEdge(device + receptacle + j, device + i, 1);
				}
			}
		}
		for (int i = 1; i <= adapter; i++)
		{
			for (int j = 1; j <= adapter; j++)
			{
				if (i == j)
				{
					continue;
				}
				if (table_adapter[i].first == table_adapter[j].second)
				{
					addEdge(device + receptacle + j, device + receptacle + i, infinity);
				}
			}
		}
	}
	void getMaxFlow_EK() 
	{
		maxflow = 0;
		queue<int> Q;
		while (!Q.empty())
		{
			Q.pop();
		}
		vector<int>path(vexnum);
		while (true) 
		{
			vector<int>remnant(vexnum);//残余流量得变0,一开始所有点都没流入对吧
			remnant[source] = infinity;   //源点嘛,剩余流量无限是必须的...
			Q.push(source);    //从源点开始进行BFS找增广路
			int from, to;
			while (!Q.empty())
			{
				from = Q.front();Q.pop();
				for (to = 0; to < vexnum; to++)//遍历所有点,找可行边
				{
					if (!remnant[to] && capacity[from][to]>flow[from][to])
					{    //该点剩余流量为0 且 容量大于流量,也就是找到了新的结点 
						path[to] = from;   //找到新结点,父节点得记录一下吧
						Q.push(to);
						remnant[to] = std::min(remnant[from], capacity[from][to] - flow[from][to]);    //如果u的剩余流量能填满uv就填满,不能的话就把u这点的流量全部流向uv 
					}
				}
			}
			if (remnant[destination] == 0)     //如果当前已经是最大流,汇点没有残余流量
			{
				break;
			}
			for (from = destination; from != source; from = path[from]) 
			{   //如果还能增广,那么回溯,从汇点往回更新每条走过的边的流量
				flow[path[from]][from] += remnant[destination];  //更新正向流量   (注意这里更新的是流量,而不是容量)
				flow[from][path[from]] -= remnant[destination];  //更新反向流量
			}
			maxflow += remnant[destination];
		}
	}
	void print()
	{
		cout << device - maxflow << endl ;
	}
};
int main()
{
	//freopen("input.txt", "r", stdin);  
	//freopen("output.txt", "w", stdout);  
	int n; cin >> n;
	while (n--)
	{
		Network network;
		network.getMaxFlow_EK();
		network.print();
		if (n)
		{
			cout << endl;
		}
	}
	return 0;
}


UVA_753_A Plug for UNIX_WA

标签:

原文地址:http://blog.csdn.net/cxy7tv/article/details/51363801

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!