#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m, S, T;
const int MAXN = ;//点数的最大值
const int MAXM = ;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow; } edge[MAXM]; //注意是MAXM
int tol;
int head[MAXN];
void init()
{
tol = ;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int w,int rw = )
{
edge[tol].to = v;
edge[tol].cap = w;
edge[tol].flow = ;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = rw;
edge[tol].flow = ;
edge[tol].next = head[v];
head[v] = tol++;
}
int Q[MAXN];
int dep[MAXN],cur[MAXN],sta[MAXN];
bool bfs(int s,int t,int n)
{
int front = ,tail = ;
memset(dep,-,sizeof(dep[])*(n+));
dep[s] = ;
Q[tail++] = s;
while(front < tail)
{
int u = Q[front++];
for(int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap > edge[i].flow && dep[v] == -)
{
dep[v] = dep[u] + ;
if(v == t)
return true;
Q[tail++] = v;
}
}
}
return false;
}
int dinic(int s,int t,int n)
{
int maxflow = ;
while(bfs(s,t,n))
{
for(int i = ; i < n; i++)
cur[i] = head[i];
int u = s, tail = ;
while(cur[s] != -)
{
if(u == t)
{
int tp = INF;
for(int i = tail-; i >= ; i--)
tp = min(tp,edge[sta[i]].cap-edge[sta[i]].flow);
maxflow += tp;
for(int i = tail-; i >= ; i--)
{
edge[sta[i]].flow += tp;
edge[sta[i]^].flow -= tp;
if(edge[sta[i]].cap-edge[sta[i]].flow == )
tail = i;
} u = edge[sta[tail]^].to;
}
else if(cur[u] != - && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + == dep[edge[cur[u]].to])
{
sta[tail++] = cur[u];
u = edge[cur[u]].to;
}
else
{
while(u != s && cur[u] == -)
u = edge[sta[--tail]^].to;
cur[u] = edge[cur[u]].next;
}
}
}
return maxflow;
}
int main()
{
while(~scanf("%d %d", &m, &n))
{
init();
for(int i = ; i < m; i++)
{
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
addedge(u,v, w);
}
printf("%d\n", dinic(, n, n));
}
return ;
}

POJ 1273 Drainage Ditches(最大流Dinic 模板)的更多相关文章

  1. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  2. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

  3. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  4. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  5. POJ 1273 Drainage Ditches | 最大流模板

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  6. POJ 1273 Drainage Ditches 最大流

    这道题用dinic会超时 用E_K就没问题 注意输入数据有重边.POJ1273 dinic的复杂度为O(N*N*M)E_K的复杂度为O(N*M*M)对于这道题,复杂度是相同的. 然而dinic主要依靠 ...

  7. poj 1273 Drainage Ditches(最大流)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  8. POJ 1273 Drainage Ditches (网络最大流)

    http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  9. POJ 1273 Drainage Ditches (网络流Dinic模板)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

随机推荐

  1. Connected Components? Codeforces - 920E || 洛谷 P3452 &&bzoj1098 [POI2007]BIU-Offices

    https://codeforces.com/contest/920/problem/E https://www.luogu.org/problemnew/show/P3452 https://www ...

  2. HTML超链接的使用

    基本语法 <a href="" target="打开方式" name="页面锚点名称">链接文字或图片</a> 属性 ...

  3. npm scripts 常用规范总结

    npm scripts 常用规范总结 随着 npm scripts 使用越来越多,需要规范一下, npm run 的使用词. 定义 我定义如下形式: npm run <action>:&l ...

  4. mvc 连接数据库但单复值得问题

    1.  The model backing the ‘MusicStoreDBContext‘ context has changed since the database was created. ...

  5. APICloud手机APP开发

    官网 http://docs.apicloud.com/

  6. ES-Mac OS环境搭建(1)

    前言 由于elasticsearch依赖Java,所以先要配置上Java环境,并且Java JDK必须要求1.8以上,这里以安装Java 1.8为例.安装环境如下: elasticsearch6.5. ...

  7. Android 图片在SD卡及包下的存储

    public class FileBitmap { /** * 获取sd卡中的bitmap,bitmap可见 * * @param bitmap * 读取bitmap的路径 * @return bit ...

  8. 使用 Visual Studio 2017 部署 Azure 应用服务的 Web 应用

    本快速入门介绍了如何使用 Visual Studio 2017 创建并部署 Azure Web 应用.在本教程中完成的所有操作均符合1元试用条件. 本快速入门介绍了如何使用 Visual Studio ...

  9. Azure 项目构建 - 构建 WordPress 网站

    本课程主要介绍了如何基于 Azure Web 应用和 WordPress 快速构建网站,实践讲解如何使用 Azure Web 应用,创建并连接 MySQL Database on Azure, 使用 ...

  10. java冒泡排序和快速排序代码

    冒泡排序: package nicetime.com; //基本思想:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,// 让较大的数往下沉,较小的往上 ...