[POJ1087]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 Description
|
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 |
输出描述 Output Description
|
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
|
数据范围及提示 Data Size & Hint
|
题目中说了!
|
此题最大的难点在于能否把题读懂。读懂之后就很显然是一道最大流的题。我觉得这道题建图里面的点的设计很妙。字符串处理,一个find函数就能找到对应编号,不错!
#include<iostream> #include<cmath> #include<algorithm> #include<cstring> #include<cstdio> #include<queue> #include<string> using namespace std; typedef long long LL; #define mem(a,b) memset(a,b,sizeof(a)) inline int read() { int x=0,f=1;char c=getchar(); while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; } const int maxn=1010,maxm=2000010,oo=2147483647,maxs=110; int n,mm,k,h1[maxs],h3[maxs],h4[maxs]; string str1[maxs],str2[maxs],str3[maxs],str4[maxs],str5[maxs],list[5*maxs]; struct Edge { int u,v,f,next; Edge() {} Edge(int _1,int _2,int _3,int _4):u(_1),v(_2),f(_3),next(_4) {} }e[2*maxm]; struct Dinic { int first[maxn],dis[maxn],cur[maxn],a,b,c,ce,s,t,N; bool vis[maxn];queue <int> Q; void addEdge(int a,int b,int c) { e[++ce]=Edge(a,b,c,first[a]);first[a]=ce; e[++ce]=Edge(b,a,0,first[b]);first[b]=ce; } int find(string a) { for(int i=3;i<=N;i++)if(a==list[i])return i; list[++N]=a;return N; } void init_build() { mem(first,-1);ce=-1; s=1;t=2;N=2; for(int i=1;i<=n;i++) { list[++N]=str1[i]; addEdge(s,i+2,1); } for(int i=1;i<=mm;i++) { list[++N]=str2[i]; int t1=find(str5[i]); addEdge(t1,N,1);addEdge(N,t,1); } for(int i=1;i<=k;i++) { int t1=find(str3[i]),t2=find(str4[i]); addEdge(t2,t1,oo); } } bool BFS() { mem(dis,42);mem(vis,0); while(Q.size())Q.pop(); dis[s]=0;vis[s]=1;Q.push(s); while(Q.size()) { int now=Q.front();Q.pop(); for(int i=first[now];i!=-1;i=e[i].next) if(e[i].f && !vis[e[i].v]) { vis[e[i].v]=1; dis[e[i].v]=dis[now]+1; Q.push(e[i].v); } } return vis[t]; } int dfs(int x,int a) { if(x==t || a==0)return a; int flow=0,tmp; for(int& i=cur[x];i!=-1;i=e[i].next) if(dis[e[i].v]==dis[x]+1 && (tmp=dfs(e[i].v,min(a,e[i].f)))>0) { e[i].f-=tmp;e[i^1].f+=tmp; a-=tmp;flow+=tmp; if(a==0)break; } return flow; } int maxflow() { int flow=0; while(BFS()) { for(int i=1;i<=N;i++)cur[i]=first[i]; flow+=dfs(s,oo); } return flow; } }fyh; int main() { n=read(); for(int i=1;i<=n;i++)cin>>str1[i]; mm=read(); for(int i=1;i<=mm;i++)cin>>str2[i]>>str5[i]; k=read(); for(int i=1;i<=k;i++)cin>>str3[i]>>str4[i]; fyh.init_build(); printf("%d\n",mm-fyh.maxflow()); return 0; }