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 Source |
题意:
有插座用电器和适配器,用电器有插头,适配器本身有一个插孔和插头,它的作用是可以把别的插头插入到适合该适配器插孔的适配器,然后就可以用适配器的插头接到适合的插座,相当于转换插头的作用。每个插座只能插入一个插头。3种东西都最多有100个,但是任一种适配器可以有无限个。问最后最少能剩下几个用电器不能用上电。
题解:
建图:起点 s=0, 终点 t=1;
s->用电器 边赋值 1
用电器->插座 边值 1
插座->插座 有可以转换的适配器 加边 边值 INF
插座->t 边值 1
CODE:
邻接矩阵形式:
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <queue> #include <cmath> #include <string> #define N 10101 #define MaxN 500 #define INF 1<<29 using namespace std; int maze[MaxN][MaxN]; int gap[MaxN],dis[MaxN],pre[MaxN],cur[MaxN]; struct node { char name[MaxN]; char c[MaxN]; } B[MaxN]; char A[MaxN][MaxN]; //最大流模板 int sap(int start,int ends,int nodenum) { memset(cur,0,sizeof cur); memset(dis,0,sizeof dis); memset(gap,0,sizeof gap); int u=pre[start]=start; int maxflow=0,aug=-1; while(dis[start]<nodenum) { loop: for(int v=cur[u]; v<nodenum; v++) if(maze[u][v]&&dis[u]==dis[v]+1) { if(aug==-1||aug>maze[u][v])aug=maze[u][v]; pre[v]=u; u=cur[u]=v; if(v==ends) { maxflow+=aug; for(u=pre[u]; v!=start; v=u,u=pre[u]) { maze[u][v]-=aug; maze[v][u]+=aug; } aug=-1; } goto loop; } int mindis=nodenum-1; for(int v=0; v<nodenum; v++) { if(maze[u][v]&&mindis>dis[v]) { cur[u]=v; mindis=dis[v]; } } if((--gap[dis[u]])==0)break; gap[dis[u]=mindis+1]++; u=pre[u]; } return maxflow; } int n,m,k; int main() { //freopen("test.in","r",stdin); while(~scanf("%d",&n)) { memset(maze,0,sizeof maze); int s=0,t=1; for(int i=0; i<n; i++) { scanf("%s",A[i]); } scanf("%d",&m); for(int i=0; i<m; i++) { scanf("%s %s",B[i].name,B[i].c); maze[s][i+2]=1; } scanf("%d",&k); char x[100],y[100]; int oldn=n; int nodenum=0; while(k--) { scanf("%s %s",x,y); int i,j; for(i=0; i<n; i++) if(strcmp(x,A[i])==0)break; if(i==n) { ///原来A中不存在x strcpy(A[n],x); n++; } for(j=0; j<n; j++) if(strcmp(y,A[j])==0)break; if(j==n) { ///原来A中不存在y strcpy(A[n],y); n++; } maze[i+m+2][j+m+2]=INF; } nodenum=n+m+2; for(int i=0; i<oldn; i++) maze[i+m+2][t]=1; for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { if(strcmp(B[i].c,A[j])==0) { maze[i+2][j+m+2]=1; break; } } } int ans=sap(s,t,nodenum); printf("%d\n",m-ans); } return 0; }
邻接表形式:
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <queue> #include <cmath> #include <string> #define N 5101 #define MaxN 500 #define INF 1<<29 using namespace std; int gap[MaxN],dep[MaxN],pre[MaxN],cur[MaxN]; int head[MaxN]; struct node { char name[MaxN]; char c[MaxN]; } B[MaxN]; char A[200][200]; struct Edge { int to,next,cap,flow; } edge[N]; int n,m,k; int tol; void init() { tol=0; memset(head,-1,sizeof head); } void addedge(int u,int v,int w,int rw) { edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u]; edge[tol].flow=0; head[u]=tol++; edge[tol].to=u; edge[tol].cap=rw; edge[tol].next=head[v]; edge[tol].flow=0; head[v]=tol++; } int sap(int start,int ends,int M) { memset(gap,0,sizeof gap); memset(dep,0,sizeof dep); memcpy(cur,head,sizeof head); int u=start; pre[u]=-1; gap[0]=M; int ans=0; while(dep[start]<M) { if(u==ends) { int Min=INF; for(int i=pre[u]; i!=-1; i=pre[edge[i^1].to]) if(Min>edge[i].cap-edge[i].flow) Min=edge[i].cap-edge[i].flow; for(int i=pre[u]; i!=-1; i=pre[edge[i^1].to]) { edge[i].flow+=Min; edge[i^1].flow-=Min; } u=start; ans+=Min; continue; } bool flag=false; int v; for(int i=cur[u]; i!=-1; i=edge[i].next) { v=edge[i].to; if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]) { flag=true; cur[u]=pre[v]=i; break; } } if(flag) { u=v; continue; } int Min=M; for(int i=head[u]; i!=-1; i=edge[i].next) { if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min) { Min=dep[edge[i].to]; cur[u]=i; } } gap[dep[u]]--; if(!gap[dep[u]])return ans; dep[u]=Min+1; gap[dep[u]]++; if(u!=start)u=edge[pre[u]^1].to; } return ans; } int main() { //freopen("test.in","r",stdin); while(~scanf("%d",&n)) { init(); int s=0,t=1; for(int i=0; i<n; i++) { scanf("%s",A[i]); } scanf("%d",&m); for(int i=0; i<m; i++) { scanf("%s %s",B[i].name,B[i].c); addedge(s,i+2,1,0); } scanf("%d",&k); char x[100],y[100]; int oldn=n; int nodenum=0; while(k--) { scanf("%s %s",x,y); int i,j; for(i=0; i<n; i++) if(strcmp(x,A[i])==0)break; if(i==n) { ///原来A中不存在x strcpy(A[n],x); n++; } for(j=0; j<n; j++) if(strcmp(y,A[j])==0)break; if(j==n) { ///原来A中不存在y strcpy(A[n],y); n++; } addedge(i+m+2,j+m+2,INF,0); } nodenum=n+m+2; for(int i=0; i<oldn; i++) addedge(i+m+2,t,1,0); for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { if(strcmp(B[i].c,A[j])==0) { addedge(i+2,j+m+2,1,0); break; } } } int ans=sap(s,t,nodenum); printf("%d\n",m-ans); } return 0; }
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/45568147