UVA_753_A Plug for UNIX_WA

</pre><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;
int count = 0;
class Edge
{
public:
	int from,to, flow, capacity;
	Edge(const int &v,const int &a, const int &c,const int &f)
	{
		from=v,to = a, capacity = c,flow=f;
	}
};
class Network
{
private:
	int vexnum, edgenum;//表示顶点个数和边个数
	int receptacle, device, adapter;//表示插座数,电器数,转换器种类
	vector<string>table_receptacle;
	vector<string>table_device;
	vector<pair<string,string>>table_adapter;
	int src, sink;//表示源点和汇点
	int max_flow;//最大流
	vector<Edge>edge;//边数的两倍
	vector<vector<int>>adjList;//邻接表
public:
	void addEdge(const int &from,const int &to,const int &capacity)
	{
		edge.push_back({from,to,capacity,0});
		adjList[from].push_back(edge.size()-1);
		edge.push_back({to,from,0,0});
		adjList[from].push_back(edge.size() - 1);
	}
	Network()
	{
		max_flow = 0;
		src = 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 });
		}
		//初始化源点,汇点,邻接表,顶点数
		src = 0, sink = device + receptacle + adapter+1, vexnum = 1 + sink;
		adjList.resize(vexnum);
		//连边
		for (int i = 1;i<=device;i++)
		{
			addEdge(src, 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, sink, 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()
	{
		vector<int>path(vexnum);
		while (1)
		{
			vector<int>augument(vexnum);
			queue<int>Q;
			Q.push(src);
			augument[src] =infinity;
			while (!Q.empty())
			{
				auto x = Q.front();
				Q.pop();
				for (int i = 0; i < adjList[x].size(); i++)
				{
					Edge&e = edge[adjList[x][i]];
					if (!augument[e.to]&&e.capacity>e.flow)
					{
						path[e.to] = adjList[x][i];
						augument[e.to] = std::min(augument[x],e.capacity-e.flow);
						Q.push(e.to);
					}
				}
				if (augument[sink])
				{
					break;
				}
			}
			if (!augument[sink])
			{
				break;
			}
			for (auto u = sink;u!=src;u=edge[path[u]].from)
			{
				edge[path[u]].flow += augument[sink];
				edge[path[u]^1].flow-= augument[sink];
			}
			max_flow += augument[sink];
		}
	}
	void print()
	{
		cout << device-max_flow << endl;
		count++;
	}
};
int main()
{
	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n; cin >> n;
	while (n--)
	{
		Network network;
		network.getMaxFlow();
		network.print();
		if (n)
		{
			cout << endl;
			count++;
		}
	}
	return 0;
}


你可能感兴趣的:(UVA_753_A Plug for UNIX_WA)