A Plug for UNIX |
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.
There's a blank line between test cases.
1 4 A B C D 5 laptop B phone C pager B clock B comb X 3 B X X A X D
1
uva的题就是坑。。题目这么长。。英语烂的伤不起。。
给你N个插头,可能一种插头有多个。M个用电器和用电器需要匹配的插头。K种适配器(每种有无限个),可以把后面那种插头转化成前面那种(一开始就没注意这,以为是前面转化到后面。。无语。。)
每种拥有的插头、需要的插头、适配器都要设为一个点。建立一个起点0,连接拥有的每种插头,容量是那种插头的数量。建立一个终点1,连接用电器需要的每种插头,容量是每种插头需要的个数。再把适配器的两种类型连接,容量为INF。
这道题因为不知道总共有多少个东西,最简单的方法是用MAP,输入串后如果不在MAP里就加入作为新的点。
还有数组要设400+。。。一开始没注意RE了。因为可能出现400个完全不同的东西。。
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<map> #define INF 0x3f3f3f3f #define MAXN 410 using namespace std; int T,N,M,K,P; int flow[MAXN][MAXN],cap[MAXN][MAXN],a[MAXN],p[MAXN]; map<string,int> Map; int Edmonds_Karp(){ queue<int> q; int f=0,s=0,t=1; memset(flow,0,sizeof(flow)); while(1){ memset(a,0,sizeof(a)); a[s]=INF; q.push(s); while(!q.empty()){ int u=q.front(); q.pop(); for(int v=0;v<=P;v++) if(!a[v]&&flow[u][v]<cap[u][v]){ a[v]=min(a[u],cap[u][v]-flow[u][v]); p[v]=u; q.push(v); } } if(!a[t]) break; for(int u=t;u!=s;u=p[u]){ flow[p[u]][u]+=a[t]; flow[u][p[u]]-=a[t]; } f+=a[t]; } return f; } int main(){ freopen("in.txt","r",stdin); scanf("%d",&T); while(T--){ Map.clear(); memset(cap,0,sizeof(cap)); char str[30],t[30]; P=1; scanf("%d",&N); for(int i=0;i<N;i++){ scanf("%s",str); if(!Map[str]) Map[str]=++P; int v=Map[str]; cap[0][v]++; } scanf("%d",&M); for(int i=0;i<M;i++){ scanf("%s%s",t,str); if(!Map[str]) Map[str]=++P; int u=Map[str]; cap[u][1]++; } scanf("%d",&K); for(int i=0;i<K;i++){ scanf("%s%s",t,str); if(!Map[t]) Map[t]=++P; if(!Map[str]) Map[str]=++P; int v=Map[t],u=Map[str]; cap[u][v]=INF; } printf("%d\n",M-Edmonds_Karp()); if(T) puts(""); } return 0; }