题意比较啰嗦。简单的说就是有n个插座,m个设备和k种插座转换器,问最多能同时插上几个设备。我们可以用网络流中的最大流来解这个问题。 首先,我们要将设备名映射成一个唯一的数字,本来想用hash,但是范围太大,因此可以使用动态递增标记,每添加一个设备,为其分配一个编号,并且编号值增1,这个可以用map来解决。 其次,不管是设备还是插座还是转换器,我们都将其视为图中的一个节点,并且让源点指向所有的设备,容量为1;让设备指向其所需要的插座,容量为1;假设转换器能将A转换为B,那么让A指向B,容量为无穷;所有的插座指向汇点。 按照上述方法建图之后求最大流,就可以求得最多有多少个设备能从源到达汇点,然后ans=m-sap()。A Plug for UNIX
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15565 Accepted: 5297 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 DSample Output
1Source
#include <iostream> #include <stdio.h> #include <string.h> #include <map> using namespace std; const int MAXN=1010; const int INF=99999999; int maze[MAXN][MAXN]; int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN]; map<string,int> Hash; int num; int sap(int start,int send,int nodenum) { memset(cur,0,sizeof(cur)); memset(dis,0,sizeof(dis)); memset(gap,0,sizeof(gap)); int u=pre[start]=start,maxflow=0,aug=-1; gap[0]=nodenum; 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==send) { 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 main() { int n,m,k; string dev,plug; int sink=MAXN-1; int sour=0; int ans; num=1; Hash.clear(); memset(maze,0,sizeof(maze)); scanf("%d",&n); for(int i=0;i<n;i++) { cin>>plug; Hash[plug]=num++; maze[Hash[plug]][sink]=1;//从插座指向汇点 } scanf("%d",&m); for(int i=0;i<m;i++) { cin>>dev>>plug; if(Hash[plug]==0)//如果不存在则添加这个插座 Hash[plug]=num++; Hash[dev]=num++; maze[Hash[dev]][Hash[plug]]=1;//从设备指向插座 maze[sour][Hash[dev]]=1;//从源指向设备 } scanf("%d",&k); for(int i=0;i<k;i++) { cin>>dev>>plug; if(Hash[dev]==0)//不存在则添加 Hash[dev]=num++; if(Hash[plug]==0)//不存在则添加 Hash[plug]=num++; maze[Hash[dev]][Hash[plug]]=INF;//根据题意,插座转换的数量不限,因此容量无限 } ans=m-sap(sour,sink,sink-sour+1); printf("%d\n",ans); return 0; }
查看原文:http://colorfulshark.cn/wordpress/plug-unix-1007.html
POJ 1087 A Plug for UNIX 网络流最大流
最新推荐文章于 2020-07-10 11:53:32 发布