Flow Problem

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

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
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#define maxn 16
#define inf 1<<29
using namespace std;
struct edge
{
int to,cap,rev;
};
int n,m,vis[maxn];
vector <edge> fuck[maxn];
void add_edge(int from,int to,int cap)//构图过程
{
fuck[from].push_back((edge){to,cap,fuck[to].size()});//fuck[to].size() 表示反边在反向邻接表的位置--即fuck[to]中的位置
fuck[to].push_back((edge){from,0,fuck[from].size()-1});//同上
}
void init()
{
for(int i=1;i<=n;i++) fuck[maxn].clear();
}
int dfs(int s,int e,int f)
{
if(s==e) return f;
vis[s]=1;
for(int i=0;i<fuck[s].size();i++)//每次的搜索 依次遍历s链接的点 更新cap 以及最大流量
{
edge &temp=fuck[s][i];//注意&这个东西 如果不用这个封装的话 改变的内容就只是形参的内容
if(!vis[temp.to]&&temp.cap>0)
{
int d=dfs(temp.to,e,min(f,temp.cap));
if(d>0)
{
temp.cap-=d;
fuck[temp.to][temp.rev].cap+=d;// 回溯处理容量问题 以及对反向边的处理
//一直回溯到流量最小的那个点上去 然后去走其他路
return d;//回溯过程肯定是要更新的
}
}
}
return 0;
}
int max_flow(int s,int e)
{
int flow=0;
while(1)//不断的从s走到e这个点 当所有可能的通道cap为0的时候 贪心完成(因为每次过程都会cap进行一次更新)
{
memset(vis,0,sizeof(vis));
int temp=dfs(s,e,inf);
if(temp==0) return flow;
else flow+=temp;
}
}
int main()
{
cin.sync_with_stdio(false);
int t,Case=0;
cin>>t;
while(t--)
{
cin>>n>>m;
init();
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add_edge(a,b,c);//构图
}
cout<<"Case "<<++Case<<": "<<max_flow(1,n)<<endl; }
return 0;
}
 
 
 
 

hdu 3549 初试最大流问题的更多相关文章

  1. HDU 3549 Flow Problem(最大流)

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

  2. Flow Problem HDU - 3549

    Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...

  3. 网络流 HDU 3549 Flow Problem

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

  4. hdu 3549 Flow Problem 最大流问题 (模板题)

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

  5. HDU 3549 网络最大流再试

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 同样的网络最大流 T了好几次原因是用了cout,改成printf就A了 还有HDU oj的编译器也不支持以 ...

  6. hdu 3549 Flow Problem

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

  7. hdu 3549 Flow Problem 网络流

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

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

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

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

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

随机推荐

  1. maven的pom报错web.xml is missing and <failOnMissingWebXml> is set to true

    错误信息:web.xml is missing and <failOnMissingWebXml> is set to true 解决办法:https://blog.csdn.net/si ...

  2. Centos 6 can't found command subscription-manager

    [root@localhost ~]# subscription-manager: command not found-bash: -bash:: command not found resoluti ...

  3. linux里面源码安装imagemagick库

    在搞树莓派的时候想搞一下树莓派中摄像头获取图像之后传给安卓,安卓进行展示. 恰好用到了imagemagick这个库,我就像正常一样进行安装,sudo apt-get install Imagick 但 ...

  4. produceTestDate

    set serveroutput on --使用基本变量类型 declare --定义基本变量:类型 --基本数据类型 pnumber , ); pname ); pdate date; begin ...

  5. Ubuntu 18.04设置1920*1080

    Ubuntu升级后,发现分辨率没有1920*1080,在网上寻找了一个文章解决办法如下. 方案一(临时性,重启会失效): 1.打开终端.输入:cvt 1920 1080 出现有modeline 的提示 ...

  6. js 高级程序设计 第三章学习笔记——Number数据类型需要注意的事项

    1.浮点数值 虽然小数点前面可以没有整数,但是并不推荐这种写法. 由于保存浮点数值需要的内存空间是保存整数值的两倍,因此ECMAScript会不失时机地将浮点数值转化为整数数值.显然,如果小数点后面没 ...

  7. Instant Messaging for Business: Your 10 Best Options

    Instant Messaging for Business: Your 10 Best Options By Iaroslav Kudritskiy It's probably not a surp ...

  8. 《精通并发与Netty》学习笔记(11 - 详解NIO (二) 分散/聚集 Scatter/Gather、Selector)

    一.分散/聚集 Scatter/Gather scatter/gather指的在多个缓冲区上实现一个简单的I/O操作,比如从通道中读取数据到多个缓冲区,或从多个缓冲区中写入数据到通道:scatter( ...

  9. 【转载】DOS系统的安装

    <电脑爱好者>报转载第一辑第一篇之DOS系统的安装 DOS系统的安装 一.DOS的历史 DOS是Diskette Operating System的缩写,意思是磁盘操作系统,主要有MS-D ...

  10. 在VMware上安装centos

    Windows,VMware和Centos三者的关系 VMware安装.centos安装 在 VMware 上安装 CentOS 第 1 步:打开 VMware,点击创建新的虚拟机 第 2 步:选择典 ...