POJ-1087-A Plug for UNIX(map+dinic,最大流)

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

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 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

题目大意:首先给出n个插座(可以被插),再给出m个电器(电器名称   插销),再给出k个转接口(str1,str2)使str1接口能插到str2上,问这些插座在转接口无限的情况下最多能够让多少电器插上电,输出剩余没有插电的电器数

分析:

很明显的网络流,但要注意插座可能重复,一个对应的权值为1,重复多少次,对应的权值为几。

根据样例,画图:

因为中间的转接器可以无限接,因此将他们的权值设为INF即可,

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 998244353
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);
struct node{
	int v,w,nxt;
}edge[30000];
int head[30000];
int dis[600];//BFS(分层 ) 
int pre[600];//bfs(找上级)
int cnta[600],cntb[600];
//char str[600][30];//map 
int n,m,k;
int e,s,t,num;
map<string,int> book;

void AddEdge(int u,int v,int w)
{
	edge[e].v=v;
	edge[e].w=w;
	edge[e].nxt=head[u];
	head[u]=e++;
	
	edge[e].v=u;
	edge[e].w=0;
	edge[e].nxt=head[v];
	head[v]=e++;
}

bool BFS()
{
    clean(dis,0);
    dis[s]=1;
    queue<int> que;
    que.push(s);
    while(que.size())
    {
    	int u=que.front();
    	que.pop();
		if(u==t)//结尾 
		    return 1;
		for(int i=head[u];i!=-1;i=edge[i].nxt)//遍历这个邻接表 
		{
		    if(edge[i].w&&dis[edge[i].v]==0)//该邻接表有值 && 没有被便利过 
			{
				dis[edge[i].v]=dis[u]+1;
				que.push(edge[i].v);
		    }
		}
    }
    return 0;
}

int DFS(int u,int sum)
{
    if(u==t)
		return sum;
    for(int i=head[u],t;i!=-1;i=edge[i].nxt)
    { 
		if(edge[i].w&&dis[edge[i].v]==dis[u]+1
		&&(t=DFS(edge[i].v,min(sum,edge[i].w))))
		{
		    edge[i].w-=t;
		    edge[i^1].w+=t;
		    return t;
		}
    } 
	return dis[u]=0;
}

int maxflow()
{
    int ans=0;
    while(BFS())//分层 
    {
    	int res=0;
		while(res=DFS(s,INF))
		    ans+=res;
    }
	return ans;
}

int main()
{
    while(cin>>n)
    {
		e=0; 
		num=1;//插销 种类数 
		book.clear();
		clean(head,-1);
		clean(cnta,0);
		clean(cntb,0);
		string str;
		for(int i=0;i<n;i++)
		{
		    cin>>str;
		    if(book[str])
		    	cnta[book[str]]++;
		    else
		    	cnta[book[str]=num++]++;
		}
		cin>>m;
		string str1;
		for(int i=0;i<m;i++)
		{
		    cin>>str>>str1;
		    if(book[str1])
		    	cntb[book[str1]]++;
		    else
		    	cntb[book[str1]=num++]++;
		}
		s=0,t=num++;
		for(int i=1;i<num;i++)//num种接口 ,第i种接口 
		{
		    if(cnta[i])//有这种接口的插座 
				AddEdge(i,t,cnta[i]);//第i种接口和 汇 相连通 
		    if(cntb[i])//有这种接口的插销 
				AddEdge(s,i,cntb[i]);//第 i种接口和 源 相连通 
		}
		cin>>k;
		for(int i=0;i<k;i++)
		{
		   	cin>>str>>str1;
			if(book[str]==0)
		   		book[str]=num++;
		   	if(book[str1]==0)
		   		book[str1]=num++;
		   	AddEdge(book[str],book[str1],INF);//相连通
		}
		cout<<m-maxflow()<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81843741