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 UNIX / UVAlive 5418 A Plug for UNIX / SCU 1671 A Plug for UNIX (网络流)
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
Http
POJ:https://vjudge.net/problem/POJ-1087
HDU:https://vjudge.net/problem/HDU-1526
ZOJ:https://vjudge.net/problem/ZOJ-1157
UVA:https://vjudge.net/problem/UVA-753
UVAlive:https://vjudge.net/problem/UVALive-5418
SCU:https://vjudge.net/problem/SCU-1671
Source
网络流,最大流
题目大意
现在提供若干规格确定的插座,电器以及插座转换器,其中转换器数量无限。每个插座只能给一个电器插电,转换器可以叠加。现在求不能连接上电的电器数量最小值。
解决思路
这道题想复杂了,之前还想着要把转换器拆成两个点来处理,其实不用。
我们将源点与每一个插座之间连一条流量为1的边(因为一个电器只能供一个,所以可以理解为只提供一个电流)
然后我们对于每一种转换器将对应的插座连上,注意,因为这个时候可能出现转换器转换后出现插座中没出现过的规格。
我们再对每一种电器和它可以匹配的插座或转换器规格连一条容量无穷的边。这里为什么要连无穷大呢?因为在用转换器转换过后可能从别的地方连过来一条边。
最后再在电器与汇点之间连容量为1的边。
需要注意的是,我们要用一个变量n12记下插座数,用n1记下插座数+转换器中新出现的规格数,因为只能在插座与源点之间连线,但可以在任意规格与电器之间连线。
还有一些细节问题,如是否有多组数据,是否有行末换行与组数之间的换行。
虽然笔者是用EK算法实现的最大流,但更推荐的是效率更好的Dinic算法,具体请移步我的这篇文章
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
#include<vector>
#include<queue>
using namespace std;
const int maxN=2000;
const int inf=2147483647;
class Edge
{
public:
int v,w;
};
int n1,n2,n3;//这三个变量插座,转换器,电器
int n12;//在读入n1后就复制到这里,因为后面可能会改变n1的值
int R1[maxN];//读入插座规格
int R2[maxN];//读入电器规格
int R3[maxN][3];//R3[i][1]代表i号转换器提供的插座规格,R3[i][2]表示i号转换器需要的插座规格
int G[maxN][maxN];
map<string,int> Map;//用于将字符串编号
int Path[maxN];
int Flow[maxN];
bool bfs();
int main()
{
int T;
cin>>T;//多组数据,POJ没有,注释掉相关行即可
for (int ti=1;ti<=T;ti++)
{
Map.clear();
memset(G,0,sizeof(G));
scanf("%d",&n1);
for (int i=1;i<=n1;i++)//读入插座规格
{
string str;
cin>>str;
Map[str]=i;
R1[i]=i;
}
n12=n1;//保存一下,下面要用
scanf("%d",&n2);
for (int i=1;i<=n2;i++)//读入电器
{
string str;
cin>>str;//这个名字其实没有用
cin>>str;
int k;
if (Map[str]==0)//遇到之前没有出现的规格要新分配编号。注意这里有可能改变了n1
k=Map[str]=++n1;
else
k=Map[str];
R2[i]=k;
}
scanf("%d",&n3);
for (int i=1;i<=n3;i++)//读入转换器
{
string str;
cin>>str;
if (Map[str]==0)//同样遇到之前没有出现过的要新分配编号
Map[str]=++n1;
R3[i][1]=Map[str];
cin>>str;
if (Map[str]==0)
Map[str]=++n1;
R3[i][2]=Map[str];
}
//cout<<n1<<' '<<n2<<' '<<n3<<endl;
for (int i=1;i<=n12;i++)//只能在插座与源点之间连边
G[0][i]=1;
for (int i=1;i<=n12;i++)//在插座与对应的电器之间连边
for (int j=1;j<=n2;j++)
if (R1[i]==R2[j])
G[i][j+n1]=inf;
for (int i=n12+1;i<=n1;i++)//在新出现的规格与电器之间连边
for (int j=1;j<=n2;j++)
if (i==R2[j])
G[i][j+n1]=inf;
for (int i=1;i<=n3;i++)//在转换器对应的插座规格之间连边
G[R3[i][2]][R3[i][1]]=inf;
for (int i=1;i<=n2;i++)
G[i+n1][n1+n2+1]=1;
//for (int i=0;i<=n1+n2+1;i++)
//{
// for (int j=0;j<=n1+n2+1;j++)
// cout<<G[i][j]<<' ';
// cout<<endl;
// }
int Ans=0;
while (bfs())//EK算法求最大流
{
int di=Flow[n1+n2+1];
int now=n1+n2+1;
int last=Path[now];
while (now!=0)
{
G[last][now]-=di;
G[now][last]+=di;
now=last;
last=Path[now];
}
Ans+=di;
}
cout<<n2-Ans<<endl;
if (ti!=T)//注意,如果是多组数据时最后一个不要多输出一个空行
cout<<endl;
}
return 0;
}
bool bfs()//bfs求增广路
{
memset(Path,-1,sizeof(Path));
memset(Flow,0,sizeof(Flow));
queue<int> Q;
while (!Q.empty())
Q.pop();
Q.push(0);
Flow[0]=inf;
Path[0]=0;
do
{
int u=Q.front();
Q.pop();
for (int i=0;i<=n1+n2+1;i++)
if ((Path[i]==-1)&&(G[u][i]>0))
{
Path[i]=u;
Flow[i]=min(Flow[u],G[u][i]);
Q.push(i);
}
}
while (!Q.empty());
if (Flow[n1+n2+1]==0)
return 0;
return 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 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- 【网络流#4】UVA 753 最大流
最近开始刷网络流的题目了,先从紫书上的开始,这道题是P374上的,嘛,总之这道题最终还是参考了一下紫书. 中间是用了STL中map将字符串映射成编号,使用编号总比是用字符串简单的多. 超级源点S与各个 ...
- 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 (网络流,最大流)
题面 You are in charge of setting up the press room for the inaugural meeting of the United Nations In ...
- (网络流 模板)A Plug for UNIX -- poj -- 1087
链接: http://poj.org/problem?id=1087 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82835#probl ...
- C - A Plug for UNIX POJ - 1087 网络流
You are in charge of setting up the press room for the inaugural meeting of the United Nations Inter ...
- kuangbin专题专题十一 网络流 POJ 1087 A Plug for UNIX
题目链接:https://vjudge.net/problem/POJ-1087 题目:有n个插座,插座上只有一个插孔,有m个用电器,每个用电器都有插头,它们的插头可以一样, 有k个插孔转化器, a ...
- UVA 753 A Plug for UNIX 电器插座(最大基数匹配,网络流)
题意: 给n个插座,m个设备(肯定要插电了),k种转换头可无限次使用(注意是单向的),问有多少设备最终是不能够插上插座的? 分析: 看起来就是设备匹配插座,所以答案不超过m.这个题适合用网络流来解. ...
- UVA 753 - A Plug for UNIX(网络流)
A Plug for UNIX You are in charge of setting up the press room for the inaugural meeting of the U ...
随机推荐
- 20155238 2016-2017-2《Java程序设计》课程总结
每周作业链接汇总(按顺序) 预备作业1 预备作业2 预备作业3 第一周作业 第二周作业 第三周作业 第四周作业 第五周作业 第六周作业 第七周作业 第八周作业 第九周作业 第十周作业 自认为写得最好一 ...
- 矩阵乘法&&矩阵快速幂&&最基本的矩阵模型——斐波那契数列
矩阵,一个神奇又令人崩溃的东西,常常用来优化序列递推 在百度百科中,矩阵的定义: 在数学中,矩阵(Matrix)是一个按照长方阵列排列的复数或实数集合 ,最早来自于方程组的系数及常数所构成的方阵.这一 ...
- 如何查看哪个进程,使用了哪个CPU
某些时候,我们需要知道,在Unix/Linux 环境中,CPU究竟消耗在了哪些进程上面. 如下是最简单的方法: ps -elF
- Qt程序修改Ubuntu系统时间
QString str = QString("sudo date -s %1/%2/%3").arg(iMonth).arg(iDay).arg(iYear); system(st ...
- [LOJ#6066]. 「2017 山东一轮集训 Day3」第二题[二分+括号序列+hash]
题意 题目链接 分析 首先二分,假设二分的答案为 \(mid\),然后考虑利用括号序列来表示树的形态. 点 \(u\) 的 \(k-\) 子树的括号序列表示实际上是刨去了 \(u\) 子树内若干个与 ...
- ElasticSearch入门 第九篇:实现正则表达式查询的思路
这是ElasticSearch 2.4 版本系列的第九篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- 使用开源项目免费申请 JetBrains 全家桶 IDEA 开源许可证
JetBrains 公司旗下的 IDEA 功能都十分强大,深受各种编程语言相关的程序员的喜爱.我个人而言,经常使用 WebStorm,也使用过 PyCharm. 正常情况下 JetBrains 公司的 ...
- 对Java8 stream的简单实践
最近学习很多Java8方面的新特性,特地做了一些简单的实践和总结. import java.util.*; import java.util.stream.Collectors; public cla ...
- 甲题题解-1116. Come on! Let’s C (20)-(素数筛选法)
用vis标记出现过的id,checked标记询问过的id.至于如何判断排名为素数,用素数筛选法预处理一下即可,水题. #include <iostream> #include <cs ...
- linux内核分析--计算机是如何工作的
实验部分 使用gcc -S -o main.s main.c -m32命令将源代码编译成汇编代码. 源代码如下: int g(int x) { return x + 9; } int f(int x) ...