博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【poj1087/uva753】A Plug for UNIX(最大流)
阅读量:4624 次
发布时间:2019-06-09

本文共 4925 字,大约阅读时间需要 16 分钟。

A Plug for UNIX
 

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

 
 

 

 

  最大流或者二分图匹配都能做。

  我做的是最大流~~

  建图->st连插头流量为这种插头数量
  插座连ed流量为这种插座的数量
  对于插头的转换,就在左边的图连x->y,流量为正无穷
  最后计算最大流。
  不会用map的我,搞编号搞了一辈子,呵呵~

 

代码如下:

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 #define INF 0xfffffff 9 // #define Maxl 100010 10 #define Maxn 510 11 12 int n,m,k; 13 14 struct node 15 { 16 int x,y,f,o,next; 17 }t[Maxn*10*10];int len; 18 int first[Maxn],st,ed; 19 20 int mymin(int x,int y) { return x
%d :%d \n",t[i].x,t[i].y,t[i].f); 98 }*/ 99 }100 101 int dis[Maxn];102 queue
q;103 bool bfs()104 {105 while(!q.empty()) q.pop();106 memset(dis,-1,sizeof(dis));107 q.push(st);dis[st]=0;108 while(!q.empty())109 {110 int x=q.front();q.pop();111 for(int i=first[x];i;i=t[i].next) if(t[i].f>0)112 {113 if(dis[t[i].y]==-1)114 {115 q.push(t[i].y);116 dis[t[i].y]=dis[x]+1;117 }118 }119 }120 if(dis[ed]==-1) return 0;121 return 1;122 }123 124 int ffind(int x,int flow)125 {126 int now=0;127 if(x==ed) return flow;128 for(int i=first[x];i;i=t[i].next) 129 if(t[i].f>0&&dis[t[i].y]==dis[x]+1)130 {131 int a=ffind(t[i].y,132 mymin(flow-now,t[i].f));133 now+=a;134 t[i].f-=a;135 t[t[i].o].f+=a;136 if(now==flow) break;137 }138 if(now==0) dis[x]=-1;139 return now;140 }141 142 void max_flow()143 {144 int ans=0;145 while(bfs()) ans+=ffind(st,INF);146 printf("%d\n",m-ans);147 }148 149 int main()150 {151 int T;152 scanf("%d",&T);153 while(T--)154 {155 init();156 bfs();157 max_flow();158 if(T!=0) printf("\n");159 }160 return 0;161 }
[UVA753]

 

2016-07-15 14:26:32

 

转载于:https://www.cnblogs.com/Konjakmoyu/p/5673430.html

你可能感兴趣的文章
Cookie是可以覆盖的,如果重复写入同名的Cookie,那么将会覆盖之前的Cookie。
查看>>
Django Models的数据类型
查看>>
博客之初体验-----python初了解
查看>>
jquery.fileupload插件 ie9下不支持上传
查看>>
6.1 HTML5的框架
查看>>
Nginx的500,502,504错误解决方法
查看>>
SAP MM MM17里不能修改物料主数据'Purchasing Value Key'字段值?
查看>>
Revel示例 - 验证
查看>>
怪物,托管玩家的设计基本思路
查看>>
.Net Remoting
查看>>
HTML基础入门
查看>>
生成器和生成器表达式
查看>>
C#中数组、ArrayList和List三者的区别
查看>>
MVC4 WEBAPI(一)使用概述
查看>>
推荐给非互联网主体的用户
查看>>
sublime3 sftp 忽略文件的写法
查看>>
42. Multiply Strings
查看>>
Socket 基于完整CS样式例子(转载)
查看>>
C++ 概念易错点
查看>>
【顶】在node环境下玩转less
查看>>