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. iOS 根据域名查询 IP 地址

    在 iOS 开发中,如果需要知道网站的 IP 地址: #include <netdb.h> #include <arpa/inet.h> NSString *webSiteSt ...

  2. stm32 串口发送字符串丢失第一个字节

    使用stm32f103调试串口通讯时,上电后发送的字符串的第一个字节丢失. 发送数据GpuSend("SPG(2);"); 接收端收到的数据为:PG(2);,第一个字符丢失. 出现 ...

  3. 子元素使用position:fixed,导致他的宽度不能和父元素保持一致的解决方案

    最近在编码过程中,遇到过这样一个问题,代码如下,我们有一个父级,他有一定的宽度,在他的里面有两个子级,其中一个是绝对定位的,且要求他们的宽度都和父级保持一致,然后问题就出现了,我们会发现,有了定位的s ...

  4. linux磁盘空间挂载

    (1)查看磁盘空间 df -hl (3)查看硬盘及分区信息 fdisk -l (4)格式化新分区 mkfs.ext3 /dev/xvdb (5)将磁盘挂载在/www/wwwroot/default目录 ...

  5. 最全Python学习路线图【2020最新版】

    2020年最新的python学习大纲,专为python高薪打造另外很多人在学习Python的过程中,往往因为没有好的教程或者没人指导从而导致自己容易放弃,为此我建了个Python交流.裙 :一久武其而 ...

  6. AT指令之 TCP/IP 命令

    BC26 还有一组专用于 TCP 通信的 AT 指令:<BC26_TCP/IP_AT_Commands_Manual_V1.1>,之前已经有了 Socket 可以进行 TCP 通信,现在又 ...

  7. pymongo 用户密码连接

    # db mongodbdb_mongo_attr = { 'host': '*', 'port': 27, 'database':'tease', 'username':'*v', 'passwor ...

  8. Django:cookie和session相关问题

    http://www.cnblogs.com/fnng/p/3750596.html http://www.cnblogs.com/chenchao1990/p/5283725.html

  9. 调用天气预报webservice接口

    1.将 服务端的wsdl文档保存至 本地 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL 2.将里面的 <s:element re ...

  10. eclipse问题集合

    [eclipse更换jdk版本]