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和 ...
随机推荐
- IT兄弟连 JavaWeb教程 经典案例3
案例需求:写一个用户登录的html页面有账号和密码,并在登录的Servlet中获取登录的账号和密码,如果账号是abc密码是123则重定向到main.html,否则重定向到login.html. 案例实 ...
- python 处理时间和日期
转自: https://www.cnblogs.com/65702708/archive/2011/04/17/2018936.html Python格式化日期时间的函数为datetime.datet ...
- C#递归拷贝文件夹下文件以及文件夹
public bool CopyOldLabFilesToNewLab(string sourcePath, string savePath) { if (!Directory.Exists(save ...
- 简单实现人工智能:百度aip+tuling123
目录结构: app.py # -*- coding: utf-8 -*- # __author: ward # data: 2018/12/21 # @File: app from flask imp ...
- Codeforces Round #321 (Div. 2)
水 A - Kefa and First Steps /************************************************ * Author :Running_Time ...
- cpp extern 关键字用法
语法说明: extern 可以置于变量或者函数前,以标示变量或者函数的在别的文件中定义,提示编译器遇到此变量和函数后,在其他模块中寻找其定义.此外extern也可用来进行链接指定. 即 extern ...
- java常用类要点总结
- apt-get的一些坑
apt-get update:更新安装列表apt-get upgrade:升级软件apt-get install software_name :安装软件apt-get --purge remove ...
- java实现课堂随机点名小程序
通过jdbc连接数据库实现读取学生花名册进行随机点名! ~jdbc连接mysql数据库 || 注释部分代码可通过读取.txt文档实现显示学生信息 ~通过点击开始按钮实现界面中间标签不断更新学生信息 ...
- CCF|学生排队|Java
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Sc ...