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

UVA - 753 A Plug for UNIX

时间:2015-03-16 21:22:49      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

 Status

Description

技术分享

You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information and ideas on the Internet as cumbersome and bureaucratic as possible.


Since the room was designed to accommodate reporters and journalists from around the world, it is equipped with electrical receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed when the room was built. Unfortunately, the room was built many years ago when reporters used very few electric and electronic devices and is equipped with only one receptacle of each type. These days, like everyone else, reporters require many such devices to do their jobs: laptops, cell phones, tape recorders, pagers, coffee pots, microwave ovens, blow dryers, curling irons, tooth brushes, etc. Naturally, many of these devices can operate on batteries, but since the meeting is likely to be long and tedious, you want to be able to plug in as many as you can.


Before the meeting begins, you gather up all the devices that the reporters would like to use, and attempt to set them up. You notice that some of the devices use plugs for which there is no receptacle. You wonder if these devices are from countries that didn‘t exist when the room was built. For some receptacles, there are several devices that use the corresponding plug. For other receptacles, there are no devices that use the corresponding plug.


In order to try to solve the problem you visit a nearby parts supply store. The store sells adapters that allow one type of plug to be used in a different type of outlet. Moreover, adapters are allowed to be plugged into other adapters. The store does not have adapters for all possible combinations of plugs and receptacles, but there is essentially an unlimited supply of the ones they do have.

Input 

The input will consist of several case. The first line of the input contains the number of cases, and it‘s followed bya blank line. The first line of each case contains a single positive integer n ( 技术分享) indicating the number of receptacles in the room. The next n lines list the receptacle types found in the room. Each receptacle type consists of a string of at most 24 alphanumeric characters. The next line contains a single positive integer m ( 技术分享) indicating the number of devices you would like to plug in. Each of the next m lines lists the name of a device followed by the type of plug it uses (which is identical to the type of receptacle it requires). A device name is a string of at most 24 alphanumeric characters. No two devices will have exactly the same name. The plug type is separated from the device name by a space. The next line contains a single positive integer k ( 技术分享) indicating the number of different varieties of adapters that are available. Each of the next k lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed by a space, followed by the type of plug.

There‘s a blank line between test cases.

Output 

For each case, print a line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in. Print a blank line between cases.

Sample Input 

1

4
A
B
C
D
5
laptop B
phone C
pager B
clock B
comb X
3
B X
X A
X D

Sample Output 

1



Miguel A. Revilla
2000-02-09

Source

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 7. Graph Algorithms and Implementation Techniques
Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 4. Graph :: Special Graph - Bipartite Graph :: Max Cardinality Bipartite Matching
Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 11. Graph Theory :: Examples
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Graph :: Maximum Flow :: Standard
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Graph :: Special Graphs (Others) :: Bipartite Graph


题意:输入n,有n个插座,下面n行是每个插座的类型(最后24个字母来表示一个插座,没有空格放心用scanf,但是有可能插座会相同,但是这个没有什么影响)

         输入m,有m个电器,下面m行每行两个单词分别是电器的名字和插头类型(同样24个字母单词内没空格,两个单词空格隔开)

         输入k,有k个转换器,下面k行每行两个单词,分别表示转换器的入口类型和插头类型

每种转换器的个数是无限的,转换器本身可以与转换器相连

要你求,让最多的电器能够插在插座上(可以用转换器辅助也可以直接插上去),输出不能插上去的电器的数量

s与所有电器建一条有向边,容量为1,所有插座与汇点建一条有向边,容量为1。对于每个电器,如果能直接和插座相连的,和每个能相连的插座建一条有向边,容量为1.另外,所有电器和所有能连接上的转换器建一条有向边,容量为1,转换器和转换器之间能相连的建一条有向边容量为INF,转换器和插座能相连建一条有向边容量为1。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int head[int(1e3)],tail;
struct Edge
{
	int from,to,next,cap,flow;
}edge[int(1e6)];
void add(int from,int to,int cap,int flow)
{
	edge[tail].from=from;
	edge[tail].to=to;
	edge[tail].cap=cap;
	edge[tail].flow=flow;
	edge[tail].next=head[from];
	head[from]=tail++;
}
int up[int(1e3)],path[int(1e3)];
int work(int ed)
{
	int ans=0;
	while(1)
	{
		queue<int>qq;
		memset(up,0,sizeof(up));
		memset(path,-1,sizeof(path));
		up[0]=INT_MAX;
		qq.push(0);	
		while(qq.size())
		{
			int from=qq.front();
			qq.pop();
			for(int i=head[from];i!=-1;i=edge[i].next)
				if(up[edge[i].to]==0&&edge[i].cap>edge[i].flow)
				{
					path[edge[i].to]=i;
					qq.push(edge[i].to);
					up[edge[i].to]=min(up[from],edge[i].cap-edge[i].flow);
				}
		}
		if(up[ed]==0)
			return ans;
		ans+=up[ed];
		for(int i=path[ed];i!=-1;i=path[edge[i].from])
		{
			edge[i].flow+=up[ed];
			edge[i^1].flow-=up[ed];
		}
	}
}
string rp[110],dv[110][2],ap[110][2];
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		for(int i=0;i<n;i++)
			cin>>rp[i];
		int m;
		cin>>m;
		for(int i=0;i<m;i++)
			for(int j=0;j<2;j++)
				cin>>dv[i][j];
		int k;
		cin>>k;
		for(int i=0;i<k;i++)
			for(int j=0;j<2;j++)
				cin>>ap[i][j];
		tail=0;
		memset(head,-1,sizeof(head));
		for(int i=0;i<m;i++)
		{
			add(0,n+i+1,1,0);
			add(n+i+1,0,0,0);
			for(int j=0;j<n;j++)
				if(dv[i][1]==rp[j])
				{
					add(n+1+i,1+j,1,0);
					add(1+j,n+1+i,0,0);
				}
			for(int j=0;j<k;j++)
				if(dv[i][1]==ap[j][0])
				{
					add(n+1+i,n+m+1+j,1,0);
					add(n+m+1+j,n+1+i,0,0);
				}
		}
		for(int i=0;i<k;i++)
		{
			for(int j=0;j<k;j++)
				if(ap[i][1]==ap[j][0])
				{
					add(n+m+1+i,n+m+1+j,INT_MAX,0);
					add(n+m+1+j,n+m+1+i,0,0);
				}
			for(int j=0;j<n;j++)
				if(ap[i][1]==rp[j])
				{
					add(n+m+1+i,1+j,1,0);
					add(1+j,n+m+1+i,0,0);
				}
		}
		for(int i=0;i<n;i++)
		{
			add(1+i,n+m+k+1,1,0);
			add(n+m+k+1,1+i,0,0);
		}
		cout<<m-work(n+m+k+1)<<endl;
		if(T)
			cout<<endl;
	}
}



UVA - 753 A Plug for UNIX

标签:

原文地址:http://blog.csdn.net/stl112514/article/details/44310745

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