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. HDU 1171 Big Event in HDU (多重背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. Windows Phone开发(28):隔离存储B

    原文:Windows Phone开发(28):隔离存储B 上一节我们聊了目录的操作,这一节我们继续来看看如何读写文件. 首先说一下题外话,许多朋友都在摇摆不定,三心二意,其实这样的学习态度是很不好的, ...

  3. D3js-堆栈图

    效果图: 源码: <%@ page language="java" import="java.util.*" pageEncoding="UTF ...

  4. svn 使用(一个)

    一个. 安装svn  server(操作系统centos) yum install subversion 通过 subversion -v 如果成功安装命令来查看 温馨提示不承担任何subversio ...

  5. 全文检索引擎Solr 指南

    全文检索引擎Solr系列:第一篇:http://t.cn/RP004gl.第二篇:http://t.cn/RPHDjk7 .第三篇:http://t.cn/RPuJt3T

  6. aspx向silverlight传值

    原文:http://www.cnblogs.com/lensso/archive/2010/07/27/1785844.html 方法1: 向嵌入aspx页面的silverlight对象添加imnit ...

  7. Gradle 教程:第一部分,安装【翻译】(转)

    原文地址:http://rominirani.com/2014/07/28/gradle-tutorial-part-1-installation-setup/ 在这篇教程里,我们将主要讲解如何在我们 ...

  8. Android采取async框架文件上传

    页面效果 须要的权限 <uses-permission android:name="android.permission.INTERNET"/> 网络訪问权限; 布局文 ...

  9. Unix编程艺术札记版本

    这本书是一位面试官告诉我,.非常感谢他的忠告.面试的说明.我写后认为,足.一方面是面试的技巧.另一方面就是学习的方法. 阅读这本书非常享受,加上之前的积累,一些疑惑,另一些基础的不足,在这本书里找 ...

  10. NET5 Web应用程序

    ASP.NET5 Web应用程序结构 本文参考ASP.NET5 官方文档 Understanding ASP.NET 5 Web Apps,加入了一些个人理解,理解不对的地方希望大家能指出,互相学习. ...