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

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

Source

——————————————————————————————————————————————————————

题目的意思是给你n个插头,m个电器,每个电器有对应的插头,k种 插头(数量不限)插头可以将一个孔转化为另一个(注意只有1个孔,如输入B X意思是把X转化为B)
问最多给多少电器供电?

思路:网络最大流。类似于电流的思路,建图时可以把源点和每个电源上的插孔连容量为1;对于插座,可把插头和插孔连容量为无穷,如输入B X那么X连B;对于电器,可把插头连电器容量为1,如输入comb X那么X连comb,最后把所有电器和汇点连。
因为是字符串,所以推荐map处理
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 500 struct node
{
int u, v, next, cap;
} edge[MAXN*MAXN];
int nt[MAXN], s[MAXN], d[MAXN], visit[MAXN];
int cnt;
int ct; void init()
{
cnt = 0;
memset(s, -1, sizeof(s));
} void add(int u, int v, int c)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cap = c;
edge[cnt].next = s[u];
s[u] = cnt++;
edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].cap = 0;
edge[cnt].next = s[v];
s[v] = cnt++;
} bool BFS(int ss, int ee)
{
memset(d, 0, sizeof d);
d[ss] = 1;
queue<int>q;
q.push(ss);
while (!q.empty())
{
int pre = q.front();
q.pop();
for (int i = s[pre]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (edge[i].cap > 0 && !d[v])
{
d[v] = d[pre] + 1;
q.push(v);
}
}
}
return d[ee];
} int DFS(int x, int exp, int ee)
{
if (x == ee||!exp) return exp;
int temp,flow=0;
for (int i = nt[x]; ~i ; i = edge[i].next, nt[x] = i)
{
int v = edge[i].v;
if (d[v] == d[x] + 1&&(temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
{
edge[i].cap -= temp;
edge[i ^ 1].cap += temp;
flow += temp;
exp -= temp;
if (!exp) break;
}
}
if (!flow) d[x] = 0;
return flow;
} int Dinic_flow(int ss, int ee)
{
int ans = 0;
while (BFS(ss, ee))
{
for (int i = 0; i < ct; i++) nt[i] = s[i];
ans+= DFS(ss, INF, ee);
}
return ans;
} int main()
{
int n,m,x;
string a,b;
while(~scanf("%d",&n))
{
map<string,int>mp;
init();
ct=2;
for(int i=0; i<n; i++)
{
cin>>a;
mp[a]=ct++;
add(0,mp[a],1);
}
scanf("%d",&m);
for(int i=0; i<m; i++)
{
cin>>a>>b;
mp[a]=ct++;
add(mp[a],1,1);
if(!mp[b])
mp[b]=ct++;
add(mp[b],mp[a],1);
}
scanf("%d",&x);
for(int i=0; i<x; i++)
{
cin>>a>>b;
if(!mp[a])
mp[a]=ct++;
if(!mp[b])
mp[b]=ct++;
add(mp[b],mp[a],INF);
}
printf("%d\n",m-Dinic_flow(0,1));
}
return 0;
}






POJ1087 A Plug for UNIX 2017-02-12 13:38 40人阅读 评论(0) 收藏的更多相关文章

  1. HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏

    Nightmare Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth w ...

  2. A Plug for UNIX 分类: POJ 图论 函数 2015-08-10 14:18 2人阅读 评论(0) 收藏

    A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14786 Accepted: 4994 Desc ...

  3. Safecracker 分类: HDU 搜索 2015-06-25 21:12 12人阅读 评论(0) 收藏

    Safecracker Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...

  4. ios UIKit动力 分类: ios技术 2015-07-14 12:55 196人阅读 评论(0) 收藏

    UIkit动力学是UIkit框架中模拟真实世界的一些特性. UIDynamicAnimator 主要有UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性. 它一般有两种初始 ...

  5. iOS开发之监听键盘高度的变化 分类: ios技术 2015-04-21 12:04 233人阅读 评论(0) 收藏

    最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用textView来做,当textView成为第一响应者的时候它的Y值随着键盘高度的改变而改变,保证textView紧贴着键盘,但又 ...

  6. POJ1789 Truck History 2017-04-13 12:02 33人阅读 评论(0) 收藏

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27335   Accepted: 10634 D ...

  7. ZOJ3770Ranking System 2017-04-14 12:42 52人阅读 评论(0) 收藏

    Ranking System Time Limit: 2 Seconds      Memory Limit: 65536 KB Few weeks ago, a famous software co ...

  8. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

  9. Maya Calendar 分类: POJ 2015-06-11 21:44 12人阅读 评论(0) 收藏

    Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 70016   Accepted: 21547 D ...

随机推荐

  1. .NET 4.5 HttpClient 中使用Cookie

    为了使用.NET 4.5的HttpClient从WIN2K3换成了WIN7.装VS2010,结果告诉我VS2010不支持.NET 4.5.又装VS2012,接着装.NET FRAMEWORK 4.5. ...

  2. 剪贴板增强---Kawvin增强剪贴板_V2.0

    #Persistent SetWorkingDir,%A_ScriptDir% ;设置工作目录 #MaxThreadsPerHotkey ;最大热键数量 #NoEnv ;#Warn #SingleIn ...

  3. mac sublime切换编辑语言的方法(添加其他版本的python)

    在sublime中指定python版本,操作如下: Sublime——tools——build system——new build system 把文件中的内容替换为 { "cmd" ...

  4. MySQL锁之二:锁相关的配置参数

    锁相关的配置参数: mysql> SHOW VARIABLES LIKE '%timeout%'; +-----------------------------+----------+ | Va ...

  5. while循环优化版本-for循环

    for i in range(0,10): if i <3: print("loop ",i) else : continue print("hehe...&quo ...

  6. 给iOS开发新手送点福利,简述UISlide的属性和用法

    UISlide属性 1.   minimumValue  : 当值可以改变时,滑块可以滑动到最小位置的值,默认为0.0 _slider.minimumValue = 10.0; 2.   maximu ...

  7. Python普通方法、静态方法、类方法

    开始 # -*-coding:utf-8-*- # 普通方法,类方法,静态方法的区别 __metaclass__ = type class Tst: name = 'tst' data = 'this ...

  8. HTML常用技术

    1. 使用a标签实现文件的上传下载 完整代码: <a class="btn btn-success btn-sm" href="http://sf.jb51.net ...

  9. Excel2013复制内容粘贴到刷选的数据表中

    如何将复制内容粘贴到筛选后表格中: 1.需要一个表格:我要把这个表格里姓名列不包含A的项改为另外一个表中的数据列: 2.在姓名右侧插入两列,列A.列B: 3.在列A下第一格输入1,向下序列填充: 4. ...

  10. selenium+python自动化81-html报告优化(饼图+失败重跑+兼容python2&3)

    优化html报告 为了满足小伙伴的各种变态需求,为了装逼提升逼格,为了让报告更加高大上,测试报告做了以下优化: 测试报告中文显示,优化一些断言失败正文乱码问题 新增错误和失败截图,展示到html报告里 ...