博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3549 Flow Problem
阅读量:4960 次
发布时间:2019-06-12

本文共 3106 字,大约阅读时间需要 10 分钟。

Flow Problem

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 19255    Accepted Submission(s): 9046

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
Sample Output
Case 1: 1
Case 2: 2
 
 题解:裸地网络流求1到n
#include 
using namespace std;#define INF 100000008const int maxn = 20,maxm = 4005;struct Netflow{ int S,T,tot,head[maxn],dis[maxn],hh[maxn]; bool vis[maxn]; struct edge{ int to,f,nxt; }G[maxm]; void init(int s, int t){ tot = 1; memset(head,0,sizeof(head)); memset(head,0,sizeof(head)); S = s, T = t; } void add(int u,int v,int w){ G[++tot].nxt = head[u]; G[tot].to = v; G[tot].f = w; head[u] = tot; G[++tot].nxt = head[v]; G[tot].to = u; G[tot].f = 0; head[v] = tot; } bool bfs(){ memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); vis[S] = 1; queue
q; q.push(S); while(!q.empty()){ int u = q.front(); q.pop(); for(int i = head[u]; i; i = G[i].nxt){ int v = G[i].to; if(!vis[v] && G[i].f){ vis[v] = 1; dis[v] = dis[u] + 1; q.push(v); } } } return vis[T]; } int dfs(int u,int v){ if(u == T || !v )return v; int ret = 0; for(int i = hh[u]; i; i = G[i].nxt){ int p = G[i].to; if(G[i].f && dis[p] == dis[u] + 1){ int dd = dfs(p, min(v, G[i].f)); G[i].f -= dd; G[i^1].f += dd; v-=dd; ret += dd; hh[u] = i; } } if(!ret)dis[u]=-1; return ret; } int dinic(){ int ans = 0; while(bfs()){ for(int i = S; i <= T; i++)hh[i] = head[i]; ans += dfs(S,INF); } return ans; } };Netflow Tr;int main(){ int T; scanf("%d",&T); for(int k = 1; k <= T; k++){ int n,m; scanf("%d%d",&n,&m); Tr.init(1,n); for(int i = 1; i <= m; i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); Tr.add(u,v,w); } printf("Case %d: %d\n",k,Tr.dinic()); }}

 

 

 

 

转载于:https://www.cnblogs.com/EdSheeran/p/8458448.html

你可能感兴趣的文章
语义分析应用——美通社
查看>>
数据类型及操作
查看>>
提高前端开发效率的N种方法
查看>>
第一个Vus.js
查看>>
10款最好的Python IDE
查看>>
js如何获取样式?
查看>>
保护视力最佳电脑窗口颜色配置Win7、Vista和XP适用!转
查看>>
关于CentOS下 yum包下载下的rpm包放置路径
查看>>
PHPMyAdmin在Window下的安装
查看>>
asmlinkage宏
查看>>
python socket
查看>>
彻底解决降级安装失败无法彻底卸载应用bug
查看>>
C 语言 循环控制
查看>>
HTML5之Canvas绘图(二) ——应用篇之七巧板
查看>>
Java多线程:Linux多路复用,Java NIO与Netty简述
查看>>
ZOJ 2526 FatMouse and JavaBean II
查看>>
android中TextView字体的格式化
查看>>
C#数组学习
查看>>
JQuery插件机制
查看>>
Mariadb安装
查看>>