POJ1087 A Plug for UNIX 网络最大流

求出最大流后,与总电器数量相减就可以得出答案。
建图:每一个插头和源点连一条容量为1的弧(数量为1),每一个用电器与汇点连一个容量为1的弧(此处可能出现未出现的插头种类,要先记下给予他一个编号,下面可能出现相应的转换器),然后转换器(提供的插头,转换器需要的插头),将转换器需要的插头到提供的插头连一条容量为INF的弧(此处会出现未出现的插头种类,注意记录,给与编号。INF是因为一种转换器不止一个。)
题外话、 昨天有人做题的时候ISAP被卡了,估计是没写好,所以试着写了下DINIC留作预备模板。
A Plug for UNIX
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11598 Accepted: 3818

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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<utility>
#include<string>
#include<map>

using namespace std;

#define MAXN 555
#define MAXM 20000
#define INF 0xFFFFFF

typedef map<string , int>  msi;
struct edge
{
	int to,c,next;
};
edge e[MAXM];
int que[MAXN*100],dis[MAXN],pre[MAXN];
int head[MAXN],head2[MAXN];
int st,ed,maxflow;
int en,n,m;
msi a,b;

void add(int a,int b,int c)
{
	e[en].to=b;
	e[en].c=c;
	e[en].next=head[a];
	head[a]=en++;
	e[en].to=a;
	e[en].c=0;
	e[en].next=head[b];
	head[b]=en++;	
} 

bool bfs() //构建层次网络
{
	memset(dis,-1,sizeof(dis));
	que[0]=st,dis[st]=1;
	int t=1,f=0;
	while(f<t)
	{
		int j=que[f++];
		for(int k=head[j];k!=-1;k=e[k].next)
		{	
			int i=e[k].to;
			if(dis[i]==-1 && e[k].c)
			{
				que[t++]=i;
				dis[i]=dis[j]+1;
				if(i==ed) return true;
			}
		}
	}
	return false; 
} 

int update()  
{  
	int p,flow=INF;  
    for (int i=pre[ed];i!=-1;i=pre[i])
		if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;    
    for (int i=pre[ed];i!=-1;i=pre[i]) 
		e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;   
    maxflow+=flow; 
    return p;
}  

void dfs()  
{  
	memset(pre,-1,sizeof(pre));
	memcpy(head2,head,sizeof(head2));  
    for(int i=st,j;i!=-1;)  
    {  
        int flag=false;  
        for(int k=head[i];k!=-1;k=e[k].next)    
          if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )  
          {  
                pre[j]=i; 
				head2[i]=k;  
				i=j; 
				flag=true;  
                if(i==ed) 
					i=update();  
                if(flag) 
					break;  
          }  
        if (!flag) dis[i]=-1,i=pre[i];   
    }  
} 

void solve()
{
	int counts=0;
	st=0,ed=500;
	en=0;
	string s,ss;
	a.clear(),b.clear();
	//scanf("%d",&n);
	memset(head,-1,sizeof(head));
	for(int i=1;i<=n;i++)
	{
		cin>>s;
		if(!a.count(s))
			a[s]=++counts;
		add(st,a[s],1);
	}
	scanf("%d",&n);
	m=n;
	for(int i=1;i<=n;i++)
	{
		cin>>s>>ss;
		if(!b.count(s))
			b[s]=++counts;
		if(!a.count(ss))
			a[ss]=++counts;
		add(b[s],ed,1);
		add(a[ss],b[s],1);
	}
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		cin>>s>>ss;
		if(!a.count(s))
			a[s]=++counts;
		if(!a.count(ss))
			a[ss]=++counts;
		add(a[ss],a[s],INF);
	}
	maxflow=0;
	while(bfs())
		dfs();
	printf("%d\n",m-maxflow);
	
	
}

int main()
{
	while(scanf("%d",&n)!=EOF)	solve();
	return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值