HDU3549 Flow Problem 【最大流量】
Flow Problem
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)
2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1
Case 1: 1
Case 2: 2
水题。
#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 【最大流量】的更多相关文章
- 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 ...
- HDU3549 Flow Problem(网络流增广路算法)
题目链接. 分析: 网络流增广路算法模板题.http://www.cnblogs.com/tanhehe/p/3234248.html AC代码: #include <iostream> ...
- [hdu3549]Flow Problem(最大流模板题)
解题关键:使用的挑战程序设计竞赛上的模板,第一道网络流题目,效率比较低,且用不习惯的vector来建图. 看到网上其他人说此题有重边,需要注意下,此问题只在邻接矩阵建图时会出问题,邻接表不会存在的,也 ...
- HDU3549:Flow Problem(最大流入门EK)
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> ...
- hdu 3549 Flow Problem Edmonds_Karp算法求解最大流
Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...
- Flow Problem
Flow Problem TimeLimit:5000MS MemoryLimit:32768KB 64-bit integer IO format:%I64d Problem Descript ...
- hdu 3549 Flow Problem 最大流问题 (模板题)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- HDU 3549 Flow Problem(最大流)
HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...
- hdu------(3549)Flow Problem(最大流(水体))
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
随机推荐
- CTR校准
普遍预测CTR不准,需要校准.例如.boosted trees and SVM预測结果趋于保守.即预測的概率偏向于中值:而对于NaiveBayes预測的概率,小概率趋于更小.大概率趋于更大.经常使用的 ...
- 构建自己的Java并发模型框架
Java的多线程特性为构建高性能的应用提供了极大的方便,可是也带来了不少的麻烦.线程间同步.数据一致性等烦琐的问题须要细心的考虑,一不小心就会出现一些微妙的,难以调试的错误. 另外.应用逻辑和线程逻辑 ...
- 基于Cocos2dx开发卡牌游戏_松开,这三个国家
1.它实现了动态读取地图资源.地图信息被记录excel桌格.假设你想添加地图,编者excel导入后CocoStudio数据编辑器,然后出口到Json档,到项目的Resource文件夹下. 2.SGFi ...
- awk与sed:关于多行的样本
几天前CSDN看到一个帖子http://bbs.csdn.net/topics/390848841,楼主贴了以下的问题: 例: 12345 67890 1234567890 123 4567890 怎 ...
- _00013 一致性哈希算法 Consistent Hashing 新的讨论,并出现相应的解决
笔者博文:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 个性签名:世界上最遥远的距离不是天涯,也不是海角,而是我站在妳的面前.妳却感觉不到我的存在 技术方向: ...
- Silverlight的Socket通信
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPwAAAGwCAIAAAACJJ+TAAAgAElEQVR4nO2deXgT5534Rdhskme7+9
- rsync+inotify 实现资源服务器的同步目录下的文件变化时,备份服务器的同步目录更新,以资源服务器为准,去同步其他客户端
测试环境: 资源服务器(主服务器):192.168.200.95 备份服务器(客户端):192.168.200.89 同步目录:/etc/test 同步时使用的用户名hadoop密码12345 实验目 ...
- Windows在结构Eclipse+Android4.0开发环境
官方搭建步骤: http://developer.android.com/index.html 搭建好开发环境之前须要下载以下几个文件包: 一.安装Java执行环境JRE(没这个Eclipse执行不起 ...
- iOS:编译错误 linker command failed with exit code 1 (use -v to see invocation)
将project不加入.m要求加入 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzI0MzQ2OQ==/font/5a6L5L2T/fontsi ...
- Dom操作高级应用
table tBodies,tHead,tFoot,rows,cells 一个table有多个tbody oTab.tBodies[0].rows[i].style.background = &quo ...