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 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ...
随机推荐
- ruby json解析&生成
JSON 通常用于与服务端交换数据. 在接收服务器数据时一般是字符串. 我们可以使用 JSON.parse() 方法将数据转换为 ruby 对象. 一. json字符串解析 require 'json ...
- 嵌入式linux系统移植(一)
内容: 交叉编译环境 bootloader功能子系统 内核核心子系统 文件系统子系统要点: 搭建交叉编译环境 bootloader的选择和移植 kernel的配置.编译.移植和调 ...
- PAT-B1032
1032 挖掘机技术哪家强(20) 输入格式: 输入在第1行给出不超过10^5^的正整数N,即参赛人数.随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从1开始连续编号).及其比赛 ...
- maven 添加自己下载的jar包到本地仓库
1.在pom文件中添加依赖,其中groupId等变量都自拟. 例如: 2.在命令行执行以下命令,提示build success即表示安装成功. mvn install:install-file -Dg ...
- POJ 3210 : Coins
参考:https://blog.csdn.net/u010885899/article/details/46636523 http://kqwd.blog.163.com/blog/static/41 ...
- ES6--Set之再理解
Set 其实2016年就看过阮大神的ECMAScript 6 入门,当时看了Set之后,大致看懂了,但事实上根本没有理解Set到底是什么,所以更记不住,平时做项目大多用到的还是ES5的传统写法,以至于 ...
- CUBLAS基础实验
一.概述 最近在试图进行cuda并行编程,目标是编写一段矩阵计算代码,将计算结果存储进入GPU的缓冲区当中,并在达到某些要求后强制刷新缓冲区,取得计算结果. 但是考虑时间紧任务重的状况和实际的性能要求 ...
- The Road to learn React书籍学习笔记(第四章)
高级React组件 本章将重点介绍高级 React 组件的实现.我们将了解什么是高阶组件以及如何实现它们.此外,我们还将深入探讨 React 中更高级的主题,并用它实现复杂的交互功能. 引用 DOM ...
- Android Google Maps 监听地图缩放
接上篇.http://www.cnblogs.com/maomishen/p/3556297.html 由于公司项目要求,需要对google map监听地图的缩放(zoom)来进行一些操作. 但是在网 ...
- Cross Entropy in Machine Learning
整理摘自:https://blog.csdn.net/tsyccnh/article/details/79163834 信息论 Outline 1. 信息量与信息熵 2. 相对熵(KL散度) 3. 交 ...