Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 13849    Accepted Submission(s): 6609

Problem Description
Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
 
Input
The first line of input contains an integer T, denoting the number of test cases.
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
 
Output
For each test cases, you should output the maximum flow from source 1 to sink N.
 
Sample Input
2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1
 
Sample Output
Case 1: 1
Case 2: 2
 
第一道网络流的题。
Edmonds-Karp算法:
从零流(所有边流量均为0)开始不断增加流量,保持每次增加流量后都满足流量限制、反对称性、流量平衡这3个条件。
把原图中每条边上的容量与流量之差(称为残余容量,简称残量)计算出,得到残量网络,残量网络中的边数可能达到原图中边数的两倍(详见小白书。残量网络是为了可以自我修正。假如当前增广路造成了堵塞时,若有反向边就可以修正错误。)
 
残量网络中任何一条从s到t的邮箱道路对应一条原图中的增广路,只要求出该道路中所有残量的最小值d,把对应的所有边上的流量增加d即可,这个过程称为增广。不难验证,如果增广前的流量满足3个条件,增广后仍然满足。显然,只要残量网络中存在增广路,流量就可以增大(如果残量网络中不存在增广路,则当前流就是最大流)——增广路定理
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 20
#define INF 100000000 int cap[N][N];
int flow[N][N];
queue<int>q;
int main()
{
int t,cnt=;
scanf("%d",&t);
while(t--)
{
while(!q.empty())
q.pop();
int n,m;
scanf("%d%d",&n,&m);
memset(cap,,sizeof(cap));
for(int i=; i<m; i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
if(cap[x][y]!=)
cap[x][y]+=c;
else
cap[x][y]=c;
}
int totf=,a[N],p[N];
memset(flow,,sizeof(flow));
for(;;)
{
memset(a,,sizeof(a));
a[]=INF;
q.push();
while(!q.empty())
{
int u=q.front();
q.pop();
for(int v=; v<=n; v++)
if(!a[v]&&cap[u][v]>flow[u][v])
{
p[v]=u;
q.push(v);
if(cap[u][v]-flow[u][v]>a[u])
a[v]=a[u];
else
a[v]=cap[u][v]-flow[u][v];
}
}
if(a[n]==)
break;
for(int u=n; u!=; u=p[u])
{
flow[p[u]][u]+=a[n];
flow[u][p[u]]-=a[n];
}
totf+=a[n];
}
printf("Case %d: %d\n",++cnt,totf);
}
return ;
}

HDU_3549_网络流(最大流)的更多相关文章

  1. POJ 1459-Power Network(网络流-最大流-ISAP)C++

    Power Network 时间限制: 1 Sec  内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...

  2. [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)

    题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...

  3. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  4. HDU1532 网络流最大流【EK算法】(模板题)

    <题目链接> 题目大意: 一个农夫他家的农田每次下雨都会被淹,所以这个农夫就修建了排水系统,还聪明的给每个排水管道设置了最大流量:首先输入两个数n,m ;n为排水管道的数量,m为节点的数量 ...

  5. Redraw Beautiful Drawings(hdu4888)网络流+最大流

    Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...

  6. A simple Gaussian elimination problem.(hdu4975)网络流+最大流

    A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...

  7. 【bzoj3130】[Sdoi2013]费用流 二分+网络流最大流

    题目描述 Alice和Bob做游戏,给出一张有向图表示运输网络,Alice先给Bob一种最大流方案,然后Bob在所有边上分配总和等于P的非负费用.Alice希望总费用尽量小,而Bob希望总费用尽量大. ...

  8. 【bzoj1822】[JSOI2010]Frozen Nova 冷冻波 计算几何+二分+网络流最大流

    题目描述 WJJ喜欢“魔兽争霸”这个游戏.在游戏中,巫妖是一种强大的英雄,它的技能Frozen Nova每次可以杀死一个小精灵.我们认为,巫妖和小精灵都可以看成是平面上的点. 当巫妖和小精灵之间的直线 ...

  9. 【bzoj1733】[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+网络流最大流

    题目描述 Farmer John is constructing a new milking machine and wishes to keep it secret as long as possi ...

  10. 【bzoj1532】[POI2005]Kos-Dicing 二分+网络流最大流

    题目描述 Dicing 是一个两人玩的游戏,这个游戏在Byteotia非常流行. 甚至人们专门成立了这个游戏的一个俱乐部. 俱乐部的人时常在一起玩这个游戏然后评选出玩得最好的人.现在有一个非常不走运的 ...

随机推荐

  1. BZOJ(8) 1053: [HAOI2007]反素数ant

    1053: [HAOI2007]反素数ant Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4118  Solved: 2453[Submit][St ...

  2. 为部门整理的mysql_db使用军规

    mysql_db使用军规: 1.禁止开发測试人员在IDC环境手工删除和改动数据 2.全部需求通过DB工具系统提交 3.禁止在IDC环境DB进行測试 4.IDC环境提交的sql语句一定要经过非正式环境验 ...

  3. TomCatserver的安装,环境的配置,服务的启动以及验证---ShinePans

    首先下载 TomCat 6: http://yunpan.cn/cg5icf3dha4k3  提取码 34c5 然后配置环境变量: 电脑>>>属性>>>高级系统设置 ...

  4. Ubuntu 13.10 安装 TeX Live 2013

    注:笔者也是刚刚接触TeX系统,水平有限,若有疏漏之处还望指正. 中文解决方案 对于LaTeX中文排版,比较方便有这样的几种解决方案:LaTeX+CJK / LaTeX+XeTeX / CTeX.其中 ...

  5. npm/bower/brew

    npm: npm(node package manager)node包管理器,用来下载发布第三方工具包,例如:代码的压缩.合并.编译的插件包.主要功能:安装.卸载.更新.查看.搜索.发布等 npm的具 ...

  6. 加logo

    import cv2 import numpy as np # f0,f1='feiyong.jpg','g3logo.jpg' # # #f1,f0='feiyong.jpg','g3logo.jp ...

  7. Linux音频驱动-ALSA概述

    概述 ALSA(Advanced Linux Sound Architecture)是linux上主流的音频结构,在没有出现ALSA架构之前,一直使用的是OSS(Open Sound System)音 ...

  8. luogu 1939 【模板】矩阵加速(数列)

    题目大意: a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 求a数列的第n项%1000000007 思路: 使用矩阵快速幂进行加速 在草稿纸上填了填数 然后就 ...

  9. Nginx的alias与root的用法区别和location匹配规则

    1.alias与root的用法区别 最基本的区别:alias指定的目录是准确的,root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录. location /abc/ { ...

  10. Sqlserver日期操作

    Sqlserver日期操作 select GETDATE() as '当前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) ...