您的位置:首页 > 理论基础 > 计算机网络

POJ 1087 A Plug for UNIX(网络流)

2017-04-06 20:50 453 查看
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

最大流的题,建图崩了好几次。。。为了锻炼能力我选了细节较多的ISAP。
题意大致为:给你n种电源插座、m种用电器(每种用电器对应唯一一种插头)、k种转换器(“A->B”,表示可将A类型转换为B类型,每种转换器有无穷个),问最少能剩余几个用电器没电源插座可插。可建一个图转化为最大流:从源点向每种用电器建一条容量为1的边,用电器向它对应的插头建一条容量为1的边,每种电源插座向汇点建一条容量为1的边,每种转换器对应的插座类型向插头类型建一条容量为INF的边(容量为INF!!!),最大流流一流就OK了。
紫书上还给出了另一种解法,就是先对转换器求一下传递闭包,即可得到每种用电器的插头是否可以插到电源插座上,能插的就建一条从插头到插座的容量为1的边,依然最大流。
BFS分层的时候边的方向搞反了,中间几次WA到爆炸。还需要注意的是转换头加边时容量应为INF,因为转换头不限数量。还有。。。一点。。。插头、插座、用电器的名字都有可能是字符串,没细看题,让样例忽悠了。这题A的好艰辛。。代码如下,第二种方法的代码有时间再补吧:

//ISAP
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstdio>
using namespace std;
typedef struct node
{
int from,to,cap,flow;
node(int f,int t,int c,int ff):from(f),to(t),cap(c),flow(ff){}
}node;
int n,m,k,N,s,t;
const int INF = 1 << 30;
const int maxn = 305;
int num[maxn],p[maxn],d[maxn],cur[maxn];
bool vis[maxn];
vector<node> edge;
vector<int> G[maxn];
void addedge(int from,int to,int cap,int flow)
{
edge.push_back(node(from,to,cap,flow));
edge.push_back(node(to,from,0,0));
int mm = edge.size();
G[from].push_back(mm - 2);
G[to].push_back(mm - 1);
}
void BFS()
{
memset(vis,false,sizeof(vis));
memset(d,0,sizeof(d));
d[t] = 0,vis[t] = true;
queue<int> q;
q.push(t);
while(!q.empty())
{
int x = q.front();q.pop();
for(int i = 0;i < G[x].size(); ++i)
{
node &e = edge[G[x][i]];
if(!vis[e.to])
{
vis[e.to] = true;
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
}
int argument()
{
int x = t,a = INF;
while(x != s)
{
node &e = edge[p[x]];
a = min(a,e.cap - e.flow);
x = edge[p[x]].from;
}
x = t;
while(x != s)
{
edge[p[x]].flow += a;
edge[p[x] ^ 1].flow -= a;
x = edge[p[x]].from;
}
return a;
}
int Maxflow()
{
BFS();
memset(cur,0,sizeof(cur));
memset(num,0,sizeof(num));
for(int i = 0;i < maxn; ++i) num[d[i]]++;
int flow = 0,x = s;
while(d[s] < N)
{
if(x == t)
{
flow += argument();
x = s;
}
bool ok = false;
for(int i = cur[x];i < G[x].size(); ++i)
{
node &e = edge[G[x][i]];
if(d[x] == d[e.to] + 1 && e.cap > e.flow)
{
ok = true;
p[e.to] = G[x][i];
cur[x] = i;
x = e.to;//Be carefull!
break;//Be carefull!
}
}
if(!ok)
{
int M = N;
if(--num[d[x]] == 0) break;
for(int i = 0;i < G[x].size(); ++i)
{
node &e = edge[G[x][i]];
if(e.cap > e.flow) M = min(M,d[e.to]);
}
num[d[x] = M + 1]++;
cur[x] = 0;
if(x != s) x = edge[p[x]].from;
}
}
return flow;
}
int main()
{
map<string,int> a;
map<string,int> b;
char p1[30],p2[30];
s = 0,t = 301;
int t1 = 1,t2 = 1;
string temp1,temp2;
scanf("%d",&n);
for(int i = 0;i < n; ++i)
{
temp1.clear(),temp2.clear();
scanf(" %s",p1);
temp1.insert(0,p1);
if(!a[temp1]) a[temp1] = t1++;
addedge(a[temp1] + 100,t,1,0);
}
scanf("%d",&m);
for(int i = 0;i < m; ++i)
{
temp1.clear(),temp2.clear();
scanf(" %s %s",p1,p2);
temp1.insert(0,p1);
temp2.insert(0,p2);
if(!b[temp1]) b[temp1] = t2++;
if(!a[temp2]) a[temp2] = t1++;
addedge(s,b[temp1],1,0);
addedge(b[temp1],a[temp2] + 100,1,0);
}
scanf("%d",&k);
for(int i = 0;i < k; ++i)
{
temp1.clear(),temp2.clear();
scanf(" %s %s",p1,p2);
temp1.insert(0,p1),temp2.insert(0,p2);
if(!a[temp1]) a[temp1] = t1++;
if(!a[temp2]) a[temp2] = t1++;
addedge(a[temp1] + 100,a[temp2] + 100,INF,0);
}
N = 2 * (t1 + t2 - 2) + 2;
printf("%d\n",m - Maxflow());
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: