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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=;
const int maxm=;
const int inf=1e9+;
int n,m,k;
int s,t;
int head[maxn];
int Next[maxm];
int depth[maxn];
int cnt;
struct edge{
int u,v,w;
}e[maxm];
struct cz{
char s[];
}c[maxn];
struct dq{
char s1[],s2[];
}d[maxn];
struct zhq{
char s1[],s2[];
}z[maxn];
void addedge(int u,int v,int w)
{
cnt++;
Next[cnt]=head[u];
head[u]=cnt;
e[cnt].u=u;
e[cnt].v=v;
e[cnt].w=w;
cnt++;
Next[cnt]=head[v];
head[v]=cnt;
e[cnt].u=v;
e[cnt].v=u;
e[cnt].w=;
}
int bfs()
{
queue<int>q;
memset(depth,-,sizeof(depth));
depth[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=Next[i])
{
int v=e[i].v;
if((depth[v]==-)&&(e[i].w>))
{
depth[v]=depth[u]+;
q.push(v);
}
}
}
if(depth[t]==-)
return ;
return ;
}
int dfs(int u,int w)
{
if(u==t)
return w;
for(int i=head[u];i!=-;i=Next[i])
{
int v=e[i].v;
if((depth[v]==depth[u]+)&&(e[i].w>))
{
int di=dfs(v,min(w,e[i].w));
if(di>)
{
e[i].w-=di;
e[i^].w+=di;
return di;
}
}
}
return ;
}
int main()
{
while(~scanf("%d",&n))
{
memset(head,-,sizeof(head));
cnt=-;
int i;
s=;
for(i=;i<=n;i++)
{
scanf("%s",c[i].s);
}
scanf("%d",&m);
for(i=;i<=m;i++)
{
scanf("%s%s",d[i].s1,d[i].s2);
}
scanf("%d",&k);
for(i=;i<=k;i++)
{
scanf("%s%s",z[i].s1,z[i].s2);
}
t=n+m+k+;
for(i=;i<=m;i++)
{
addedge(s,i,);
for(int j=;j<=k;j++)
{
if(!strcmp(d[i].s2,z[j].s1))
addedge(i,j+m,inf);
}
for(int j=;j<=n;j++)
{
if(!strcmp(d[i].s2,c[j].s))
addedge(i,j+m+k,inf);
}
}
for(i=;i<=k;i++)
{
for(int j=;j<=k;j++)
{
if(i!=j&&!strcmp(z[i].s2,z[j].s1))
addedge(i+m,j+m,inf);
}
for(int j=;j<=n;j++)
{
if(!strcmp(z[i].s2,c[j].s))
addedge(i+m,m+k+j,inf);
}
}
for(i=;i<=n;i++)
{
addedge(m+k+i,t,);
}
int ans=;
while(bfs())
{
while(int di=dfs(,inf))
ans+=di;
}
printf("%d\n",m-ans);
}
return ;
}

网络流的算法,EK的比较简单,这是dinic的算法,其中有两个数组,不容易看懂,一个是head数组,一个是next数组。

next这个名字起的实际上也对,因为它是循环时候的那个  下一个   的意思,但是里面存入的是这个边上一个边的编号。

这样for循环的时候,bfs()for循环里面有判断条件,直接跳转到源,然后开始进去队列。

dfs() for 循环的时候,本身dfs就是回溯的一个算法,一直往回找,这样正顺应着dfs的思路,一直去寻找上一条边。

