HDU 3549 Flow Problem
2016-02-03 18:32
246 查看
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
Sample Output
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网络流模板题,先来一发dinic的#include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> #include<queue> using namespace std; const int maxn = 1e5 + 10; int T, n, m, z = 0; struct MaxFlow { struct Edges { int end, flow; Edges(){} Edges(int end, int flow) :end(end), flow(flow){} }edge[maxn]; int first[maxn], next[maxn], dis[maxn], tot; void clear(){ tot = 0; memset(first, -1, sizeof(first)); } void AddEdge(int s, int t, int flow) { edge[tot] = Edges(t, 0); next[tot] = first[s]; first[s] = tot++; edge[tot] = Edges(s, flow); next[tot] = first[t]; first[t] = tot++; } bool bfs(int s, int t) { memset(dis, -1, sizeof(dis)); queue<int> p; p.push(s); dis[s] = 0; while (!p.empty()) { int q = p.front(); p.pop(); for (int i = first[q]; i >= 0; i = next[i]) { if (edge[i ^ 1].flow&&dis[edge[i].end] == -1) { p.push(edge[i].end); dis[edge[i].end] = dis[q] + 1; } } } return dis[t] != -1; } int dfs(int s, int t, int low) { if (s == t) return low; for (int i = first[s]; i >= 0; i = next[i]) { if (dis[s] + 1 == dis[edge[i].end] && edge[i ^ 1].flow) { int x = dfs(edge[i].end, t, min(low, edge[i ^ 1].flow)); if (x) { edge[i].flow += x; edge[i ^ 1].flow -= x; return x; } } } return 0; } int dinic(int s, int t) { int maxflow = 0, inc = 0; while (bfs(s, t)) while (inc = dfs(s, t, 0x7FFFFFFF)) maxflow += inc; return maxflow; } }solve; int main() { scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); int s, t, flow; solve.clear(); while (m--) { scanf("%d%d%d", &s, &t, &flow); solve.AddEdge(s, t, flow); } printf("Case %d: %d\n", ++z, solve.dinic(1, n)); } return 0; }
相关文章推荐
- NYOJ 1066 CO-PRIME(数论)
- Python3.4爬虫编程
- START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */
- FIR with FPGA[1].全并行FIR实现
- Redis入门
- C++链队列类实现
- Android常用开源项目库
- 连接断开导致XA事务丢失
- mysql实现随机获取几条数据的方法
- svn 用法零散速记
- 广西首届网络安全选拔赛PWN、REVERSE、决赛题目
- hdu4352 XHXJ's LIS
- TokuDB · 行锁(row-lock)与区间锁(range-lock)
- HTML5开发移动web应用——SAP UI5篇(1)
- bzoj2427 软件安装
- iOS上视频剪辑片段和添加背景音乐
- [Leetcode] 331
- RBM学习算法
- 由浅入深探究 MySQL索引结构原理、性能分析与优化
- 史上最强大的程序猿装X教程