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. jenkins权限问题

    今天用jenkins的时候,构建失败,看了下控制台输出,提示是缺少权限,以前也遇到过这个问题,当时是通过把相关文件夹权限设置为777解决的,这种办法有两个不好的地方,一是这样一来任何用户都能操作这个文 ...

  2. vue devtools无法使用

    vue devtools无法使用 一.总结 一句话总结: 没显示vue devtools调试工具的原因是用了生产环境的版本或是压缩的vue版本,或是没有勾选:允许访问文件网址 二.vue调试工具Dev ...

  3. 在testrpc以太坊测试环境部署智能合约

    2018年03月13日 09:20:54 思无邪-machengyu 阅读数 2683   版权声明:本文为博主原创文章,转载请务必注明出处,否则追究法律责任 https://blog.csdn.ne ...

  4. ubuntu 上开发.netcore

    ubuntu需要安装的软件: 1.sudo apt-get install openssh-server openssh-client 2.sudo apt-get git 3.安装vscode 4. ...

  5. kotlin泛型中星号投射

    如果一个泛型类型中存在多个类型的参数,那么每个类型的参数都可以单独投射,例如:如果类型定义为:"interface Function<in T,out>",那么可以出现 ...

  6. python-30个骚操作

    1.冒泡排序     2.计算x的n次方的方法     3.计算a*a + b*b + c*c + ……     4.计算阶乘 n!             5.列出当前目录下的所有文件和目录名   ...

  7. Java 正则判断一个字符串中是否包含中文

    使用正则判断一个字符串中是否包含中文或者中文字符 代码实现如下: import java.util.regex.Matcher; import java.util.regex.Pattern; /** ...

  8. C-Lodop提示Access violation at address ...in module 'CLodopPrint32.exe' write of address

    C-Lodop提示 Access violation at address ……in module 'CLodopPrint32.exe' write of address ……最近遇到了个问题,上午 ...

  9. 获取Xshell Xftp等官网下载地址

    1. 首先还是得填写邮箱获取试用链接地址,例如我这次获取的是: https://cdn.netsarang.net/c5711331/Xshell-6.0.0175.exe 关键需要记下 c57113 ...

  10. 分布式消息通信之RabbitMQ_01

    目录 官网 1. RabbitMQ安装 1.1 Window版安装 1.2 Linux版安装 2. 典型应用场景 3. 基本介绍 3.1 AMQP协议 3.2 RabbitMQ的特性 3.3 工作模型 ...