技术经验分享: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
相关文章
|
9月前
|
存储
技术经验分享:hdoj1002解题报告(大数入门)
技术经验分享:hdoj1002解题报告(大数入门)
29 0
ACM刷题之路(十八)二分 2019暑期集训 POJ 3579 Median
ACM刷题之路(十八)二分 2019暑期集训 POJ 3579 Median
ACM刷题之路(二十)线筛素数+找规律f(n) 2019暑期集训 HDU 2585
ACM刷题之路(二十)线筛素数+找规律f(n) 2019暑期集训 HDU 2585
|
算法 Android开发 容器
LeetCode 周赛上分之旅 #35 两题坐牢,菜鸡现出原形
学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场 LeetCode 周赛的解题报告,一起体会上分之旅。
120 0
ACM刷题之路(二十二)多重背包转01背包 HDU 1171
ACM刷题之路(二十二)多重背包转01背包 HDU 1171
|
机器学习/深度学习 C++
ACM刷题之路(十七)二分 2019暑期集训 POJ2785
ACM刷题之路(十七)二分 2019暑期集训 POJ2785
101 0
ACM刷题之路(十九)二分+尺取 2019暑期集训 HDU6231 K-th Number
ACM刷题之路(十九)二分+尺取 2019暑期集训 HDU6231 K-th Number
三道好题分享
上课睡觉 - AcWing题库
99 0
|
人工智能 算法 测试技术
LeetCode 双周赛 103(2023/04/29)区间求和的树状数组经典应用
这场周赛是 LeetCode 双周赛第 103 场,难得在五一假期第一天打周赛的人数也没有少太多。这场比赛前 3 题比较简单,我们把篇幅留给最后一题。
100 0
|
算法 程序员 C++
【算法集训 | 暑期刷题营】7.22日题---二分
【算法集训 | 暑期刷题营】7.22日题---二分
【算法集训 | 暑期刷题营】7.22日题---二分