#include<iostream>
/*
题意:就是寻找从源点到汇点的最大流!
要注意的是每两个点的流量可能有多个,也就是说有重边,所以要把两个点的所有的流量都加起来
就是这两个点之间的流量了! 思路:建图之后直接套用最大流算法(EK, 或者是Dinic算法) 图解Dinic算法流程!

*/
#include<queue>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define INF 0x3f3f3f3f3f3f3f3f
#define N 205
using namespace std;
typedef long long LL;
LL cap[N][N]; int m, n;
LL maxFlow;
int d[N];
queue<int>q; bool bfs(){
q.push();
memset(d, , sizeof(d));
d[]=;
while(!q.empty()){
int u=q.front();
q.pop();
for(int v=; v<=n; ++v)
if(!d[v] && cap[u][v]>){
d[v]=d[u]+;
q.push(v);
}
}
if(!d[n]) return false;
return true;
} LL dfs(int u, LL flow){
if(u==n) return flow;
for(int v=; v<=n; ++v)
if(d[v]==d[u]+ && cap[u][v]>){
LL a=dfs(v, min(flow, cap[u][v]));
if(a==) continue;//如果a==0 说明没有找到从起点到汇点的增广路, 然后换其他路接着寻找!
cap[u][v]-=a;
cap[v][u]+=a;
return a;
}
return ;
} void Dinic(){
LL flow;
while(bfs()){//利用bfs构造好层次图,这样dfs在寻找阻塞流的时候,就不会盲目的寻找了!
while(flow=dfs(, INF)) maxFlow+=flow;//利用构造好的层次图不断的寻找阻塞流!
}
} int main(){
while(scanf("%d%d", &m, &n)!=EOF){
memset(cap, , sizeof(cap));
while(m--){
int u, v;
LL w;
scanf("%d%d%lld", &u, &v, &w);
cap[u][v]+=w;
}
maxFlow=;
Dinic();
printf("%lld\n", maxFlow);
}
return ;
}
 //EK算法同样搞定
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef __int64 LL;
LL cap[][];
int pre[];
LL a[];
int m, n;
queue<int>q;
LL maxFlow;
bool spfa(){
while(!q.empty()) q.pop();
memset(a, , sizeof(a));
q.push();
a[]=INF;
while(!q.empty()){
int u=q.front();
q.pop();
for(int v=; v<=n; ++v)
if(!a[v] && cap[u][v]>){
pre[v]=u;
a[v]=min(a[u], cap[u][v]);
q.push(v);
}
if(a[n]) break;
}
if(!a[n]) return false;
return true;
} void EK(){
maxFlow=;
while(spfa()){
int u=n;
maxFlow+=a[n];
while(u!=){
cap[pre[u]][u]-=a[n];
cap[u][pre[u]]+=a[n];
u=pre[u];
}
}
}
int main(){
while(scanf("%d%d", &m, &n)!=EOF){
memset(cap, , sizeof(cap));
while(m--){
int u, v;
LL w;
scanf("%d%d%I64d", &u, &v, &w);
cap[u][v]+=w;
}
EK();
printf("%I64d\n", maxFlow);
}
return ;
}

poj1273Drainage Ditches的更多相关文章

  1. POJ1273Drainage Ditches[最大流]

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 71559   Accepted: 2784 ...

  2. POJ-1273-Drainage Ditches(网络流之最大流)

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

  3. POJ-1273-Drainage Ditches 朴素增广路

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 70588   Accepted: 2743 ...

  4. 图论-网络流-最大流--POJ1273Drainage Ditches(Dinic)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 91585   Accepted: 3549 ...

  5. POJ-1273Drainage Ditches(网络流入门题,最大流)

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

  6. poj1273--Drainage Ditches(最大流Edmond-Karp算法 邻接表实现)

    最大流模板题 大部分Edmond-Karp算法代码都是邻接矩阵实现,试着改成了邻接表. #include <iostream> #include <cstdio> #inclu ...

  7. 【最大流Dinic模板】HDU1532&POJ1273-Drainage Ditches(16/3/6更正)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  8. POJ 1273 Drainage Ditches题解——S.B.S.

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67823   Accepted: 2620 ...

  9. I:trainage Ditches

    总时间限制: 1000ms 内存限制: 65536kB描述Every time it rains on Farmer John's fields, a pond forms over Bessie's ...

随机推荐

  1. [Leetcode][JAVA] Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  2. 第53讲:Scala中结构类型实战详解

    今天学习了scala的结构类型,让我们看看代码 class Structural {def open() = print("A class interface opened") } ...

  3. Android中的IntentService

    首先说下,其他概念:Android中的本地服务与远程服务是什么? 本地服务:LocalService 应用程序内部------startService远程服务:RemoteService androi ...

  4. 字符串匹配的Boyer-Moore算法

    作者: 阮一峰    http://www.ruanyifeng.com/blog/2013/05/boyer-moore_string_search_algorithm.html 上一篇文章,我介绍 ...

  5. Android ViewPager 用法

    Android ViewPager 用法 场景:一般第一次打开应用程序时,程序会有一个提示页来给展现应用程序都有哪些功能:或者程序更新时,又更新哪些新特性,都可以使用ViewPager Demo 描述 ...

  6. TFT-LCD的相关概念

    显示尺寸(display size) 是指实际可视区域的对角线长度,单位是英寸,简称寸(1英寸=2.54厘米). 长宽比(aspect ratio) 是指TFT-LCD可视区域的长度和宽度之比,也叫做 ...

  7. C++混合编程之idlcpp教程Python篇(5)

    上一篇在这  C++混合编程之idlcpp教程Python篇(4) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程PythonTutorial3中,同样加入了三个文件:Py ...

  8. sqlserver 读取xml 字符串方法

    declare @xml xml declare @propertyName varchar(50)  declare @str nvarchar(max)   set @propertyName = ...

  9. 【腾讯Bugly干货分享】深入理解 ButterKnife,让你的程序学会写代码

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/578753c0c9da73584b025875 0.引子 话说我们做程序员的,都 ...

  10. java提高篇(二五)-----HashTable

          在java中与有两个类都提供了一个多种用途的hashTable机制,他们都可以将可以key和value结合起来构成键值对通过put(key,value)方法保存起来,然后通过get(key ...