HDU 3549 Flow Problem 网络流(最大流) FF EK
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 15345 Accepted Submission(s): 7234
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<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的更多相关文章
- 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【最大流增广路入门模板题】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...
- HDU 3549 Flow Problem(最大流模板)
http://acm.hdu.edu.cn/showproblem.php?pid=3549 刚接触网络流,感觉有点难啊,只好先拿几道基础的模板题来练练手. 最大流的模板题. #include< ...
- HDU 3549 Flow Problem (最大流ISAP)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- 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 网络流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Network flow is a well-known difficult problem f ...
- hdu 3549 Flow Problem(最大流模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known ...
- 题解报告:hdu 3549 Flow Problem(最大流入门)
Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your t ...
- hdu 3549 Flow Problem 【最大流】
其实还是不是很懂dinic----- 抄了一个模板--- http://www.cnblogs.com/naturepengchen/articles/4403408.html 先放在这里--- #i ...
随机推荐
- Java基础笔记13
1.集合与对象数组的区别 集合与对象数组共同点:都是存放对象的容器: 区别在于:①集合是没有长度限制的:②集合容器中没有类型的限制. 2.集合(都在Java.util包下) 常用的集合:Collect ...
- C++ 标准库之iomanip
C++ 标准库之iomanip istream & istream::get(char *, int, char = '\n');istream & istream::getline( ...
- code force 424 A - Office Keys
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- 使用sklearn进行数据挖掘-房价预测(6)—模型调优
通过上一节的探索,我们会得到几个相对比较满意的模型,本节我们就对模型进行调优 网格搜索 列举出参数组合,直到找到比较满意的参数组合,这是一种调优方法,当然如果手动选择并一一进行实验这是一个十分繁琐的工 ...
- Hive中常用的查询命令
日志数据的统计处理在这里反倒没有什么特别之处,就是一些 SQL 语句而已,也没有什么高深的技巧,不过还是列举一些语句示例,以示 hive 处理数据的方便之处,并展示 hive 的一些用法. a) ...
- git使用教程之git分支
1 分支简介 让我们来看一个简单的分支新建与分支合并的例子,实际工作中你可能会用到类似的工作流. 你将经历如下步骤: 开发某个网站. 为实现某个新的需求,创建一个分支. 在这个分支上开展工作. 正在此 ...
- C++中模板template <typename T>
最近在看C++的源码,遇到了不少问题,一点一点进行补充. 首先就是遇到template <typename Dtype>. 网上解释的非常多,觉得比较啰嗦,其实就是一个类型模板. 比如我们 ...
- 利用HTML5新特性改变浏览器地址后不刷新页面
原文:http://www.cnblogs.com/xuchengzone/archive/2013/04/18/html5-history-pushstate.html 作为一个程序员,上Git ...
- HTML5网页音乐播放器
1功能介绍 HTML5中推出了音视频标签,可以让我们不借助其他插件就可以直接播放音视频.下面我们就利用H5的audio标签及其相关属性和方法来制作一个简单的音乐播放器.主要包括以下几个功能: 1.播放 ...
- Minix3信号处理分析
进程的信号处理的相关结构 PM中存放着所有进程的进程描述符,在一个进程描述符中,有一个指针,指向一个sigaction结构二维数组中的一项,表示这个进程所有信号的操作.一个sigaction结构包含信 ...