POJ1087——建图+建图+最大匹配(最大流)

题目链接:http://poj.org/problem?id=1087

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 one case. The first line contains a single positive integer n (1 <= n <= 100) 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 (1 <= m <= 100) 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 (1 <= k <= 100) 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.

Output

A line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in.

Sample Input

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

题目翻译:

AS WE ALL KNOW,最近哈理工出现了一起着火事件,原因是用了杂牌的插座,有着强烈安全意识的鑫爹急忙为G808换上了最牛B的红牛插座。假设他买了n种插座,G808有m个电器,分别有自己可以插的插座。由于鑫爹的插座非常牛B,可以相互转换,接下来给出k行说明S1插座和S2插座把S2换成S1,那么最少有几个电器没有插座用呢?

‎输入‎

‎输入将由一个案例组成。第一行包含单个正整数 n (1 <= n <= 100),指示房间中的插座数量。下一行列出了在房间中找到的插座类型。每个插座类型由最多 24 个字母数字字符的字符串组成。下一行包含单个正整数 m(1 <= m <= 100),指示要插入的设备数。接下来的每行都列出设备的名称,然后是它使用的插头类型(与所需的插座类型相同)。设备名称是最多包含 24 个字母数字‎
‎字符的字符串。没有两台设备的名称完全相同。插头类型与设备名称用空格分隔。下一行包含单个正整数 k (1 <= k <= 100),指示可用的不同种类的适配器的数量。接下来的 k 行描述各种适配器,提供适配器提供的插座类型,后跟空格,然后是插头类型。‎

‎输出‎

‎包含单个非负整数的行,指示无法插入的设备数最小。‎

 

比较经典的建图问题,题目要求无法插入的最小设备数,其实就是让你求最大的能插入设备数,就是建个图求最大流。

至于怎么建图呢,总的思路就是源点->插座->插头->设备->汇点,源点(可以取0)和插座的边权值为1,表示有一个插座,

插座和插头的权值为INF,因为题目给了,插座和插头的供应无限,然后是插头和设备的边权值为1,表示一个设备对应一个插头,然后设备和汇点权值为1,表示一个设备。

因为这个题的输入有点坑,我们这里可以妙用STL中的map,其实最大流的点的编号不是这么关键,一般取的时候只要能把关系定了,编号没太大关系,为了好写,我们把0设为源点,1设为汇点。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
const int INF = 0x7ffffff;
map<string,int> hash;
int G[507][507];
int pre[507],visited[507];
int n,S,T;
int EK(){
	queue<int> q;
	int ans=0;
	while(1){
		memset(visited,0,sizeof(visited));
		memset(pre,-1,sizeof(pre));
		while(!q.empty()) q.pop();
		visited[0]=1;
		q.push(0);
		while(!q.empty()){
			int now=q.front();
			q.pop();
			if(now==1) break;
			for(int i = 0;i<=n;++i){
				if(!visited[i]&&G[now][i]>0){
					pre[i]=now;
					visited[i]=1;
					q.push(i);
				}
			}
		}
		if(!visited[T]) break;
		int minn=INF;
		for(int i = T;i!=0;i=pre[i])
			minn=min(minn,G[pre[i]][i]);
		ans+=minn;
		for(int i = T;i!=0;i=pre[i]){
			G[pre[i]][i]-=minn;
			G[i][pre[i]]+=minn;
		}
	}
	return ans;
}
int main(int argc, char** argv) {
	cin>>n;
	hash.clear();
	S=0,T=1;
	int total=2;
	while(n--){
		string str;
		cin>>str;
		hash[str]=total++;//给插座,插头,设备编号。 
		G[0][hash[str]]=1; 
	}
	int m;
	cin>>m;
	for(int i = 1;i<=m;++i){
		string str1,str2;
		cin>>str1>>str2;
		if(!hash[str1]) hash[str1]=total++;
		if(!hash[str2]) hash[str2]=total++;
		G[hash[str1]][T]=1;
		G[hash[str2]][hash[str1]]=1;
 	}
 	int k;
 	cin>>k;
 	while(k--){
 		string str1,str2;
 		cin>>str1>>str2;
 		if(!hash[str1]) hash[str1]=total++;
		if(!hash[str2]) hash[str2]=total++;
		G[hash[str2]][hash[str1]]=INF;
	}
	n=total-1;
	printf("%d\n",m-EK());
	return 0;
}

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值