【poj1087/uva753】A Plug for UNIX(最大流)
A Plug for UNIXDescription
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 DSample Output
1
最大流或者二分图匹配都能做。
我做的是最大流~~
建图->st连插头流量为这种插头数量
插座连ed流量为这种插座的数量
对于插头的转换,就在左边的图连x->y,流量为正无穷
最后计算最大流。
不会用map的我,搞编号搞了一辈子,呵呵~
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 0xfffffff
// #define Maxl 100010
#define Maxn 510 int n,m,k; struct node
{
int x,y,f,o,next;
}t[Maxn**];int len;
int first[Maxn],st,ed; int mymin(int x,int y) {return x<y?x:y;} void ins(int x,int y,int f)
{
t[++len].x=x;t[len].y=y;t[len].f=f;
t[len].next=first[x];first[x]=len;t[len].o=len+;
t[++len].x=y;t[len].y=x;t[len].f=;
t[len].next=first[y];first[y]=len;t[len].o=len-;
} int h[Maxn],num[Maxn];
char c[Maxn],ss[Maxn],sh[*Maxn][Maxn]; void init()
{
scanf("%d",&n);
memset(first,,sizeof(first));
len=;int sl=;
for(int i=;i<=n;i++)
{
scanf("%s",ss);sl++;
memcpy(sh[sl],ss,sizeof(sh[sl]));
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%s%s",c,ss);sl++;
memcpy(sh[sl],ss,sizeof(sh[sl]));
}
scanf("%d",&k);
for(int i=;i<=k;i++)
{
scanf("%s",ss);sl++;
memcpy(sh[sl],ss,sizeof(sh[sl]));
scanf("%s",ss);sl++;
memcpy(sh[sl],ss,sizeof(sh[sl]));
} st=,ed=;
int p=;
for(int i=;i<=sl;i++)
{
int id=-;
for(int j=;j<i;j++)
{
if(strcmp(sh[i],sh[j])==) {id=num[j];break;}
}
if(id==-) id=++p;
num[i]=id;
} // printf("%d\n",p); memset(h,,sizeof(h));
for(int i=;i<=n;i++) h[num[i]]++;
for(int i=;i<=p;i++) if(h[i])
{
ins(i,ed,h[i]);
ins(i+p,i,INF);
// ins(i+n+m,i,INF);
} memset(h,,sizeof(h));
for(int i=;i<=m;i++) h[num[i+n]]++;
for(int i=;i<=p;i++) if(h[i])
{
ins(st,i+p,h[i]);
ins(i+p,i,INF);
} for(int i=;i<=k;i++)
{
ins(num[i*-+n+m]+p,num[i*+n+m]+p,INF);
// ins(num[i*2-1+n+m]+n+m,num[i*2+n+m],INF);
}
/*for(int i=1;i<=len;i+=2)
{
printf("%d -> %d :%d \n",t[i].x,t[i].y,t[i].f);
}*/
} int dis[Maxn];
queue<int > q;
bool bfs()
{
while(!q.empty()) q.pop();
memset(dis,-,sizeof(dis));
q.push(st);dis[st]=;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=first[x];i;i=t[i].next) if(t[i].f>)
{
if(dis[t[i].y]==-)
{
q.push(t[i].y);
dis[t[i].y]=dis[x]+;
}
}
}
if(dis[ed]==-) return ;
return ;
} int ffind(int x,int flow)
{
int now=;
if(x==ed) return flow;
for(int i=first[x];i;i=t[i].next)
if(t[i].f>&&dis[t[i].y]==dis[x]+)
{
int a=ffind(t[i].y,
mymin(flow-now,t[i].f));
now+=a;
t[i].f-=a;
t[t[i].o].f+=a;
if(now==flow) break;
}
if(now==) dis[x]=-;
return now;
} void max_flow()
{
int ans=;
while(bfs()) ans+=ffind(st,INF);
printf("%d\n",m-ans);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
bfs();
max_flow();
if(T!=) printf("\n");
}
return ;
}
[UVA753]
2016-07-15 14:26:32
【poj1087/uva753】A Plug for UNIX(最大流)的更多相关文章
- 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 ...
- POJ1087:A Plug for UNIX(最大流)
A Plug for UNIX 题目链接:https://vjudge.net/problem/POJ-1087 Description: You are in charge of setting u ...
- POJ1087 A Plug for UNIX —— 最大流
题目链接:https://vjudge.net/problem/POJ-1087 A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K T ...
- UVa753/POJ1087_A Plug for UNIX(网络流最大流)(小白书图论专题)
解题报告 题意: n个插头m个设备k种转换器.求有多少设备无法插入. 思路: 定义源点和汇点,源点和设备相连,容量为1. 汇点和插头相连,容量也为1. 插头和设备相连,容量也为1. 可转换插头相连,容 ...
- ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)
链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? id=26746 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ...
- TZOJ 1911 A Plug for UNIX(最大流)
描述 You are in charge of setting up the press room for the inaugural meeting of the United Nations In ...
- POJ A Plug for UNIX (最大流 建图)
Description You are in charge of setting up the press room for the inaugural meeting of the United N ...
- uva753 A Plug for UNIX
最大流. 流可以对应一种分配方式. 显然最大流就可以表示最多匹配数 #include<cstdio> #include<algorithm> #include<cstri ...
- UVa 753 A Plug for UNIX (最大流)
题意:给定 n 种插座,m种设备,和k个转换器,问你最少有几台设备不能匹配. 析:一个很裸的网络流,直接上模板就行,建立一个源点s和汇点t,源点和每个设备连一条边,每个插座和汇点连一条边,然后再连转换 ...
随机推荐
- linux中的帮助命令 分类: linux 学习笔记 ubuntu 2015-07-05 19:07 31人阅读 评论(0) 收藏
说实话,到目前为止我还是不太习惯使用linux自带的帮助文档,遇到问题都是去查我自己下载的chm格式的命令大全,不过这些帮助命令我们还是有必要了解的. 1.man [要查看的命令名称] 例如想要查看l ...
- 分分钟教你集成沉浸式侧滑关闭Activity
网上搜索侧滑关闭Activity,几乎没有系统状态栏跟随页面一起联动的,有明显的撕裂感,而这里则是状态栏跟随页面联动的,说来集成也是简单,等会你就知道了. 个人习惯,写博客前喜欢先截图 1.首先以项目 ...
- SetUID、SetGID中的大小写Ss和Sticky bit中的大小写Tt
大写:原文件/目录没有执行(x)权限 小写:原文件/目录有执行(x)权限 例如: 原文件:-rwxr-xr-x 增加SetUID后 4755 变为:-rwsr-xr-x 再如: 原文件:-rwxr-- ...
- ArcGIS API for Javascript配置
1.去官网下载最新版: ArcGIS API for JavaScript 3.9 API 参考文档:https://developers.arcgis.com/javascr 智能提示下载:http ...
- jQuery滑动并响应事件
jQuery滑动并打开指定页面: <!DOCTYPE html> <html> <head> <script src="http://code.jq ...
- JS实例(一)
一:单选按钮,选择同意,提交变为可用,反正提交不可用: HTML里面代码: <form id="f1" name="f1"> <input t ...
- javascript中强制类型转换
javascript开发过程中,强制类型转换一般发生在条件判断和==运算符.其他情况,发生的类型转换(与这两种情况也是基本类似,属于万变不离其宗的范畴),暂不讨论. == 双等运算符 考虑代码: a ...
- html a标签 图片边框和点击后虚线框的有关问题
html a标签 图片边框和点击后虚线框的问题 一直在在chrome上开发自己的项目,今天上ie和firefix一看,真丑,a标签在使用图片时,多加了蓝色的边框,并且点击后所有a标签都会出现一个虚线框 ...
- 前台添加jquery的引用
注意引用的顺序. 以下两个引用,因为bxCarousel.js引用了jquery.js所以jquery.js必须在bxCarousel.js的前面.一般来说对jquery.js的引用放在前面. < ...
- 《你不常用的c#之三》:Action 之怪状
转载自csdn:http://blog.csdn.net/robingaoxb/article/details/6199891 例1: public static void Main() { Li ...