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
题解
题目大意:
有N1个插座,每个插座可以插一个电器
有N2个电器,每个电器只能够插在对应型号的插座上
有N3种转换器,可以把后面一种插座变成前面那种
问,最少有几个电器不能够插在插座上
题解:
源点向电器连边,容量为1,
电器向对应的插座连边,容量为1
对于转换器,在插座和插座之间连边,容量为INF
最后插座向汇点连边,容量为插座的个数
最后计算最大流即可
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
#define MAX 100000
#define MAXL 500000
#define INF 10000000
map<string,int> cz;//插座
int cnt1,level[MAX],cnt2;
int vis[MAX];
int A[MAX],B[MAX];
int WP,Change,CZ;
int S,T;
struct Line
{
int v,next,w,fb;
}e[MAXL];
string ss,sss;
int h[MAX],cnt=1;
inline void Add(int u,int v,int w)
{
e[cnt]=(Line){v,h[u],w,cnt+1};
h[u]=cnt++;
e[cnt]=(Line){u,h[v],0,cnt-1};
h[v]=cnt++;
}
bool BFS()
{
for(int i=S;i<=T;++i)level[i]=0;
level[S]=0;
queue<int> Q;while(!Q.empty())Q.pop();
Q.push(S);
level[S]=1;
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=h[u];i;i=e[i].next)
{
int v=e[i].v;
if(e[i].w&&!level[v])
{
level[v]=level[u]+1;
Q.push(v);
}
}
}
return level[T];
}
int DFS(int u,int f)
{
if(u==T||f==0)return f;
int re=0;
for(int i=h[u];i;i=e[i].next)
{
int v=e[i].v,d;
if(e[i].w&&level[v]==level[u]+1)
{
d=DFS(v,min(f,e[i].w));
f-=d;re+=d;
e[i].w-=d;e[e[i].fb].w+=d;
}
}
return re;
}
int Dinic()
{
int re=0;
while(BFS())
re+=DFS(S,INF);
return re;
}
int main()
{
freopen("POJ1087.in","r",stdin);
cin>>CZ;
for(int i=1;i<=CZ;++i)
{
cin>>ss;
if(cz.find(ss)==cz.end())
cz[ss]=++cnt1;
vis[cz[ss]]+=1;
}
cnt2=cnt1;
cin>>WP;
for(int i=1;i<=WP;++i)//连接物品和插座
{
cin>>ss>>sss;
Add(S,i,1);
if(cz.find(sss)==cz.end())cz[sss]=++cnt1;
Add(i,WP+cz[sss],1);
}
cin>>Change;
S=0;
for(int i=1;i<=Change;++i)
{
cin>>ss>>sss;
//Add(S,i,1);
if(cz.find(ss)==cz.end())cz[ss]=++cnt1;
if(cz.find(sss)==cz.end())cz[sss]=++cnt1;
Add(WP+cz[ss],WP+cz[sss],INF);
}
T=WP+cnt1+1;
for(int i=1;i<=cnt2;++i)
Add(WP+i,T,vis[i]);
printf("%d\n",WP-Dinic());
return 0;
}
POJ 1087 A Plug for UNIX (网络流,最大流)的更多相关文章
- poj 1087 A Plug for UNIX 【最大流】
题目连接:http://poj.org/problem? id=1087 题意: n种插座 ,m个电器,f组(x,y)表示插座x能够替换插座y,问你最多能给几个电器充电. 解法:起点向插座建边,容量1 ...
- poj 1087.A Plug for UNIX (最大流)
网络流,关键在建图 建图思路在代码里 /* 最大流SAP 邻接表 思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧. 优化: 1.当前弧优化(重要). 1.每找到以条增广路回退到断点(常数优化 ...
- POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for UNIX / UVAlive 5418 A Plug for UNIX / SCU 1671 A Plug for UNIX (网络流)
POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for ...
- poj 1087 C - A Plug for UNIX 网络流最大流
C - A Plug for UNIXTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...
- poj 1087 A Plug for UNIX(字符串编号建图)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14862 Accepted: 5026 ...
- uva753 A Plug for UNIX 网络流最大流
C - A Plug for UNIX You are in charge of setting up the press room for the inaugural meeting of t ...
- UVa753/POJ1087_A Plug for UNIX(网络流最大流)(小白书图论专题)
解题报告 题意: n个插头m个设备k种转换器.求有多少设备无法插入. 思路: 定义源点和汇点,源点和设备相连,容量为1. 汇点和插头相连,容量也为1. 插头和设备相连,容量也为1. 可转换插头相连,容 ...
- kuangbin专题专题十一 网络流 POJ 1087 A Plug for UNIX
题目链接:https://vjudge.net/problem/POJ-1087 题目:有n个插座,插座上只有一个插孔,有m个用电器,每个用电器都有插头,它们的插头可以一样, 有k个插孔转化器, a ...
- poj 1087 A Plug for UNIX
题目描述:现在由你负责布置Internet联合组织首席执行官就职新闻发布会的会议室.由于会议室修建时被设计成容纳全世界各地的新闻记者,因此会议室提供了多种电源插座用以满足(会议室修建时期)各国不同插头 ...
随机推荐
- Zabbix监控之迁移zabbix server
abbix监控中有时会根据需要对zabbix服务器进行迁移,zabbix迁移是非常简单的,因为zabbix的前端所有的操作都存在zabbix数据库里.所以zabbix迁移只需对zabbix库中相应的表 ...
- 分布式集群下的Session存储方式窥探
传统的应用服务器,自身实现的session管理是大多是基于单机的,对于大型分布式网站来说,支撑其业务的远远不止一台服务器,而是一个分布式集群,请求在不同的服务器之间跳转.那么,如何保持服务器之前的se ...
- 谷歌内核浏览器 iframe内容的 tab切换 滚动条消失
问题: 新版本的-webkit- 内核浏览器 在tab切换时,iframe 内容区 丢失滚动条 如下图 (虽然滚动条位置还在,可以垂直滚动,但滚动条不见了) 解决思路: 让iframe重新计算宽高,重 ...
- [原创]Oracle 12c的备份和恢复策略
Oracle 12c的备份和恢复策略(RMAN备份[开启归档/控制文件/数据文件/归档日志]): 备份策略: * 每半年做一个数据库的全备份(包括所有的数据和只读表空间) * 每周做一次零级备份 * ...
- C语言学习之插入排序
此前的一些博文分别写了C语言中经典的排序方式,选择排序 冒泡排序 桶排序,此文就写 插入排序吧. 相对于冒泡排序,插入排序就比较方便快捷了.和冒泡 选择排序一样,插入排序也需要比较大小.可以这样理解插 ...
- Ubuntu忘记root密码怎么办?
http://www.linuxidc.com/Linux/2016-05/131256.htm
- 使用Netbeans内置的Git工具
在 NetBeans IDE 中使用 Git 支持 NetBeans IDE 为 Git 版本控制客户端提供支持.通过利用 IDE 的 Git 支持,您可以从 IDE 内的项目中直接执行版本控制任务. ...
- Android安全机制
1)Android是基于Linux内核的,因此Linux对文件权限的控制同样适用于Android.在Android中每个应用都有自己的/data/data/包名文件夹,该文件夹只能该应用访问,而其他应 ...
- hihoCoder 1493 : 歌德巴赫猜想 素数筛法
题意:哥德巴赫猜想认为"每一个大于2的偶数,都能表示成两个质数之和".给定一个大于2的偶数N,你能找到两个质数P和Q满足P<=Q并且P+Q=N吗?如果有多组解,输出P最小的一 ...
- HDU - 1847 巴什博弈
思路: 0 1 2 3 4 5 6 7 8 9 10 11 12 P N N P N N P N N P N N P 不难发现:当n为三的倍数时,KIKI ...