POJ 1273
给出M条边,N个点,求源点1到汇点N的最大流量。

本文主要就是附上dinic的模板,供以后参考。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <string.h> /*
POJ 1273
dinic算法模板 边是有向的,而且存在重边,且这里重边不是取MAX,而是累加和
*/
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
int pri[maxn];
long long sum; //计算总流量
int s,t; //s:源点 t:汇点
int n,m; struct Edge{
int c,f;
}maps[maxn][maxn]; int min(int a,int b){
return a<b?a:b;
}
//每次先BFS,看看是否存在从源点到汇点的增广路
bool BFS() {
queue<int> q;
memset(pri,,sizeof(pri));
pri[]=;
q.push();
while(!q.empty()) {
int temp=q.front();
q.pop();
for(int i=; i<=m; i++) {
if(!pri[i] && maps[temp][i].c-maps[temp][i].f){
pri[i]=pri[temp]+;
if(i==t)
return true; //即如果可以流到汇点,直接return true
q.push(i);
}
}
}
//if(pri[m]>0)
// return true;
return false;
} //p表示当前节点,flow表示该节点通过的流量
int dinic(int p,int flow){
if(p==t){
return flow;
}
int f=flow;
//int value=0;
for(int i=;i<=m;i++){
if(pri[i]==pri[p]+ && maps[p][i].c-maps[p][i].f){
int a=maps[p][i].c-maps[p][i].f; //a为该边可以增加的流量
int ff=dinic(i,min(a,flow)); //ff为路径中所有a的最小值,即为该条路中可以增加的流量
maps[p][i].f+=ff; //正向边
maps[i][p].f-=ff; //逆向边
//value+=ff;
flow-=ff;
if(flow<=)
break; //优化剪枝
}
}
//return value;
if(f-flow<=)
pri[p]=;//如果从p点流出去的流量<=0,那么设置pri[p]的值为0,之后在dinic中就不考虑到p点的情况了。
return f-flow;
}
void init(){
for(int i=;i<=m;i++){
for(int j=;j<=m;j++){
maps[i][j].c=maps[i][j].f=;
}
}
}
int main() {
int a,b,c;
s=;
while(scanf("%d%d",&n,&m)!=EOF){
init();
//memset(maps,0,sizeof(maps));
sum=;
t=m;
for(int i=;i<=n;i++){
scanf("%d%d%d",&a,&b,&c);
maps[a][b].c+=c; //该题有重边,这里要+=,不是去max
}
while(BFS()){
sum+=dinic(s,INF);
}
printf("%I64d\n",sum);
}
return ;
}

再给出一个大牛的模板:

#include <cstdio>
#include <queue> using namespace std; typedef int LL;
const int N = ;
const int M = N << ;
const int INF = (int)1e9; struct Dinic { struct Edge {
int v;
LL cap, flow;
Edge* next, * pair; void init(int a, LL b, Edge* e1, Edge* e2) {
v = a, cap = b, flow = , next = e1, pair = e2;
}
}; Edge* head[N], * used[N];
Edge* it;
int lev[N], que[N];
Edge E[M];
int n, s, t;
LL maxFlow; void init(int n, int s, int t) {
it = E;
this->n = n;
this->s = s, this->t = t;
for (int i = ; i < n; i++)
head[i] = ;
} void add(int u, int v, LL c) {
it->init(v, c, head[u], it + );
head[u] = it++;
it->init(u, , head[v], it - );
head[v] = it++;
} bool bfs() {
for (int i = ; i < n; lev[i++] = -);
lev[s] = ;
int st = , ed = ;
que[ed++] = s;
while (st < ed) {
int u = que[st++];
for (Edge* e = head[u]; e; e = e->next) {
int v = e->v;
if (lev[v] == - && e->cap > e->flow) {
lev[v] = lev[u] + ;
que[ed++] = v;
}
}
}
return lev[t] != -;
} LL dfs(int u, LL f) {
if (u == t) return f;
for (Edge* & e = used[u]; e; e = e->next) {
int v = e->v;
if (e->cap > e->flow && lev[v] == lev[u] + ) {
LL tmp = dfs(v, min(e->cap - e->flow, f));
if (tmp > ) {
e->flow += tmp;
e->pair->flow -= tmp;
return tmp;
}
}
}
return ;
} void run() {
maxFlow = ;
while (bfs()) {
for (int i = ; i < n; i++)
used[i] = head[i];
LL f = ;
while (f) {
f = dfs(s, INF);
maxFlow += f;
}
}
} }G; int main() {
int n, m, u, v, w;
while (~scanf("%d%d", &m, &n)) {
G.init(n, , n - );
while (m--) {
scanf("%d%d%d", &u, &v, &w);
G.add(u - , v - , w);
}
G.run();
printf("%d\n", G.maxFlow);
}
return ;
}

POJ 1273 Drainage Ditches(网络流dinic算法模板)的更多相关文章

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

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

  2. poj 1273 Drainage Ditches 网络流最大流基础

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59176   Accepted: 2272 ...

  3. POJ 1273 Drainage Ditches 网络流 FF

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74480   Accepted: 2895 ...

  4. poj 1273 Drainage Ditches (网络流 最大流)

    网络流模板题. ============================================================================================ ...

  5. poj 1273 Drainage Ditches(最大流)

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

  6. POJ 1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67387   Accepted: 2603 ...

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

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

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

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

  9. 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches

    Drainage Ditches 题目抽象:给你m条边u,v,c.   n个定点,源点1,汇点n.求最大流.  最好的入门题,各种算法都可以拿来练习 (1):  一般增广路算法  ford() #in ...

随机推荐

  1. WPF动画 storyboard

    <Window x:Class="StoryBoard.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...

  2. opengl基础学习专题 (三) 多边形绘制的几种样式

    题外话 聪明人之所以不会成功,是由于他们缺乏坚韧的毅力. ——艾萨克·牛顿(1643年1月4日—1727年3月31日)英国 也许可以理解为 想更深一步的时候,坚持,努力和聪明缺一不可. 挺直腰杆在此向 ...

  3. chattr 与 lsattr 命令详解

    PS:有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,不过现在生产绝大部分跑的li ...

  4. iOS学习之UI可视化编程-StoryBoard

    一.StoryBoard与xib 对比: 相同点:都属于IB编程的方式,可以快速构建GUI. 不同点:xib侧重于单文件(单独的控制器或者视图)编辑,storyboard侧重于多页面关联.storyb ...

  5. [转]network-manager与interfaces冲突

      [转]network-manager与interfaces冲突   http://blog.sina.com.cn/s/blog_48a45b9501010681.html   网络配置的两种方式 ...

  6. Hadoop TextInputFormat源码分析

    from:http://blog.csdn.net/lzm1340458776/article/details/42707047 InputFormat主要用于描述输入数据的格式(我们只分析新API, ...

  7. Convert Sorted Array to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  8. c++ _beginthread

    c++多线程编程 #include <windows.h> #include <process.h> /* _beginthread, _endthread */ #inclu ...

  9. 了解struts2 action的一些原理

    今天在struts2中的 action方法中,打印了一下当前action,即this,还打印了一下当前action引用的service, 在页面中访问该action方法2次,发现action是不一样的 ...

  10. 关于java.lang.OutOfMemoryError: Java heap space的错误分析

    今天无意间遇到这个错误:java.lang.OutOfMemoryError: Java heap space 问题出现原因:使用a标签实现快速下载[当然已经实现了,但想了想还是要归纳解决这类问题] ...