[POJ1087]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 Description
|
|
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 |
|
输出描述 Output Description
|
|
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
|
|
数据范围及提示 Data Size & Hint
|
|
题目中说了!
|
此题最大的难点在于能否把题读懂。读懂之后就很显然是一道最大流的题。我觉得这道题建图里面的点的设计很妙。字符串处理,一个find函数就能找到对应编号,不错!
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<string>
using namespace std;
typedef long long LL;
#define mem(a,b) memset(a,b,sizeof(a))
inline int read()
{
int x=,f=;char c=getchar();
while(!isdigit(c)){if(c=='-')f=-;c=getchar();}
while(isdigit(c)){x=x*+c-'';c=getchar();}
return x*f;
}
const int maxn=,maxm=,oo=,maxs=;
int n,mm,k,h1[maxs],h3[maxs],h4[maxs];
string str1[maxs],str2[maxs],str3[maxs],str4[maxs],str5[maxs],list[*maxs];
struct Edge
{
int u,v,f,next;
Edge() {}
Edge(int _1,int _2,int _3,int _4):u(_1),v(_2),f(_3),next(_4) {}
}e[*maxm];
struct Dinic
{
int first[maxn],dis[maxn],cur[maxn],a,b,c,ce,s,t,N;
bool vis[maxn];queue <int> Q;
void addEdge(int a,int b,int c)
{
e[++ce]=Edge(a,b,c,first[a]);first[a]=ce;
e[++ce]=Edge(b,a,,first[b]);first[b]=ce;
}
int find(string a)
{
for(int i=;i<=N;i++)if(a==list[i])return i;
list[++N]=a;return N;
}
void init_build()
{
mem(first,-);ce=-;
s=;t=;N=;
for(int i=;i<=n;i++)
{
list[++N]=str1[i];
addEdge(s,i+,);
}
for(int i=;i<=mm;i++)
{
list[++N]=str2[i];
int t1=find(str5[i]);
addEdge(t1,N,);addEdge(N,t,);
}
for(int i=;i<=k;i++)
{
int t1=find(str3[i]),t2=find(str4[i]);
addEdge(t2,t1,oo);
}
}
bool BFS()
{
mem(dis,);mem(vis,);
while(Q.size())Q.pop();
dis[s]=;vis[s]=;Q.push(s);
while(Q.size())
{
int now=Q.front();Q.pop();
for(int i=first[now];i!=-;i=e[i].next)
if(e[i].f && !vis[e[i].v])
{
vis[e[i].v]=;
dis[e[i].v]=dis[now]+;
Q.push(e[i].v);
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t || a==)return a;
int flow=,tmp;
for(int& i=cur[x];i!=-;i=e[i].next)
if(dis[e[i].v]==dis[x]+ && (tmp=dfs(e[i].v,min(a,e[i].f)))>)
{
e[i].f-=tmp;e[i^].f+=tmp;
a-=tmp;flow+=tmp;
if(a==)break;
}
return flow;
}
int maxflow()
{
int flow=;
while(BFS())
{
for(int i=;i<=N;i++)cur[i]=first[i];
flow+=dfs(s,oo);
}
return flow;
}
}fyh;
int main()
{
n=read();
for(int i=;i<=n;i++)cin>>str1[i];
mm=read();
for(int i=;i<=mm;i++)cin>>str2[i]>>str5[i];
k=read();
for(int i=;i<=k;i++)cin>>str3[i]>>str4[i];
fyh.init_build();
printf("%d\n",mm-fyh.maxflow());
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: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(网络流)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 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 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.问最少有多少个插头被剩下. 题解: 最大流或者二分图匹配.然而我不知道怎么打二分图匹配..打了最大流.这题字符串比较坑 ...
- POJ-1087 A Plug for UNIX (网络流)
思路 电器数1 ~ 100,附带100种接口,注意题目:You notice that some of the devices use plugs for which there is no rece ...
随机推荐
- Leetcode 第137场周赛解题报告
今天的比赛的题目相对来说比较「直白」,不像前几周都是一些特定的算法,如果你没学过不可能想出来. 做了这些周,对leetcode比赛的题目也发现了一些「规律」. 一般前两道题都很「简单」,只要有想法,直 ...
- 调用其他python脚本文件里面的类和方法
问题描述: 自己编写了若干个Python脚本. 在testC.py里面需要调用testA.py和testB.py里面的若干类和方法.要怎么办? 需要都打包.安装,再去调用吗? 其实不必那么麻烦. 这里 ...
- html5手机端播放音效不卡的方法
html5手机端播放音效不卡的方法线下载http://wxserver.knowway.cn/solosea/js/audioEngine.js 这个是性能不错 然后直接播放音效就可以了 audioE ...
- Java匹马行天下之JavaSE核心技术——工具类
Java匹马行天之JavaSE核心技术——工具类 一.Object类 java.lang.ObjectObject类是所有类直接或间接的父类 常用的方法: toString():以字符串形式返回对象的 ...
- Application类-多窗口交互
我们在派生自Application类中出来放置响应应用程序事件的代码外,还可以放置一些完成其他任务的代码. 在此之前要知道: 如何获取应用程序的Application对象: //App是一个继承自Ap ...
- MySql 获取数据库的所有表名
目录 写在前面 根据数据库获取该数据库下所有的表名 根据表名获取列名与列值 写在前面 在实现某个功能的时候,需要使用MySql数据库获取某数据的所有的表名以及该表名的所有列名与列值. 根据数据库获取该 ...
- Python: 把txt文件转换成csv
最近在项目上需要批量把txt文件转成成csv文件格式,以前是手动打开excel文件,然后导入txt来生产csv文件,由于这已经变成每周需要做的事情,决定用python自动化脚本来实现,思路: 读取文件 ...
- WPF控件介绍(2)
上一章讲到了布局.这点就有点类似建筑设计.第一步是出图纸.整体的结构.而第二步就是堆砌, 建筑学里面也会有很多描述, 例如砖头,水泥.玻璃.瓷板.而在WPF中, 这一切的基础也就是控件.用于填充结构的 ...
- Java诊断利器Arthas优雅排查生产环境
前言 Arthas 是Alibaba开源的Java诊断工具.在线排查问题,无需重启:动态跟踪Java代码:实时监控JVM状态.对分秒必争的线上异常,Arthas可帮助我们快速诊断相关问题. 下载安装 ...
- 线程状态---Day24
线程状态概述: 当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态.在线程的生命周期中, 有几种状态呢?在API中 java.lang.Thread.State 这个枚举中 ...