Flow Problem

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

Total Submission(s): 15345    Accepted Submission(s): 7234

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
 代码:

//网络流:福特-富尔克森算法(二维数组版本)
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e3+5;
int v;
int capacity[maxn][maxn],flow[maxn][maxn];
//capacity保存u到v的容量,flow保存u到v的流量,反方向时为负
//函数计算返回总流量
int networkflow(int source, int sink) {
memset(flow,0,sizeof(flow));
int totalflow=0;
while(true) {
//BFS寻找增广路径
vector<int> parent(maxn,-1);
queue<int> q;
parent[source]=source;
q.push(source);
while(!q.empty()) {
int here=q.front();q.pop();
for(int there=0;there<v;++there)
//沿着还有剩余容量的边搜索
if(capacity[here][there]-flow[here][there]>0&&parent[there]==-1) {
q.push(there);
parent[there]=here;
}
}
//没有增广路经存在
if(parent[sink]==-1) break;
int amount=INF;
for(int p=sink;p!=source;p=parent[p]) {
amount=min(capacity[parent[p]][p]-flow[parent[p]][p],amount);
}
//决定通过增广路径传输流
for(int p=sink;p!=source;p=parent[p]) {
flow[parent[p]][p]+=amount;
flow[p][parent[p]]-=amount;
}
totalflow+=amount;
}
return totalflow;
}
int main() {
int t,cnt=1;
scanf("%d",&t);
while(t--) {
int n,m;
scanf("%d%d",&n,&m);
v=n;
memset(capacity,0,sizeof(capacity));
for(int i=0;i<m;++i) {
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
capacity[x-1][y-1]+=c;
}
printf("Case %d: %d\n",cnt++,networkflow(0,n-1));
}
return 0;
}

HDU 3549 Flow Problem 网络流(最大流) FF EK的更多相关文章

  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【最大流增广路入门模板题】

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

  3. HDU 3549 Flow Problem(最大流模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 刚接触网络流,感觉有点难啊,只好先拿几道基础的模板题来练练手. 最大流的模板题. #include< ...

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

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

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

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

  6. hdu 3549 Flow Problem 网络流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Network flow is a well-known difficult problem f ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known ...

  8. 题解报告:hdu 3549 Flow Problem(最大流入门)

    Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your t ...

  9. hdu 3549 Flow Problem 【最大流】

    其实还是不是很懂dinic----- 抄了一个模板--- http://www.cnblogs.com/naturepengchen/articles/4403408.html 先放在这里--- #i ...

随机推荐

  1. 【20171028早】ubuntu 16.04 LTS 安装php遇到的问题

    背景: 10月28日的一个早上,老黑一如往常地练习,我测试不破坏,当时我找到sqli-libs 游戏,可是我没有立即开始,于是,奇妙的事情就由php开始了.ubuntu16.04安装相关环境 apac ...

  2. #1094 : Lost in the City by C solution

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He does not know where he is ...

  3. net start mongodb 服务名无效解决方案

    net start mongodb 服务名无效 或者 net start mongodb 发生错误5,拒绝访问.是因为没有用管理员权限运行cmd. 解决方案:在window中,在搜索框输入cmd后,在 ...

  4. x86-64栈帧中的“红色区域” red zone of stack frame on x86-64

    前几天看System V AMD64 ABI标准的时候发现栈帧的顶部后面有一块"red zone",在学cs:app3e/深入理解操作系统的时候并没有遇到这个,总结一下. 引用标准 ...

  5. CS231n 2017 学习笔记01——KNN(K-Nearest Neighbors)

    本博客内容来自 Stanford University CS231N 2017 Lecture 2 - Image Classification 课程官网:http://cs231n.stanford ...

  6. Log4j 2翻译 Garbage-free Steady State Logging(稳定的以不会生成垃圾的状态来记录日志)

    本人菜鸟,在学习Log4j 2 的时候做的一些笔记---对"官方网站"的翻译,部分内容自己也不懂,希望大家指点 Garbage collection pauses are a co ...

  7. MS-SQL 错误: The offset specified in a OFFSET clause may not be negative

    Example 1 : (Fetch clause must be greater than zero) USE AdventureWorks2012 GO SELECT * FROM [HumanR ...

  8. 建立LINUX服务器

    建立LINUX服务器:一. 下载UltraISO工具并用该工具制作LINUX启动U盘,制作步骤如下:http://www.linuxidc.com/Linux/2012-11/74695.htm [^ ...

  9. Python函数篇:装饰器

    装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前提下增加额外的功能,装饰器的返回值也是一个函数对象.它经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理. ...

  10. 数组去重+indexOf()应用

    说起数组去重大家都不陌生,去重也有好多种方法,这里介绍很好理解的两种. 第一种 首先说一下第一种的逻辑,就是先拿第一个去跟第二个比,再跟第三个比,再跟第四个比--只要发现有相等的,可以用splice( ...