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. vue.js ③

    1.组件使用的细节点 H5编码中的规范是tr必须在tbody里所以不能直接套用<row></row>的写法,<ul>标签下支持<li>,select标签 ...

  2. Nginx企业级优化!(重点)

    隐藏Nginx版本号!(重点) 在生产环境中,需要隐藏 Nginx 的版本号,以避免安全漏洞的泄漏 一旦有黑客知道Nginx版本号便可以利用Nginx漏洞进行攻击,严重影响到了公司的安全 查看隐藏版本 ...

  3. how to analyze jmeter results

    https://octoperf.com/blog/2017/10/19/how-to-analyze-jmeter-results/

  4. 操作系统OS - 同步和异步,阻塞和非阻塞

    同步和异步关注的是消息通信机制,阻塞/非阻塞是程序在等待调用结果(消息,返回值)时的状态

  5. IDEA 查看某个class的maven引用依赖&如何展示Diagram Elements

    1.打开对应的class,如下图所示,至于具体快捷键就不说了,我是设置的eclipse的快捷键: 2.定位到对应jar,记下jar名称及版本: 3.在右侧栏点击maven,再在展出的视图中找到对应的m ...

  6. 陶陶摘苹果(0)<P2005_1>

    陶陶摘苹果 (apple.pas/c/cpp) [问题描述]  陶陶家的院子里有一棵苹果树,每到秋天树上就会结出10个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶有个30厘米高的板凳,当她不能直接用 ...

  7. c# 分页 PaginatedList<TResult>

    using System; using System.Collections.Generic; using System.Linq; namespace Microestc.PaginatedList ...

  8. PTA的Python练习题(十)

    从 第3章-22 输出大写英文字母 开始 1. a=str(input()) b='' for i in a: if 'A'<=i<='Z' and i not in b: b=b+i i ...

  9. PTA的Python练习题(一)

    最近宅家里没事干,顺便把python给学了.教程和书看了一段时间,但是缺少练习的平台. 想起大一时候练习C语言的PTA平台,就拿来练手了. (因为没有验证码无法提交题目,所以自己用pycharm来做题 ...

  10. 解决Hibernate配置文件不在SRC文件夹下获取Session方法