Flow Problem

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

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
 

题意:有t个测试样例,n个点,m条边组成的一个网络图,问从起点1到终点n的最大流量是多少

最大流模板题,EK算法

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#define ll long long
#define mx 0x3f3f3f3f
using namespace std;
int flow[][],cap[][];//flow当前流量,cap总容量
int f[],vis[];//f[i]最小残量=cap-flow,vis[i]标记i节点的上一个最小残量所在的位置
int mx_flow;//最大流量,所有增广路最小残量之和
void bfs(int n)
{
queue<int>p;
mx_flow=;
int flag=;
memset(flow,,sizeof(flow));
while(flag==)
{
memset(f,,sizeof(f));
memset(vis,,sizeof(vis));
f[]=mx,vis[]=-;//初始化源点
p.push();
while(!p.empty())//bfs找增广路
{
int now=p.front();
p.pop();
for(int i=;i<=n;i++)
{
if(!f[i]&&flow[now][i]<cap[now][i])
{
f[i]=min(f[now],cap[now][i]-flow[now][i]);//取最小残量
vis[i]=now;
p.push(i);
}
}
}
if(f[n]==)//容量-流量==0,一条增广路寻找结束
flag=;
mx_flow+=f[n];
int pos=n;//从汇点开始更新流量
while(!flag&&pos!=)
{
flow[vis[pos]][pos]+=f[n];//正向更新流量
flow[pos][vis[pos]]-=f[n];//反向更新流量
pos=vis[pos];
}
}
} int main()
{
int t,n,m,cnt=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);//n是顶点数,m是边数
memset(cap,,sizeof(cap));
for(int i=;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
cap[x][y]+=z;
}
bfs(n);
printf("Case %d: %d\n",++cnt,mx_flow);
}
}

hdu 3549 Flow Problem 最大流问题 (模板题)的更多相关文章

  1. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  2. 网络流 HDU 3549 Flow Problem

    网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...

  3. hdu 3549 Flow Problem【最大流增广路入门模板题】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...

  4. hdu 3549 Flow Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...

  5. HDU 3549 Flow Problem(最大流模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 刚接触网络流,感觉有点难啊,只好先拿几道基础的模板题来练练手. 最大流的模板题. #include< ...

  6. hdu 3549 Flow Problem 网络流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Network flow is a well-known difficult problem f ...

  7. hdu 3549 Flow Problem(增广路算法)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 模板题,白书上的代码... #include <iostream> #include & ...

  8. hdu 3549 Flow Problem (网络最大流)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  9. 网络流--最大流--HDU 3549 Flow Problem

    题目链接 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, y ...

随机推荐

  1. cnblog 开通啦!

    喜大普奔! 终于开通cnblog了!以后有blog都会放这里哦 > o < 希望大家可以关注窝哦.

  2. vue注册全局过滤器

    1.src目录下创建filter文件 /** * 男女 * @param val * @returns {string} */ const status = val => { let name ...

  3. dmidecode查看硬件信息

    //check memory,cpu,system,biosroot@dellemc-diag-os:/home# dmidecode -t Memoryroot@dellemc-diag-os:/h ...

  4. nginx 性能优化的概述及在CPU资源方面的处理

    nginx的性能优化的概述 软件层面的提升硬件的使用率 增大CPU的利用率 增大内存的利用率 增大磁盘IO利用率 增大网络带宽利用率 提升硬件规格 网卡:万兆网卡.例如10G.25G.40G等 磁盘: ...

  5. 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式

    Spring的属性注入: 构造方法的属性注入 set方法的属性注入

  6. JS经典理解例子

    1. var name = 'the window'; var obj = { name:"my obj", getNameFunc:function(){ return func ...

  7. Python学习第十四课——面向对象基本思想part1

    面向对象的基本思想 # 写法1 person1 = { 'name': 'hanhan', ', 'sex': '男' } def xue_xi(person): print('%s在学习' % pe ...

  8. 【PAT甲级】1034 Head of a Gang (30 分)

    题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...

  9. 如何启动mac版docker自带的k8s

    最近准备好好学习下k8s,为了图方便,直接使用docker集成的k8s,但是网上找了一些教程但都没能一次性成功,只好自己从头跑一遍,顺手写个教程可以方便有类似需求的同学参考. 话不多说,直接上步骤. ...

  10. Android 学习笔记四:创建工具栏按钮

    原文:http://blog.csdn.net/lihongxun945/article/details/48951199 前面我们已经可以在一个Activity中添加一些按钮之类的组件.由于手机的屏 ...