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 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ...
随机推荐
- go学习笔记-基础类型
基础类型 布尔值 布尔值的类型为bool,值是true或false,默认为false. //示例代码 var isActive bool // 全局变量声明 var enabled, disabled ...
- Linux系统下安装rz/sz命令
执行命令 yum install -y lrzsz rz -be本地上传文件到服务器
- shell重温---基础篇(参数传递&echo命令)
经过前两天的学习,关于shell的基础算是知道的一般般啦,最起码不算是小白了(纯属意淫).今天就来点干货哈. 首先是运行shell脚本时的参数传递.脚本内获取参数的格式为$n.n代表了一个数字,例 ...
- 20145202马超《网络对抗》Exp4 恶意代码分析
20145202马超<网络对抗>Exp4 恶意代码分析 1.实验后回答问题 (1)总结一下监控一个系统通常需要监控什么.用什么来监控. 虽然这次试验的软件很好用,我承认,但是他拖慢了电脑的 ...
- 关于实现mybatis order by 排序传递参数实现 问题记录
一 问题场景:本人项目纯纯的后端系统 并且项目前端采用纯纯的原生js 实现 1)表格 通过查询列表数据放入到域中 前段采用 for循环的方式实现遍历生成列表 2)分页实现本人是公司内部自定 ...
- 【PHP】进一法取整、四舍五入取整、忽略小数等的取整数方法大全
PHP取整数函数常用的四种方法,下面收集了四个函数:经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已--主要是:ceil,floor,round,intval PHP取整数函数常用 ...
- 通过调用API在JavaWeb项目中实现证件识别
本文详细介绍自己如何在JavaWeb项目中通过调用API实现证件识别. 一,Face++使用简介 二,两种方式(图片URL与本地上传)实现证件识别 一,Face++使用简介 Face++旷视人工智能开 ...
- 【Swift】日期比较函数 记录下 Comparing date in Swift
Add this code to your project and comparing dates is easier than ever 扩展NSDATE //swift 3.0.2 extensi ...
- python------- IO 模型
IO模型介绍 ...
- (原创)不过如此的 DFS 深度优先遍历
DFS 深度优先遍历 DFS算法用于遍历图结构,旨在遍历每一个结点,顾名思义,这种方法把遍历的重点放在深度上,什么意思呢?就是在访问过的结点做标记的前提下,一条路走到天黑,我们都知道当每一个结点都有很 ...