dinic网络流
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
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
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网络流的更多相关文章
- DINIC网络流+当前弧优化
DINIC网络流+当前弧优化 const inf=; type rec=record s,e,w,next:longint; end; var b,bb,d,q,tb:..] of longint; ...
- [codevs1227]草地排水<Dinic网络流最大流>
题目链接:http://codevs.cn/problem/1993/ https://www.luogu.org/problemnew/show/P2740 之前一直都没去管网络流这算法,但是老师最 ...
- 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]= ...
- dinic网络流模板
src:源点 sink:汇点 #include<queue> #include<iostream> #include<string.h> #include<s ...
- Internship-ZOJ2532(网络流求割边)
Internship Time Limit: 5 Seconds Memory Limit: 32768 KB CIA headquarter collects data from acro ...
- HDU 3416 Marriage Match IV dij+dinic
题意:给你n个点,m条边的图(有向图,记住一定是有向图),给定起点和终点,问你从起点到终点有几条不同的最短路 分析:不同的最短路,即一条边也不能相同,然后刚开始我的想法是找到一条删一条,然后光荣TLE ...
- ZOJ 2532 网络流最小割
求最小割的问题. 题意:已知网络中有n个源点,m的中转站(也就是节点),一个汇点(编号为0).给出网络,求一些边(增大这个边就可以增大汇点流量的边). 思路:一开始代码只找了有流=0就加入输出数组的情 ...
- POJ2987 Firing 最大权闭合图
详情请参考http://www.cnblogs.com/kane0526/archive/2013/04/05/3001557.html 值得注意的地方,割边会把图分成两部分,一部分和起点相连,另一部 ...
- 【HDOJ】3505 Writing Robot
挺好的一道题目,我的做法是kmp+Dinic网络流.kmp求子串在P中出现的次数,从而计算love值.网络流主要用来处理最优解.case2中p1的love值是8,p2的love值是7,最终T包含p1和 ...
随机推荐
- Codeforces 2 A. Winner
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈....... 先让我笑完................ 就是一道撒比题啊,一开始是题目看错= =.是,但是后面还是自己不仔细错的.....不存在题目坑这种情况 ...
- 解决wubi安装ubuntu时要下载系统映像文件问题
转载:一个人的旅行的博客(http://www.cnblogs.com/rollenholt/articles/2607433.html) 下面我介绍解决wubi安装ubuntu时要去官网下载系统映像 ...
- 同一台服务器上部署多个Tomcat的配置修改方法
同一服务器部署多个tomcat时,存在端口号冲突的问题,所以需要修改tomcat配置文件server.xml,以tomcat7为例. 首先了解下tomcat的几个主要端口: <Server po ...
- 跟我一起玩Win32开发(20):浏览文件夹
最近忙于一些相当无聊的事情,还没忙完,不过,博客还是要写的,不然我头顶上会多了几块砖头. 在上一篇博文中,我们浏览了文件,今天我们也浏览一下目录,如何? 浏览目录我们同样有两个规矩,用托管类库的我就不 ...
- Codeforces Round #302 (Div. 1) 训练
链接: http://codeforces.com/contest/543 过程: 惨淡的只做出了A和C 题解: A 题解: 简单的一道题 我们用$dp[i][j]$表示当前考虑到前num个人(这个另 ...
- fzu Problem 2198 快来快来数一数 (快速幂+优化)
题目链接: Problem 2198 快来快来数一数 题目描述: 给出n个六边形排成一排,a[i]代表i个六边形能组成的生成树个数,设定s[i]等于a[1]+a[2]+a[3]+....+a[i- ...
- stack(单调栈) POJ 2082 Terrible Sets
题目传送门 题意:紧贴x轴有一些挨着的矩形,给出每个矩形的长宽,问能组成的最大矩形面积为多少 分析:用堆栈来维护高度递增的矩形,遇到高度小的,弹出顶部矩形直到符合递增,顺便计算矩形面积,且将弹出的宽度 ...
- 牛客国庆集训派对Day_7
A.Relic Discovery 题目描述 Recently, paleoanthropologists have found historical remains on an island in ...
- Collection2
- 生产环境中配置的samba
实验需求: 由于实验室纳新一帮新孩子,搭建samba主要是临时共享一些学习资源的,基本上大家用的都是windows.而且这个服务可以让他们在校园的里的个个角落都可以访问到,所以给挂了学校的公网,不过我 ...