hdu 3549 Flow Problem 最大流问题 (模板题)
模板 最大 HDU Problem Flow 问题
2023-09-14 08:56:54 时间
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 28193 Accepted Submission(s): 12476
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)
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
Sample Output
Case 1: 1
Case 2: 2
题意:有t个测试样例,n个点,m条边组成的一个网络图,问从起点1到终点n的最大流量是多少
最大流模板题,EK算法
#include<iostream> #include<string.h> #include<string> #include<algorithm> #include<queue> #define ll long long #define mx 0x3f3f3f3f using namespace std; int flow[205][205],cap[205][205];//flow当前流量,cap总容量 int f[205],vis[205];//f[i]最小残量=cap-flow,vis[i]标记i节点的上一个最小残量所在的位置 int mx_flow;//最大流量,所有增广路最小残量之和 void bfs(int n) { queue<int>p; mx_flow=0; int flag=0; memset(flow,0,sizeof(flow)); while(flag==0) { memset(f,0,sizeof(f)); memset(vis,0,sizeof(vis)); f[1]=mx,vis[1]=-1;//初始化源点 p.push(1); while(!p.empty())//bfs找增广路 { int now=p.front(); p.pop(); for(int i=1;i<=n;i++) { if(!f[i]&&flow[now][i]<cap[now][i]) { f[i]=min(f[now],cap[now][i]-flow[now][i]);//取最小残量 vis[i]=now; p.push(i); } } } if(f[n]==0)//容量-流量==0,一条增广路寻找结束 flag=1; mx_flow+=f[n]; int pos=n;//从汇点开始更新流量 while(!flag&&pos!=1) { flow[vis[pos]][pos]+=f[n];//正向更新流量 flow[pos][vis[pos]]-=f[n];//反向更新流量 pos=vis[pos]; } } } int main() { int t,n,m,cnt=0; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m);//n是顶点数,m是边数 memset(cap,0,sizeof(cap)); for(int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); cap[x][y]+=z; } bfs(n); printf("Case %d: %d\n",++cnt,mx_flow); } }
相关文章
- 阿里云的邮件推送功能那个模板审核时间怎么那么长啊
- Django模板系统
- java实现洛谷P3376【模板】网络最大流
- Atlassian JIRA服务器模板注入漏洞复现(CVE-2019-11581)
- 【RTOS】基于V7开发板的RTX5和FreeRTOS带CMSIS-RTOS V2封装层的模板例程下载,AC6和AC5两个版本
- Atitit 泛型的知识点 目录 1. 为什么需要泛型 why2 2. 定义分类 what2 2.1. 编辑类型参数2 2.2. 模板2 2.3. 简单理解 占位符 代替object、3
- 前端MVC Vue2学习总结(一)——MVC与vue2概要、模板、数据绑定与综合示例
- Django 模板系统(template)
- C++函数模板(一)
- 最大流量dinci模板
- 785. 快速排序(模板)
- Zabbix的模板管理与配置