当前位置: 代码迷 >> 综合 >> POJ-1087 A Plug for UNIX(网络最大流)
  详细解决方案

POJ-1087 A Plug for UNIX(网络最大流)

热度:28   发布时间:2023-12-09 20:26:25.0

题目链接:POJ-1087 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.

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


题意:

有n个不同型号的插座(每种型号只有1个),m个不同的用电器且有着对应的插座型号(不同用电器对应的插座型号可能相同),同时有k种插座替代关系,例如A型号插座可以替代B型号插座(但B不一定能替代A)(插座型号和用电器均用字符串表示)。
问最少有多少用电器没有插座用?

输入一个整数n,接下来n行为现有的插座型号;
再输入一个整数m,接下来m行为用电器和插座的对应关系(用电器 对应插座);
在输入一个整数k,接下来k行为插座间的替代关系(A B 表示 B可以代替A)。


分析:

网络流首先就是要建图了,以样例输入为例,可以得到以下的图。(仅标出了正向边,反向边均为0)
在这里插入图片描述
这个要注意的点(坑)挺多的;

  1. 首先要明确现有的插座只有一开始输入的那n种插座,并且每种只有1个;
  2. 在后续输入的对应和替代关系中,出现的插座都不一定是现有的;
  3. 替换关系是单向的,但是是可以传递的,例如输入了 X A 和 B X,同时说明A可以替代B,而且可能会存在多个个可以替代B的,所以替代关系的正向边的容量应当为INF
  4. 因为不知道一共会有多少不同型号的插座,所以不便于按顺序编号,于是可以使用map直接将字符串映射为编号,然后建图。
  5. 注意数组大小,稍微开大些。

建完图以后就是套最大流的板子了。


以下代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
#define LL long long
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e3+50;
const int maxm=1e5+50;
struct edge
{
    int w;int to;int next;
}e[maxm];
int head[maxn],cnt;
int s=0,t=maxn-1;
int n,m,k;
void addedge(int u,int v,int w)
{
    e[cnt].w=w;e[cnt].to=v;e[cnt].next=head[u];head[u]=cnt;cnt++;
}
int dis[maxn];     //dis数组记录层次
bool bfs()         //利用BFS建立分成图,从而可以多次DFS增广
{
    memset(dis,-1,sizeof(dis));     //初始化dis数组queue<int> q;q.push(s);dis[s]=0;      //源点层次为0while(!q.empty()){
    int u=q.front();q.pop();for(int i=head[u];i!=-1;i=e[i].next){
    int v=e[i].to;if(e[i].w>0&&dis[v]==-1)     //可达&&未分层{
    dis[v]=dis[u]+1;         //分层if(v==t)                 //若到达汇点,则分层结束,返回truereturn true;q.push(v);}}}return false;  //运行到此处,说明汇点已不可达,返回false
}
int cur[maxn];     //弧优化:cur数组用于记录上一次DFS增广时u已经增广到第几条边,从而优化时间
int dfs(int u,int flow)       //flow代表流入u点的最大流量
{
    if(u==t)return flow;          //到达汇点,直接返回flowfor(int &i=cur[u];i!=-1;i=e[i].next){
                             //注意i前面用&引用,这样就可以直接改变cur[u]int v=e[i].to;if(dis[v]==dis[u]+1&&e[i].w>0)   //v为u的下一层&&可达{
    int k=dfs(v,min(flow,e[i].w));if(k>0){
    e[i].w-=k;         //正向边-=ke[i^1].w+=k;       //反向边+=kreturn k;}}}return 0;       //无法继续增广,返回0
}
int dinic()
{
    int ans=0;      //记录总流量while(bfs())    //分层{
    for(int i=0;i<maxn;i++)    //初始化cur数组,即将head数组赋给cur数组cur[i]=head[i];while(int k=dfs(s,INF))    //增广ans+=k;}return ans;
}
int main()
{
    memset(head,-1,sizeof(head));cnt=0;scanf("%d",&n);map<string,int> mp;        //映射编号string a,b;int index=0;for(int i=1;i<=n;i++){
    cin>>a;mp[a]=++index;addedge(s,index,1);     //连接插座和源点addedge(index,s,0);}scanf("%d",&m);for(int i=1;i<=m;i++){
    cin>>a>>b;mp[a]=++index;if(!mp.count(b))mp[b]=++index;addedge(mp[b],mp[a],1);    //连接插座和用电器addedge(mp[a],mp[b],0);addedge(mp[a],t,1);        //连接用电器和汇点addedge(t,mp[a],0);}scanf("%d",&k);for(int i=1;i<=k;i++){
    cin>>a>>b;if(!mp.count(a))mp[a]=++index;if(!mp.count(b))mp[b]=++index;addedge(mp[b],mp[a],INF);   //建立替代关系addedge(mp[a],mp[b],0);}printf("%d\n",m-dinic());return 0;
}
  相关解决方案