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. 网络共享服务—SAMBA服务

    SAMBA服务简介 SMB:Server Message Block服务器消息块,IBM发布,最早是DOS网络文件共享协议 Cifs:common internet file system,微软基于S ...

  2. NodeJS的exports、module.exports与ES6的export、export default深入详解

    前言 决定开始重新规范的学习一下node编程.但是引入模块我看到用 require的方式,再联想到咱们的ES6各种export .export default. 阿西吧,头都大了.... 头大完了,那 ...

  3. 使用hibernate利用实体类生成表和利用表生成实体类

    1,配置数据库,这里以oracle数据库为例.点击右侧Database图标:

  4. Flutter移动电商实战 --(25)列表页_使用Provide控制子类-1

    主要是二级分类的UI布局 生成我们的右侧动态类 定义list变量 开始写里面的子项,把每一个小的写了 再拼成一个大的 这样我们的小类就写完了 开始写我的大类别:是一个横向的ListView.写横向的L ...

  5. Hide()方法不生效

    有时候Jquery中的.hide()不起作用,有时是因为在函数中包括着,   $(".select-dropdown").hide(); 在hide中加一个1就行了   文章来源: ...

  6. 【SpringBoot】转载 springboot使用thymeleaf完成数据的页面展示

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/weixin_36380516/artic ...

  7. 亿美软通 短信api

    亿美软通 短信api   国际短信http标准协议文档和demo的地址: http://www.b2m.cn/static/doc/ims/ims_sendSingle.html   import d ...

  8. CPU分几核几核的是什么意思?

    CPU的几核分类其实是按照CPU的地址总线数量来分类的 单核心的地址总线数量是8条 双核心就是16条 三核心就是24 依次类推,核心的多少,只需要知道地址总线的多少,然后除以8就得出来了 地址总线:地 ...

  9. js模块化编程之彻底弄懂CommonJS和AMD/CMD

    转自 http://www.cnblogs.com/chenguangliang/p/5856701.html

  10. Spring Aop(四)——基于Aspectj注解的Advice介绍

    转发地址:https://www.iteye.com/blog/elim-2395315 4 基于Aspectj注解的Advice介绍 之前介绍过,Advice一共有五种类型,分别是before.af ...