POJ 1087 A Plug for UNIX (最大流)

it2024-07-10  7

A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K   

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分析建图是比较好想的,源点和插头相连,插头和电器相连,电器和汇点相连,跑最大流就行了。只是要注意一个地方:后面可能会出现前面没有出现的插座,需要加进去,这样就行了。对于这种字符串的,还是map比较好用。 #include<stdio.h> #include<string.h> #include<iostream> #include<queue> #include<map> #include<string> using namespace std; //**************************************************** //最大流模板Edmonds_Karp算法 //初始化:G[][],st,ed //****************************************************** const int MAXN = 500; const int INF = 0x3fffffff; int G[MAXN][MAXN];//存边的容量,没有边的初始化为0 int path[MAXN],flow[MAXN],st,ed; int n;//点的个数,编号0~n,n包括了源点和汇点 queue<int>q; int bfs() { int i,t; while(!q.empty()) q.pop();//清空队列 memset(path,-1,sizeof(path));//每次搜索前都把路径初始化成-1 path[st]=0; flow[st]=INF;//源点可以有无穷的流流进 q.push(st); while(!q.empty()){ t=q.front(); q.pop(); if(t==ed) break; for(i=0;i<=n;i++){ if(i!=st&&path[i]==-1&&G[t][i]){ flow[i]=flow[t]<G[t][i]?flow[t]:G[t][i]; q.push(i); path[i]=t; } } } if(path[ed]==-1) return -1;//即找不到汇点上去了。找不到增广路径了 return flow[ed]; } int Edmonds_Karp() { int max_flow=0; int step,now,pre; while((step=bfs())!=-1){ max_flow+=step; now=ed; while(now!=st){ pre=path[now]; G[pre][now]-=step; G[now][pre]+=step; now=pre; } } return max_flow; } map<string,int>mp; int main() { int N,M,K; st=0,ed=1; int tol=2; memset(G,0,sizeof(G)); scanf("%d",&N); while(N--){ string s; cin>>s; mp[s]=tol; G[st][tol]=1; tol++; } scanf("%d",&M); for(int i=1;i<=M;i++){ string name,s; cin>>name>>s; if(mp[name]==0) mp[name]=tol++; if(mp[s]==0) mp[s]=tol++; G[mp[name]][ed]=1; G[mp[s]][mp[name]]=1; } scanf("%d",&K); while(K--){ string s1,s2; cin>>s1>>s2; if(mp[s1]==0) mp[s1]=tol++; if(mp[s2]==0) mp[s2]=tol++; G[mp[s2]][mp[s1]]=INF; } n=tol-1; printf("%d\n",M-Edmonds_Karp()); return 0; }

 

 

转载于:https://www.cnblogs.com/wangdongkai/p/5618868.html

相关资源:数据结构—成绩单生成器
最新回复(0)