POJ 1273 Drainage Ditches(网络流dinic算法模板)
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算法模板)的更多相关文章
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- poj 1273 Drainage Ditches 网络流最大流基础
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59176 Accepted: 2272 ...
- POJ 1273 Drainage Ditches 网络流 FF
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 74480 Accepted: 2895 ...
- poj 1273 Drainage Ditches (网络流 最大流)
网络流模板题. ============================================================================================ ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67387 Accepted: 2603 ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches
Drainage Ditches 题目抽象:给你m条边u,v,c. n个定点,源点1,汇点n.求最大流. 最好的入门题,各种算法都可以拿来练习 (1): 一般增广路算法 ford() #in ...
随机推荐
- C语言 将产生的随机数存入数组,数据不能相同
1.定义一个一维数,数组大小为24. 2.产生0~23的随机数. 3.将产生的随机数存入i数组,要求数组中的每个数据不能相同. 4.补充说明,这个子程序要求每次调用后,这个数组里面就 存放了0~23这 ...
- DB2物化表
DB2物化查询表(MQT)是DB2数据库中一类特殊的表 物化表和视图的区别 物化表是一个查询结果集,视图是一个SQL语句. 以下是一个简单例子(说明物化表) 1.创建表,插入测试数据 ----创建表 ...
- java reflect 初始学习 动态加载类
首先要理解Class类: 在java 的反射中,Class.forName("com.lilin.Office") 使用类的全名,这样获取,不仅仅表示了类的类类型,同时还代表着类的 ...
- Cygwin ssh服务配置 (SecureCRT连接Cygwin配置)
1.运行ssh-host-config 这里需要注意的是标红部分,输入的用户名或密码要符合计算机的用户名或密码策略(尤其是公司有权限限制的电脑). $ ssh-host-config *** Quer ...
- Python常用内建模块
Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...
- android开发系列之socket编程
上周在项目遇到一个接口需求就是通讯系列必须是socket,所以在这篇博客里面我想谈谈自己在socket编程的时候遇到的一些问题. 其实在android里面实现一个socket通讯是非常简单的,我们只需 ...
- 历时八年,HTML5 标准终于完工了
万维网联盟(W3C)2014年10月29日泪流满面地宣布,经过几乎8年的艰辛努力,HTML5标准规范终于最终制定完成了,并已公开发布. 在此之前的几年时间里,已经有很多开发者陆续使用了HTML5的部分 ...
- iOS-添加测试设备Identifier
第一步:确认你的设备已经连接 第二步:点击xcode上"Windows"标签,选择"Devices" 第三步:在弹出的左框选择你要添加的设备.在右边框里可以找到 ...
- 使用JSON的数据格式
在说JSON之前,我们先来看一下在javascript中创建对象的方式,也就是创建对象的字面量表示法.我们知道js中有五种基本的数据类型,分别是: Undefined(变量可能没有声明或者赋值) ...
- Ant学习---第二节:Ant添加文件夹和文件夹集的使用
一.创建 java 项目(Eclipse 中),结构图如下: 1.创建 .java 文件,代码如下: package com.learn.ant; public class HelloWorld { ...