POJ1087:A Plug for UNIX(最大流)
A Plug for UNIX
题目链接:https://vjudge.net/problem/POJ-1087
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
题意:
这个题意挺难理解的,一开始输入的是插座,然后输入的是插头,最后输入的是转换器。
转换器后面有两个字符串,也就是说能够把插头为第一个的转化为第二个字符串,这里插头有无限个。
要求最大能让多少插头连上插座。
题解:
首先考虑建图的两边,源点连每个插头且边权为1,然后每个插座连上汇点且边权为1。
然后每个插座可以使X->Y(举例),我们就可以想让X连一条边权为无穷大的边到Y。
最后跑一个最大流就是了。注意下输入时对字符串的处理。
注意数组要开到500左右,因为点的最多可能是400。
我这里建图是反过来建的,为了方便~
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#define INF 99999999
#define t 500
using namespace std; const int N = ;
int head[N],d[N];
int tot,n,m,k,cnt;
struct Edge{
int v,next,c;
}e[N<<];
char str[N][],s[],tmp[];
void adde(int u,int v,int c){
e[tot].v=v;e[tot].next=head[u];e[tot].c=c;head[u]=tot++;
e[tot].v=u;e[tot].next=head[v];e[tot].c=;head[v]=tot++;
}
bool bfs(int S,int T){
memset(d,,sizeof(d));d[S]=;
queue <int > q;q.push(S);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(!d[v] && e[i].c>){
d[v]=d[u]+;
q.push(v);
}
}
}
return d[T]!=;
}
int dfs(int s,int a){
int flow=,f;
if(s==t || a==) return a;
for(int i=head[s];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]!=d[s]+) continue ;
f=dfs(v,min(a,e[i].c));
if(f){
e[i].c-=f;
e[i^].c+=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[s]=-;
return flow;
}
int Dinic(){
int max_flow = ;
while(bfs(,t)){
max_flow+=dfs(,INF);
}
return max_flow;
}
int num=;
int Search(char *s){
if(num==){
strcpy(str[],s);
num++;
return ;
}
for(int i=;i<=num;i++){
if(strcmp(str[i],s)==) return i;
}
num++;
strcpy(str[num],s);
return num;
}
int main(){
scanf("%d",&n);
memset(head,-,sizeof(head));
for(int i=;i<=n;i++){
scanf("%s",s);
int a=Search(s);
adde(,a,);
}
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%s%s",tmp,s);
int a=Search(s);
adde(a,t,);
}
scanf("%d",&k);
for(int i=;i<=k;i++){
scanf("%s%s",tmp,s);
int a=Search(tmp),b=Search(s);
adde(b,a,INF);
}
printf("%d",m-Dinic());
return ;
}
POJ1087:A Plug for UNIX(最大流)的更多相关文章
- POJ1087 A Plug for UNIX —— 最大流
题目链接:https://vjudge.net/problem/POJ-1087 A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K T ...
- 【poj1087/uva753】A Plug for UNIX(最大流)
A Plug for UNIX Description You are in charge of setting up the press room for the inaugural meeti ...
- POJ1087 A Plug for UNIX 【最大流】
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13855 Accepted: 4635 ...
- POJ1087 A Plug for UNIX(网络流)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ1087 A Plug for UNIX 2017-02-12 13:38 40人阅读 评论(0) 收藏
A Plug for UNIX Description You are in charge of setting up the press room for the inaugural meeting ...
- poj1087 A Plug for UNIX(网络流最大流)
http://poj.org/problem?id=1087 好久没遇见过这么坑的题了这个题真是挫的够可以的.题目大意:你作为某高管去住宿了,然后宾馆里有几种插座,分别有其对应型号,你携带了几种用电器 ...
- poj1087 A Plug for UNIX & poj1459 Power Network (最大流)
读题比做题难系列…… poj1087 输入n,代表插座个数,接下来分别输入n个插座,字母表示.把插座看做最大流源点,连接到一个点做最大源点,流量为1. 输入m,代表电器个数,接下来分别输入m个电器,字 ...
- 【uva753/poj1087/hdu1526-A Plug for UNIX】最大流
题意:给定n个插座,m个插头,k个转换器(x,y),转换器可以让插头x转成插头y.问最少有多少个插头被剩下. 题解: 最大流或者二分图匹配.然而我不知道怎么打二分图匹配..打了最大流.这题字符串比较坑 ...
- ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)
链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? id=26746 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ...
随机推荐
- linux几条基本命令和解释
pwd 查看当前目录/ 根目录ls 查看当前目录所包含文件ls -l 查看当前目录所包含文件的详细信息d rwx rwx r-x 1 root root1 2 3 4 ...
- uva 508 - Morse Mismatches(摩斯码)
来自https://blog.csdn.net/su_cicada/article/details/80084529 习题4-6 莫尔斯电码(Morse Mismatches, ACM/ICPC Wo ...
- DDR分析与布线要求
基本知识 Double Data Rate Synchronous Dynamic Random Access Memory 简称 DDR SDRAM 双倍数据率同步动态随机存取内存 DDR SDRA ...
- 3468-A Simple Problem with Integers 线段树(区间增减,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 110077 ...
- JavaScript序列化对象成URL格式
http://access911.net/fixhtm/72FABF1E15DCEAF3.htm?tt=
- js学习日记-new Object和Object.create到底干了啥
function Car () { this.color = "red"; } Car.prototype.sayHi=function(){ console.log('你好') ...
- 阴影效果的小 demo
早上没事干,感觉字体阴影的效果还是好看的,那么就来一个小demo吧! 1.这是html 简单的有一个标签或者盒子都可以 <div class="demo11">我爱考试 ...
- Loadrunner11.0安装与简单使用
公司开发了APP或者微信小程序啊什么的,都会先进行性能测试,而性能测试一般肯定会来测试接口的压测,并发.Loadrunner是一个很强大的测试工具,它是一种预测系统行为和性能的负载测试工具.通过以模拟 ...
- 九度OJ--Q1473
import java.util.ArrayList;import java.util.Scanner; /* * 题目描述: * 大家都知道,数据在计算机里中存储是以二进制的形式存储的. * 有一天 ...
- Mininet简单性能测试
建一个简单的模型,使用一个单交换机,然后链接n个主机形成拓扑,然后对每个链路设置带宽,延迟时间,和丢包率. 这里就选择建一个单交换机和六个主机的作为例子. 创建py脚本生成拓扑:写一个类生成一个单交换 ...