HDU1532_Drainage Ditches(网络流/EK模板/Dinic模板(邻接矩阵/前向星))
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8599 Accepted Submission(s): 4005
clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to
Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
50
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 99999999
#define N 220
#define M 220
using namespace std;
int n,m,edge[N][N],flow,a[N],p[N];
queue<int>Q;
void ek()
{
while(1)
{
while(!Q.empty())
Q.pop();
memset(a,0,sizeof(a));
memset(p,0,sizeof(p));
Q.push(1);
a[1]=inf;
p[1]=1;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int v=1;v<=n;v++)
{
if(!a[v]&&edge[u][v]>0)
{
a[v]=min(a[u],edge[u][v]);
p[v]=u;
Q.push(v);
}
}
if(a[n])break;
}
if(!a[n])break;
for(int u=n;u!=1;u=p[u])
{
edge[p[u]][u]-=a[n];
edge[u][p[u]]+=a[n];
}
flow+=a[n];
}
}
int main()
{
int i,j,u,v,w;
while(~scanf("%d%d",&m,&n))
{
flow=0;
memset(edge,0,sizeof(edge));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
edge[u][v]+=w;
}
ek();
printf("%d\n",flow);
}
}
又学了Dinic算法。
。。
(基于邻接矩阵的)。前向星不知道怎么处理反向弧。。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 99999999
#define N 200
#define M 200
using namespace std;
int edge[N][N],flow,l[N],n,m;
int bfs()
{
memset(l,-1,sizeof(l));
queue<int>Q;
l[1]=0;
Q.push(1);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int v=1;v<=n;v++)
{
if(edge[u][v]&&l[v]==-1)
{
l[v]=l[u]+1;
Q.push(v);
}
}
}
if(l[n]>0)
return 1;
else return 0;
}
int dfs(int x,int f)
{
if(x==n)return f;
int a;
for(int i=1;i<=n;i++)
{
if(edge[x][i]&&(l[i]==l[x]+1)&&(a=dfs(i,min(f,edge[x][i]))))
{
edge[x][i]-=a;
edge[i][x]+=a;
return a;
}
}
return 0;
}
int main()
{
int i,j,u,v,w;
while(~scanf("%d%d",&m,&n))
{
memset(edge,0,sizeof(edge));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
edge[u][v]+=w;
}
int a=0;
flow=0;
while(bfs())
while(a=dfs(1,inf))
flow+=a;
printf("%d\n",flow);
}
return 0;
}
最终觉醒了,之前看了一串非递归的前向星dinic,,。老认为不正确。,,换了一种思路,写成递归调用dfs和邻接表一样,对于处理反向弧加流的话在建边的时候处理,建边一次建两条,正向和反向,这样就知道。反向弧在哪里了,,。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define N 300
#define M 30000
#define inf 99999999
using namespace std;
struct node
{
int u,v,w,r,next;
}edge[M]; int n,m,head[N],l[N],cnt;
void add(int u,int v,int w)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
edge[cnt].r=cnt+1;
head[u]=cnt++;
edge[cnt].u=v;
edge[cnt].v=u;
edge[cnt].w=0;
edge[cnt].next=head[v];
edge[cnt].r=cnt-1;
head[v]=cnt++;
}
int bfs()
{
queue<int >Q;
while(!Q.empty())
Q.pop();
Q.push(1);
memset(l,-1,sizeof(l));
l[1]=0;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
if(edge[i].w&&l[edge[i].v]==-1)
{
l[edge[i].v]=l[u]+1;
Q.push(edge[i].v);
}
}
}
if(l[n]>0)return 1;
else return 0;
}
int dfs(int x,int f)
{
int i,a;
if(x==n)return f;
for(i=head[x];i!=-1;i=edge[i].next)
{
if(edge[i].w&&l[edge[i].v]==l[x]+1&&(a=dfs(edge[i].v,min(f,edge[i].w))))
{
edge[i].w-=a;
edge[edge[i].r].w+=a;
return a;
}
}
return 0;
}
int main()
{
int i,j,u,v,w;
while(cin>>m>>n)
{
memset(head,-1,sizeof(head));
memset(edge,0,sizeof(edge));
cnt=0;
for(i=0;i<m;i++)
{
cin>>u>>v>>w;
add(u,v,w);
}
int ans=0,a;
while(bfs())
while(a=dfs(1,inf))
ans+=a;
cout<<ans<<endl;
}
}
HDU1532_Drainage Ditches(网络流/EK模板/Dinic模板(邻接矩阵/前向星))的更多相关文章
- poj-1459-最大流dinic+链式前向星-isap+bfs+stack
title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...
- 【模板】链式前向星+spfa
洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...
- 模板 Dijkstra+链式前向星+堆优化(非原创)
我们首先来看一下什么是前向星. 前向星是一种特殊的边集数组,我们把边集数组中的每一条边按照起点从小到大排序,如果起点相同就按照终点从小到大排序, 并记录下以某个点为起点的所有边在数组中的起始位置和 ...
- zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)
2131: Can Win Time Limit: 1 Sec Memory Limit: 128 MB Submit: 431 Solved: 50 SubmitStatusWeb Board ...
- 存图方式---邻接表&邻接矩阵&前向星
基于vector存图 struct Edge { int u, v, w; Edge(){} Edge(int u, int v, int w):u(u), v(v), w(w){} }; vecto ...
- 网络流--最大流dinic模板
标准的大白书式模板,除了变量名并不一样……在主函数中只需要用到 init 函数.add 函数以及 mf 函数 #include<stdio.h> //差不多要加这么些头文件 #includ ...
- 网络流--最大流--Dinic模板矩阵版(当前弧优化+非当前弧优化)
//非当前弧优化版 #include <iostream> #include <cstdio> #include <math.h> #include <cst ...
- 网络流-最大流 Dinic模板
#include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back #defin ...
- Drainage Ditches(网络流(EK算法))
计算最大流,EK算法模板题. #include <stdio.h> #include <string.h> #include <queue> using names ...
随机推荐
- PHPStorm.WebStrom等系列官方开发工具配置本地项目与运程服务器同步
PHPStorm.WebStrom配置本地项目与运程服务器同步 说明:PHPStorm.WebStrom等官方的系统开发工具配置本地项目与运程服务器同步的方法都基本一致没有,几乎没有什么不同之处,我们 ...
- [git 学习篇]git管理的是修改,并非文件
你会问,什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改. 为什么说Git管理的 ...
- xml和pandas结合处理的一个小例子-待完善
#!/usr/bin/env python3 # -*- coding:utf-8 -*- import pandas import json import xml.etree.ElementTree ...
- ubuntu14.04修改mysql默认编码
修改文件为/etc/mysql/my.cnf [client] default-character-set = utf8 (ps:client的设置没变) [mysqld] lower_case_ta ...
- 【bzoj4407】于神之怒加强版 莫比乌斯反演+线性筛
题目描述 给下N,M,K.求 输入 输入有多组数据,输入数据的第一行两个正整数T,K,代表有T组数据,K的意义如上所示,下面第二行到第T+1行,每行为两个正整数N,M,其意义如上式所示. 输出 如题 ...
- mysql监控指标
1.最大连接数监控 show VARIABLES like "max_connections"; //最大连接数 show global status like 'Threads_ ...
- bzoj 2803 [POI2012]prefixuffix hsh+性质
题目大意 bzoj 2803 对于两个串S1.S2,如果能够将S1的一个后缀移动到开头后变成S2,就称S1和S2循环相同.例如串ababba和串abbaab是循环相同的. 给出一个长度为n的串S,求满 ...
- xsy 1836 - Shop
from NOIP2016模拟题36 Description 商店里有n种背包和m种物品,物品体积为1到m,背包容积<=m 给出n个背包的容积 现在要求出这样一个物品集合,满足: 1)对于任意一 ...
- Python [拷贝copy / 深度拷贝deepcopy] | 可视化理解
Python 是一门面向对象的语言, 在Python一切皆对象. 每一个对象都有由以下三个属性组成: ------------------------------------------------- ...
- 一简单c++程序之反汇编
#include<iostream> using namespace std; class point3d; class point2d; class point3d { private: ...