Flow Problem

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

Total Submission(s): 8387    Accepted Submission(s): 3908
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
 
Author
HyperHexagon
 
Source

水题。

#include <stdio.h>
#include <string.h> #define maxn 20
#define maxm 2010
#define inf 0x3f3f3f3f int head[maxn], n, m, source, sink, id; // n个点m条边
struct Node {
int u, v, c, next;
} E[maxm];
int que[maxn], pre[maxn], Layer[maxn];
bool vis[maxn]; void addEdge(int u, int v, int c) {
E[id].u = u; E[id].v = v;
E[id].c = c; E[id].next = head[u];
head[u] = id++; E[id].u = v; E[id].v = u;
E[id].c = 0; E[id].next = head[v];
head[v] = id++;
} void getMap() {
int u, v, c; id = 0;
scanf("%d%d", &n, &m);
memset(head, -1, sizeof(int) * (n + 1));
source = 1; sink = n;
while(m--) {
scanf("%d%d%d", &u, &v, &c);
addEdge(u, v, c);
}
} bool countLayer() {
memset(Layer, 0, sizeof(int) * (n + 1));
int id = 0, front = 0, u, v, i;
Layer[source] = 1; que[id++] = source;
while(front != id) {
u = que[front++];
for(i = head[u]; i != -1; i = E[i].next) {
v = E[i].v;
if(E[i].c && !Layer[v]) {
Layer[v] = Layer[u] + 1;
if(v == sink) return true;
else que[id++] = v;
}
}
}
return false;
} int Dinic() {
int i, u, v, minCut, maxFlow = 0, pos, id = 0;
while(countLayer()) {
memset(vis, 0, sizeof(bool) * (n + 1));
memset(pre, -1, sizeof(int) * (n + 1));
que[id++] = source; vis[source] = 1;
while(id) {
u = que[id - 1];
if(u == sink) {
minCut = inf;
for(i = pre[sink]; i != -1; i = pre[E[i].u])
if(minCut > E[i].c) {
minCut = E[i].c; pos = E[i].u;
}
maxFlow += minCut;
for(i = pre[sink]; i != -1; i = pre[E[i].u]) {
E[i].c -= minCut;
E[i^1].c += minCut;
}
while(que[id-1] != pos)
vis[que[--id]] = 0;
} else {
for(i = head[u]; i != -1; i = E[i].next)
if(E[i].c && Layer[u] + 1 == Layer[v = E[i].v] && !vis[v]) {
vis[v] = 1; que[id++] = v; pre[v] = i; break;
}
if(i == -1) --id;
}
}
}
return maxFlow;
} void solve(int i) {
printf("Case %d: %d\n", i, Dinic());
} int main() {
int t, cas;
scanf("%d", &t);
for(cas = 1; cas <= t; ++cas) {
getMap();
solve(cas);
}
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

HDU3549 Flow Problem 【最大流量】的更多相关文章

  1. Hdu3549 Flow Problem 2017-02-11 16:24 58人阅读 评论(0) 收藏

    Flow Problem Problem Description Network flow is a well-known difficult problem for ACMers. Given a ...

  2. HDU3549 Flow Problem(网络流增广路算法)

    题目链接. 分析: 网络流增广路算法模板题.http://www.cnblogs.com/tanhehe/p/3234248.html AC代码: #include <iostream> ...

  3. [hdu3549]Flow Problem(最大流模板题)

    解题关键:使用的挑战程序设计竞赛上的模板,第一道网络流题目,效率比较低,且用不习惯的vector来建图. 看到网上其他人说此题有重边,需要注意下,此问题只在邻接矩阵建图时会出问题,邻接表不会存在的,也 ...

  4. HDU3549:Flow Problem(最大流入门EK)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> ...

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

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

  6. Flow Problem

    Flow Problem TimeLimit:5000MS  MemoryLimit:32768KB 64-bit integer IO format:%I64d   Problem Descript ...

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

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

  8. HDU 3549 Flow Problem(最大流)

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

  9. hdu------(3549)Flow Problem(最大流(水体))

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

随机推荐

  1. 分解XML方法

    分解XML方法 1.DOM生成和解析XML 2.SAX生成和解析XML 3.DOM4J生成和解析XML 4.JDOM生成和解析XML 版权声明:本文博主原创文章.博客,未经同意不得转载.

  2. [PATCH] UBUNTU: SAUCE: (no-up) apparmor: Sync to apparmor3 - RC1(v3.4.x kernel)

    ubuntu touch v3.4 kernel AppArmor v3 backport patch 地址1:https://github.com/multirom-aries/ubuntu-pho ...

  3. Bash ShellShock 解决办法

    2014 年 9 月 24 日,Bash 惊爆严重安全漏洞,编号为 CVE-2014-6271,该漏洞将导致远程攻击者在受影响的系统上执行任意代码.GNU Bash 是一个为 GNU 计划编写的 Un ...

  4. HTTP相关概念

    最近观看HTTP权威指南.这本书是一个小更,欲了解更多详细信息,我们不能照顾.但一些基本概念仍然应该清楚.在这里,我整理: HTTP--因特网的多媒体信使 HTTP 使用的是可靠的传输数据协议,因此即 ...

  5. 重写PHP的explode办法

    function rexplode($delimiter, $str){ $d_len = strlen($delimiter); $arr = array(); $i = $pos = 0; whi ...

  6. NSOperation 的使用(下载相关) 图片和文件都是能够的 断点续传 图片逐渐显示

    // // ImageDownloader.h // NSOperationTest // // Created by ydc on 11-10-29. // Copyright 2011年 __My ...

  7. SQLite Code配置DbConfiguration

    [DbConfigurationType(typeof(SQLiteConfiguration))] public partial class rsapiEntities : DbContext { ...

  8. java.nio分析软件包(三)---Charset理解力

    前面的分析后,2一个基本的封装类型.现在我们就来揭开Java.nio魔法知识的最后一块,CharsetEncoding类,他的主要功能是实现字节Unicode之间的转换转码. 让我们来看看他同样的封装 ...

  9. DB2 “The transaction log for the database is full” 存在的问题及解决方案

    DB2在执行一个大的insert/update操作的时候报"The transaction log for the database is full.. "错误,查了一下文档是DB ...

  10. Linux rpm 命令参数使用详解[介绍和应用](转)

    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种 ...