#include<stdio.h>

#include<string.h>

#define N 300

#define inf 0x7fffffff

#include<queue>

using namespace std;

struct node {

  int u,v,w,next;

}bian[N*4];

int head[N],yong,d[N],s,t;

void addedge(int u,int v,int w) {

   bian[yong].u=u;

   bian[yong].v=v;

   bian[yong].w=w;

   bian[yong].next=head[u];

   head[u]=yong++;

}

int Min(int a,int b) {

return a>b?b:a;

}

int bfs() {

  int i,cur,v;

  queue<int>q;

  memset(d,0,sizeof(d));

  q.push(s);

  d[s]=1;

  while(!q.empty()) {

    cur=q.front();

    q.pop();

    for(i=head[cur];i!=-1;i=bian[i].next) {

        v=bian[i].v;

        if(bian[i].w&&!d[v]) {

            d[v]=d[cur]+1;

            if(v==t)

                return 1;

            q.push(v);

        }

    }

  }

  return 0;

}

int dfs(int u,int limit) {

   int cost=0,v,i,flow;

   if(u==t)

    return limit;

   for(i=head[u];i!=-1;i=bian[i].next) {

    v=bian[i].v;

    if(bian[i].w&&d[v]==d[u]+1) {

        flow=dfs(v,Min(limit-cost,bian[i].w));

        if(flow>0) {//

            bian[i].w-=flow;

            bian[i^1].w+=flow;

            cost+=flow;

            if(limit==cost)//如果已经搜到了满流就跳出

                break;

        }

        else

            d[v]=-1;//如果流量等于零,再遇到这个点就进行不下去了

    }

   }

   return cost;

}

int dinic() {

int sum=0;

while(bfs())//建层次图

    sum+=dfs(s,inf);//多次dfs搜索s-t增光路求可行流

    return sum;

}

int main(){

     int m,i,j,k,n;

     while(scanf("%d%d",&m,&n)!=EOF) {

        yong=0;

        memset(head,-1,sizeof(head));

        s=1;t=n;

        while(m--){

            scanf("%d%d%d",&i,&j,&k);

            addedge(i,j,k);//建边

            addedge(j,i,0);//建反向边

        }

        printf("%d\n",dinic());

     }

return 0;

}

poj 1273最大流dinic算法模板的更多相关文章

  1. POJ 3469.Dual Core CPU 最大流dinic算法模板

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 24830   Accepted: 10756 ...

  2. 求最大流dinic算法模板

    //最短增广路,Dinic算法 struct Edge { int from,to,cap,flow; };//弧度 void AddEdge(int from,int to,int cap) //增 ...

  3. 最大流Dinic算法模板(pascal)

    program rrr(input,output); const inf=; type pointer=^nodetype; nodetype=record t,c:longint; next,rev ...

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

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

  5. POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]

    妖怪题目,做到现在:2017/8/19 - 1:41…… 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Tim ...

  6. HDU1532最大流 Edmonds-Karp,Dinic算法 模板

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...

  7. hdu 2435 dinic算法模板+最小割性质

    #include<stdio.h> #include<queue> #include<string.h> using namespace std; #define ...

  8. 网络流之最大流Dinic算法模版

    /* 网络流之最大流Dinic算法模版 */ #include <cstring> #include <cstdio> #include <queue> using ...

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

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

随机推荐

  1. SmartDispatcher 类

    UI线程中使用 public class SmartDispatcher { public static void BeginInvoke(Action action) { if (Deploymen ...

  2. eclipse.ini启动参数配置的解析及方法

    原文地址 - http://www.uzzf.com/news/18444.html 1.先了解下JVM内存管理机制,JVM内存分为堆内存和非堆内存 2.JVM内存限制 首先JVM内存限制于实际的最大 ...

  3. PHP网站 通过js方式判断是否是手机访问,若是 跳转到手机版网址!

    <script type="text/javascript" src="http://i3.dukuai.com/ui/js/jquery-1.32pack.js& ...

  4. Coursera公开课-Machine_learing:编程作业8(2016-10-06 20:49)

    Anomaly Detection and Recommender Systems 本周编程作业分为两部分:异常检测和推荐系统. 异常检测:本质就是使用样本的到特种值的gaussian分布,来预估正确 ...

  5. RabbitMQ~一些术语和最消息的生产

    学习一种技术需要先了解它,而想要学好一种技术,需要更多的了解它的组成,原理和实现机制! RabbitMQ安装介绍 RabbitMQ是由erlang语言开发的,所以必须先有安装erlang,类似java ...

  6. 消息队列 (1) mac安装RabbitMQ

    什么是RabbitMQ? RabbitMQ是由Erlang语言编写的实现了高级消息队列协议(AMQP)的开源消息代理软件(也称为面向消息的中间件).支持WIndows.Linux.MAC OS 操作系 ...

  7. Objective-C——Runtime理解

    动态语言 OC是一门不折不扣的动态语言,所以它的很多机制都是动态运行时决定的.这点和C语言不一样,C语言是静态绑定,也就是编译后所有的一切都已经决定了.这一点和C语言的函数指针有些类似,很多时候函数指 ...

  8. [转]Wote用python语言写的imgHash.py

    #!/usr/bin/python import glob import os import sys from PIL import Image EXTS = 'jpg', 'jpeg', 'JPG' ...

  9. 00-SQLite的SQL语法

    SQLite的SQL语法 SQLite库可以解析大部分标准SQL语言.但它也省去了一些特性并且加入了一些自己的新特性.这篇文档就是试图描述那些SQLite支持/不支持的SQL语法的.查看关键字列表. ...

  10. CSS3:变换和动画

    <html> <style> .container{ -webkit-perspective: 800; -webkit-perspective-origin: 50% 40% ...