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. Moses 里的参数(未完成)

    老师要求看看Moses里都有什么参数,调整了参数又会对翻译结果有什么影响,先将找到的参数列出来 首先是权重: [weight] WordPenalty0= LM= Distortion0= Phras ...

  2. 算法:C++排列组合

    题目:给定1-n数字,排列组合. 解法:递归.第一个数字有n种选择,第二个数字有n-1种选择,依次递归排列输出.用数组表示n个数字,用过的数字置0. 实现语言:C++ #include <ios ...

  3. SQL注入式攻击

    百度百科:http://baike.baidu.com/link?url=GQbJ2amTzTahZA7XJSBDLYYkN3waQ9JCoJ0l--tCWlvKQibe0YaH4hpmgEnLyn0 ...

  4. Bing Speech Recognition 标记

    Bing Speech Services Bing   Bing Speech Services provide speech capabilities for Windows and Windows ...

  5. centos php-fpm nginx配置

    移除旧的软件包:yum remove httpd* php* 安装:yum install php php-fpm yum install php-gd php-mysql php-mbstring ...

  6. 兼容sdk7&iOS7的issue解决小片段总结

    ios7新增加的icon尺寸: 76 x 76:Size for iPad 2 and iPad mini (standard resolution) 120 x 120 :Size for iPho ...

  7. SQL Server数据库学习笔记-E-R模型

    实体(Entities)联系(Relationships)模型简称E-R模型也称E-R方法,是由P.P.Chen于1976年首先提出的.还有一个关键元素Attributes-属性,它提供不受任何数据库 ...

  8. (转)android Fragments详解三:实现Fragment的界面

    为fragment添加用户界面 fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中.    一个 要为fragment提供layo ...

  9. EntityFramwork(1) 源地址https://msdn.microsoft.com/zh-cn/data/jj193542

    1.创建应用程序 简单起见,我们将构建一个使用 Code First 执行数据访问的基本控制台应用程序. 打开 Visual Studio "文件"->"新建&qu ...

  10. 2.Modelsim打开时出现的Error

    Modelsim之error “unable to check out a viewer license necessary for use of the modelsim graph.Vsim is ...