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. 洛谷 - P2762 - 太空飞行计划问题 - 最小割

    https://www.luogu.org/problemnew/solution/P2762 最小割对应的点,在最后一次更新中dinic的bfs会把他的dep重置掉.所以可以根据这个性质复原最小割. ...

  2. XTU1267:Highway(LCA+树的直径)

    传送门 题意 有n个小镇,Bobo想要建造n-1条边,并且如果在u到v建边,那么花费是u到v的最短路长度(原图),问你最大的花费. 分析 比赛的时候没做出来,QAQ 我们首先要找到树的直径起点和终点, ...

  3. hdoj5805【模拟】

    BestCoder Round #86 B NanoApe Loves Sequence 题意: 中文题,题意就算了 思路: 弱的思路- 找一个最大,和第二大,第三大,标记下标(前面那个) ①:如果是 ...

  4. Java 在线反编译

    使用jd-gui反编译java提示 // INTERNAL ERROR // 的类,用在线反编译直接反编译.class http://www.showmycode.com/

  5. 聊聊Java并发面试问题之公平锁与非公平锁是啥?

    一.什么是非公平锁? 先来聊聊非公平锁是啥,现在大家先回过头来看下面这张图. 如上图,现在线程1加了锁,然后线程2尝试加锁,失败后进入了等待队列,处于阻塞中.然后线程1释放了锁,准备来唤醒线程2重新尝 ...

  6. 问题 3: 糖果数(candy)

    问题 3: 糖果数(candy) 题目描述 学校准备去春游,委托小明分发糖果,每位同学一袋,数量随机. 一共有N袋糖果,编号为1到N,小明拿了从编号a袋到编号b袋的糖果去分发,小明想知道,他一共拿了多 ...

  7. P1226神经网络

    提交了7次,看了无数题解,要死啊~~~.(无限吐槽这道题...) 据说是Toposort,我其实也不是很清楚,反正BFS就可以过:写题之前先把题看懂: 根据公式,因为入度为零的点不会被传递,所以阈值是 ...

  8. poj2893 M*N puzzle 【n*m数码问题小结】By cellur925

    题目传送门 这个问题是来源于lydrainbowcat老师书上讲排序的一个扩展.当时讲的是奇数码问题,其实这种问题有两种问法:一种局面能否到另一种局面.到达目标局面的最小步数. 本文部分内容引用于ly ...

  9. macos php安装扩展sqlsrv连接sqlserver

    Install the PHP Drivers for SQL Serve sudo pecl install pdo_sqlsrv   sudo pecl install sqlsrv 微软官方文档 ...

  10. 【模板】c++动态数组vector

    相信大家都知道$C$++里有一个流弊的$STL$模板库.. 今天我们就要谈一谈这里面的一个容器:动态数组$vector$. $vector$实际上类似于$a[]$这个东西,也就是说它重载了$[]$运算 ...