题目连接

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

Flow 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

裸的最大流dinic,测模板。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::min;
using std::find;
using std::sort;
using std::pair;
using std::queue;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1100;
const int INF = 0x3f3f3f3f;
struct Dinic {
struct edge { int to, cap, next, rev; }G[N << 2];
int s, t, tot, level[N], ite[N], head[N];
inline void init() {
tot = 0, cls(head, -1);
}
inline void add_edge(int u, int v, int cap) {
G[tot] = (edge){ v, cap, head[u], tot + 1 }; head[u] = tot++;
G[tot] = (edge){ u, 0, head[v], tot - 1 }; head[v] = tot++;
}
inline void built(int n, int m) {
int u, v, f;
s = 1, t = n;
while(m--) {
scanf("%d %d %d", &u, &v, &f);
add_edge(u, v, f);
}
}
inline void bfs(int s) {
cls(level, -1);
queue<int> q;
q.push(s);
level[s] = 0;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = G[i].next) {
edge &e = G[i];
if(e.cap > 0 && level[e.to] < 0) {
level[e.to] = level[u] + 1;
q.push(e.to);
}
}
}
}
inline int dfs(int u, int t, int f) {
if(u == t) return f;
for(int &i = ite[u]; ~i; i = G[i].next) {
edge &e = G[i];
if(e.cap > 0 && level[u] < level[e.to]) {
int d = dfs(e.to, t, min(e.cap, f));
if(d > 0) {
e.cap -= d;
G[e.rev].cap += d;
return d;
}
}
}
return 0;
}
inline int max_flow() {
int flow = 0;
while(true) {
bfs(s);
if(level[t] < 0) break;
int f;
rep(i, t) ite[i] = head[i];
while((f = dfs(s, t, INF)) > 0) {
flow += f;
}
}
return flow;
}
inline void solve(int n, int m) {
static int k = 1;
init(), built(n, m);
printf("Case %d: %d\n", k++, max_flow());
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t, n, m;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
go.solve(n, m);
}
return 0;
}

hdu 3549 Flow Problem的更多相关文章

  1. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  2. 网络流 HDU 3549 Flow Problem

    网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...

  3. hdu 3549 Flow Problem【最大流增广路入门模板题】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...

  4. hdu 3549 Flow Problem Edmonds_Karp算法求解最大流

    Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...

  5. HDU 3549 Flow Problem 网络流(最大流) FF EK

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  6. hdu 3549 Flow Problem (网络最大流)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  7. HDU 3549 Flow Problem (最大流ISAP)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  8. hdu 3549 Flow Problem (Dinic)

    Flow ProblemTime Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  9. hdu 3549 Flow Problem 最大流问题 (模板题)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

随机推荐

  1. 分享一个MVC的多层架构,欢迎大家拍砖斧正

    如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 多层架构是开发人员在开发过程当中面对复杂且易变的需求采取的一种以隔离控制为主的应对策 ...

  2. webview渲染流程

    文档标记说明 ################# 消息边界 +++++++++++++++++ 区域分隔 $$$$$$$$$$$$$$$$$ 线程边界 ~~~~~~~~~~~~~~~~~ 进程边界 - ...

  3. 匿名管道 远程cmd

    管道是单向的,传送数据的方向是固定的,所以互相通信需要两个管道. STARTUPINFO si; ZeroMemory(&si,sizeof(si)); si.dwFlags = STARTF ...

  4. Java中自定泛型方法

    泛型用到哪些集合:List Set Map List<String> list=new ArraList<String>(); list.add("美女") ...

  5. jquery css快捷方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 学习java 第1天

    自己是刚刚开始自学java看了视频和书 =-=,刚开始我先对命令窗口进行了一个大体上的了解 ,首先是打开,可以右键开始键,然后选择命令提示符,我更喜欢第二种,就是使用Win+R,然后输入cmd进入,我 ...

  7. PayPal 开发详解(三):在网站上创建【立即付款】按钮

    1.使用[商家帐号]登录https://www.sandbox.paypal.com/ 2.点击[用户信息]->[其他选项]->[我保存的按钮] 3.选择[立即购买按钮事例] 4.[第一步 ...

  8. 使用C#三维绘图控件快速搭建DXF查看程序

    本例使用AnyCAD .Net三维图形控件快速实现一个DXF文件的读取.显示.导出JPG.PNG.PDF的应用. 代码: using System; using System.Collections. ...

  9. JS正则表达式使用方法及示例

    1.定义正则表达式: a.普通方式:var reg=/表达式/附加参数 附件参数: g:代表可以进行全局匹配.i:代表不区分大小写匹配.m:代表可以进行多行匹配. 上面三个参数,可以任意组合,代表复合 ...

  10. grep命令实战

    显示/etc/rc.d/rc.sysinit中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行 grep "^#[[:space:]]\+.\+" /etc/rc ...