技术经验分享:hdu3549初试最大流问题

简介: 技术经验分享:hdu3549初试最大流问题

"

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 //代码效果参考:https://v.youku.com/v_show/id_XNjQwNjg1MjE3Ng==.html

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

#include

#include

#include

#define maxn 16

#define inf 1[29

using namespace std;

struct edge

{

int to,cap,rev;

};

int n,m,vis【maxn】;

vector 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;i0)

{

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 //代码效果参考:https://v.youku.com/v_show/id_XNjQwMDM5NzIyOA==.html

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;

}


"
image.png
相关文章
|
12月前
ACM刷题之路(二十)线筛素数+找规律f(n) 2019暑期集训 HDU 2585
ACM刷题之路(二十)线筛素数+找规律f(n) 2019暑期集训 HDU 2585
|
8月前
|
算法 测试技术 容器
代码随想录算法训练营第四十二天 | LeetCode 1049. 最后一块石头的重量 II、494. 目标和、474. 一和零
代码随想录算法训练营第四十二天 | LeetCode 1049. 最后一块石头的重量 II、494. 目标和、474. 一和零
33 1
【动态规划上分复盘】这是你熟悉的地下城游戏吗?
【动态规划上分复盘】这是你熟悉的地下城游戏吗?
|
11月前
|
算法 Android开发 Kotlin
LeetCode 周赛上分之旅 #42 当 LeetCode 考树上倍增,出题的趋势在变化吗
学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场 LeetCode 周赛的解题报告,一起体会上分之旅。
99 0
LeetCode 周赛上分之旅 #42 当 LeetCode 考树上倍增,出题的趋势在变化吗
|
算法 Java
洛谷新手村算法题分析
本文汇总洛谷新手村算法题的分析解答,题目代码基本由Java实现。
91 0
洛谷新手村算法题分析
|
机器学习/深度学习 人工智能 BI
蓝桥杯真题31日冲刺国一 | 每日题解报告 第二十七天
大家好 我是泡泡 倒数六天 蓝桥开赛!记得打印准考证!
101 0
蓝桥杯真题31日冲刺国一 | 每日题解报告 第二十七天
|
机器学习/深度学习 定位技术
蓝桥杯真题31日冲刺国一 | 每日题解报告 第十六天
大家好,我是泡泡,今天的题目很合理,很多模板,大家多多掌握,学习一下用各种思路解题,灵活多变!
219 0
蓝桥杯真题31日冲刺国一 | 每日题解报告 第十六天
|
Java
蓝桥杯真题31日冲刺国一 | 每日题解报告 第二十二天
大家好,我是泡泡,今天给大家带来五到2020年填空真题和两个打卡题
100 0
蓝桥杯真题31日冲刺国一 | 每日题解报告 第二十二天
|
Java
蓝桥杯真题31日冲刺国一 | 每日题解报告 第十九天
大噶好,我系泡泡,今天的题难度很高(我是fw) 有能力的自己搞一下,省赛的同学今天就当放松一下
152 0
蓝桥杯真题31日冲刺国一 | 每日题解报告 第十九天
|
算法
每日一题冲刺大厂第十四天 NOIP普及组 三连击
大家好,我是泡泡,给大家带来每日一题的目的是为了更好的练习算法,我们的每日一题为了让大家练到各种各样的题目,熟悉各种题型,一年以后,蜕变成为一个不一样的自己!
85 0

热门文章

最新文章