dinic网络流的更多相关文章

  1. DINIC网络流+当前弧优化

    DINIC网络流+当前弧优化 const inf=; type rec=record s,e,w,next:longint; end; var b,bb,d,q,tb:..] of longint; ...

  2. [codevs1227]草地排水<Dinic网络流最大流>

    题目链接:http://codevs.cn/problem/1993/ https://www.luogu.org/problemnew/show/P2740 之前一直都没去管网络流这算法,但是老师最 ...

  3. Dinic 网络流

    写个博客贴板子-- inline void add_edge(int x,int y,int z){ e[++tot].x=y,e[tot].cap=z; e[tot].next=h[x],h[x]= ...

  4. dinic网络流模板

    src:源点 sink:汇点 #include<queue> #include<iostream> #include<string.h> #include<s ...

  5. Internship-ZOJ2532(网络流求割边)

    Internship Time Limit: 5 Seconds      Memory Limit: 32768 KB CIA headquarter collects data from acro ...

  6. HDU 3416 Marriage Match IV dij+dinic

    题意:给你n个点,m条边的图(有向图,记住一定是有向图),给定起点和终点,问你从起点到终点有几条不同的最短路 分析:不同的最短路,即一条边也不能相同,然后刚开始我的想法是找到一条删一条,然后光荣TLE ...

  7. ZOJ 2532 网络流最小割

    求最小割的问题. 题意:已知网络中有n个源点,m的中转站(也就是节点),一个汇点(编号为0).给出网络,求一些边(增大这个边就可以增大汇点流量的边). 思路:一开始代码只找了有流=0就加入输出数组的情 ...

  8. POJ2987 Firing 最大权闭合图

    详情请参考http://www.cnblogs.com/kane0526/archive/2013/04/05/3001557.html 值得注意的地方,割边会把图分成两部分,一部分和起点相连,另一部 ...

  9. 【HDOJ】3505 Writing Robot

    挺好的一道题目,我的做法是kmp+Dinic网络流.kmp求子串在P中出现的次数,从而计算love值.网络流主要用来处理最优解.case2中p1的love值是8,p2的love值是7,最终T包含p1和 ...

随机推荐

  1. 51nod 1138 【数学-等差数列】

    思路: 很显然每个连续的序列都是等差数列, 那么我们利用等差数列求和公式. S=(a1+a1+k-1)k/2=(2·a1+k-1)*k/2;a1是首项,k是个数. 枚举k,首项最小为1,k最大,具体不 ...

  2. 【POJ - 2664】Prerequisites? (排序+查找)

    Prerequisites? 原文是English,这里直接就写中文吧 题意简述 k:已经选择的科目数:m:选择的科目类别:c:能够选择的科目数.r:要求最少选择的科目数量 在输入的k和m以下的一行是 ...

  3. Luogu P1736 创意吃鱼法【dp】By cellur925

    题目传送门 题意:给出一个01矩阵,找出一条对角线,使得对角线上的元素都为1,而对角线所在矩阵其他元素均为0,使得这样的对角线最长. 状态:$f[i][j]$表示以($i$,$j$)为对角线端点的最长 ...

  4. Python转载

    让Python的经验更多一点 Python while 1 和 while True 速度比较 Python %s和%r的区别

  5. laravel 配置站点域名

    访问一直报404错误 laravel端: default.conf server {        listen       80;        server_name  api.xxxx.com; ...

  6. springboot之项目打包

    通过win中的cmd或者idea中终端,打包并启动项目: 1.mvn package     [打包,在target中生成jar] 2.java -jar xxxxx.jar  [启动jar]

  7. [BZOJ1040][ZJOI2008]骑士 基环树DP

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1040 题目给出了$n$个点和$n$条无向边,即一棵基环树或者基环树森林. 如果题目给的关系 ...

  8. 批量部署Hadoop集群环境(1)

    批量部署Hadoop集群环境(1) 1. 项目简介: 前言:云火的一塌糊涂,加上自大二就跟随一位教授做大数据项目,所以很早就产生了兴趣,随着知识的积累,虚拟机已经不能满足了,这次在服务器上以生产环境来 ...

  9. mySQL ODBC 在windows 64位版上的驱动问题

    1,问题的起源 某次编辑一个asp文件,其中访问mysql数据库的连接字符串如下: "driver={mysql odbc 3.51 driver};server=localhost;uid ...

  10. Qt和Cocoa混合编程

    https://el-tramo.be/blog/mixing-cocoa-and-qt/