http://poj.org/problem?id=1087
Description
Charge of setting up the Press hostel for the inaugural meeting of the All Nations Internet eXecutive (UNIX), which has a international mandate to make the free flow of information and ideas on the Internet as cumbersome and bureau Cratic as possible.
Since the designed to accommodate reporters and journalists from around the world, it's equipped with electrical Receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed wh En the hostel was built. Unfortunately, the built many years ago when reporters used very few electric and electronic devices and is equip PED 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 was likely to being long and tedious, you wan T to is able to plug in as many as can.
Before the meeting begins, you gather up all the devices so the reporters would like-to-use, and attempt-to-set them up. You notice this some of the devices use plugs for which there are no receptacle. You wonder if these devices is from countries this didn ' t exist when the the hostel was built. For some receptacles, there is several devices that use the corresponding plug. For other receptacles, there is 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 is allowed to being plugged into other adapters. The store does not has adapters for all possible combinations of plugs and receptacles, but there was essentially an unlim ited supply of the ones they do has.
Input
The input would consist of one case. The first line contains a, positive integer n (1 <= n <=) indicating the number of receptacles in the . The next n lines list the receptacle types found in the. Each receptacle type consists of a string of in most alphanumeric characters. The next line contains a single positive integer m (1 <= m <=) indicating the number of devices your would like T o plug in. Each of the next m lines lists the name of a device followed by the type of plug it uses (which are identical to the type O F receptacle it requires). A device name is a string of in most alphanumeric
Characters. No. Devices would 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 <=) indicating the number of different varieties of a Dapters that is available. Each of the next K lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed B Y 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 is plugged in.
Sample Input
Sample Output
1
/**POJ 1087 Network Maximum flow problem: (Ideas from the online) This topic is too difficult to understand, but the topic meaning to make clear after the more good to do in this topic there are two kinds of items, one is a socket, an electrical socket only one jack and a plug, electrical only one plug first there are n kinds of sockets , n kinds of sockets with a string representation, this n kinds of sockets can be understood to be plugged into the power of the socket and then have m electrical appliances, now electrical to charge, electrical appliances with a string, each appliance has its own plug sockets (this socket can not be the N plug in the power outlet, can be other sockets) now have K information S1 S2 on behalf of the S1 socket can be plugged into the S2 socket, which is similar to the plug conversion of these S1 and S2 can also not that n plug in the power supply of these information to ask you how many appliances do not have a socket can be used to solve the problem: to build a source point, pointing to all electrical appliances, Capacity 1 All appliances point to the plug they can plug, the capacity is 1 if a plug can be plugged into another plug, then the S1 point to the S2, the capacity is unlimited general all plug in the power supply points to the sink point, the capacity of 1 and then from the source point to the meeting point to find the maximum flow can */#include < cstdio> #include <string.h> #include <iostream>using namespace std;const int Oo=1e9;/**oo denotes infinity */const int MM=111111;/**MM represents the maximum number of edges, remember that if the original is twice times, when the edge is two-way */const int MN=999;/**MN represents the maximum number of points */char Name[3000][30];int K;int Node,src,dest,edge;/**node represents the number of nodes, SRC represents the source point, dest represents the meeting point, edge statistics edge number */int Ver[mm],flow[mm],next[mm];/**ver edge point node, flow edge capacity, Next side of the list is the */int of the Head[mn],work[mn],dis[mn],q[mn];/**head node, work is used for the temporary link header in the algorithm, and the DIS calculates the distance *//** the linked list and the information of the graph */void Prepare (int _node,int _src,int _dest) {node=_node,src=_src,dest=_dest; for (int i=0; i<node; ++i) head[i]=-1; edge=0;} /** adds a U to v capacity of C for the side */void addedge (int u,int v,int c) {ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++; ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;} /** wide search calculates the shortest distance between each point and the source point, if it cannot reach the meeting point description algorithm ends */bool DINIC_BFS () {int i,u,v,l,r=0; for (i=0; i<node; ++i) dis[i]=-1; dis[q[r++]=src]=0; For (l=0, l<r; ++l) for (I=head[u=q[l]]; i>=0; i=next[i]) if (flow[i]&&dis[v=ver[i]]<0) {/** This edge must have the remaining capacity */dis[q[r++]=v]=dis[u]+1; if (v==dest) return 1; } return 0;} /** search for the feasible flow of the augmented path algorithm, according to the distance of the node to find, Speed */int dinic_dfs (int u,int exp) {if (u==dest) return exp; /**work is a temporary chain header, where I refer to it, so that the searched edges no longer look for */for (int &i=work[u],v,tmp; i>=0; i=next[i]) if (flow[i]&&dis[ v=ver[i]]==dis[u]+1&& (Tmp=dinic_dfs (V,min (exp,flow[i))) >0) {flow[i]-=tmp; flow[i^1]+=tmp; /** positive and negative side capacity change */return TMP; } RETUrn 0;} int Dinic_flow () {int i,ret=0,delta; while (DINIC_BFS ()) {for (i=0; i<node; ++i) work[i]=head[i]; while (Delta=dinic_dfs (Src,oo)) Ret+=delta; } return ret;} int sol (char *str) {int i; if (k==0) {strcpy (NAME[1],STR); k=1; return 1; } for (I=1; i<=k; i++) if (strcmp (NAME[I],STR) ==0) return i; k++; strcpy (NAME[K],STR); return k;} int main () {int n1,n2,n3; int st,ed; int A, B; int num1[105][2],num2[105][2],num3[105][2]; Char str1[30],str2[30]; while (~SCANF ("%d", &n1)) {k=0; for (int i=0;i<n1;i++) {scanf ("%s", str1); Num1[i][0]=sol (STR1); } scanf ("%d", &n2); for (int i=0;i<n2;i++) {scanf ("%s%s", STR1,STR2); Num2[i][0]=sol (STR1); Num2[i][1]=sol (STR2); } scanf ("%d", &n3); for (int i=0;i<n3;i++) {scanf ("%s%s", STR1,STR2); Num3[i][0]=sol (STR1); Num3[i][1]=sol (STR2); } prepare (k+2,0,k+1); for (int i=0;i<n1;i++) {Addedge (num1[i][0],dest,1); } for (int i=0;i<n2;i++) {Addedge (num2[i][0],num2[i][1],1); Addedge (src,num2[i][0],1); } for (int i=0;i<n3;i++) {Addedge (Num3[i][0],num3[i][1],oo); } printf ("%d\n", N2-dinic_flow ()); } return 0;}
poj1087 Network Maximum Flow