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. (转)ASP连接sql server实例解析

    本文转载自:http://blog.csdn.net/xys_777/article/details/5696276 1.首先确定自己的iis没有问题 2.其次确定自己sqlserver没有问题 然后 ...

  2. Java 字符串与对象进行比较 compareTo()

    Java 手册 compareTo public int compareTo(String anotherString) 按字典顺序比较两个字符串.该比较基于字符串中各个字符的 Unicode 值.按 ...

  3. [MVC 4] ActionResult 使用示例

    在控制器 HomeController.cs 中使用以下代码 public ActionResult Contact() { ViewBag.Message = "Your contact ...

  4. rpc简易实现-zookeeper

    一.RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的存在,如TCP或UDP, ...

  5. 常见的加密和解密算法—AES

    一.AES加密概述 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用 ...

  6. PHP中的=>,->,@,&,::,%

    在php中数组默认键名是整数,也可以自己定义任意字符键名(最好是有实际意义).如: $css=array('style'=>'0',‘color’=>‘green‘), 则$css['st ...

  7. c#中var类型用法同dynamic,object区别

    1.object万能对象:object只是个对象,是个强类型,在编译的时候确定类型了,只能用对象本身的方法属性等等,object想使用动态属性方法只能用反射. 2.var万能强类型. 3.dynami ...

  8. canvas给图形添加颜色

    canvas给图形添加颜色 合法属性 ctx.fillStyle = 'orange'; ctx.fillStyle = '#FFA500'; ctx.fillStyle = 'rgb(255, 16 ...

  9. 七、配置ssh keys连通github跟ssh-agent

    jenkins+github配置完成后,能够实现在提交pull request或者直接push时,能够将提交的代码拉去一份到服务器本地,并自动merge:但是代码拉去下来了,部署环境的时候却需要输入登 ...

  10. jquery的html代码中a的onclick的正确显示的代码

    jquery的html代码中a的onclick的正确显示的代码 需要转义一下,试了好久才试出来 img_delete.html('<a onclick="deleteImg(\''+s ...