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. LeetCode 606. Construct String from Binary Tree (建立一个二叉树的string)

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  2. asp.net在类库中使用EF 6.0时的相关配置

    前提:之前使用EF的配置都是直接使用NuGet安装在项目中,然后直接修改web.config中的connectionString,然后创建相关dbcontext直接使用就可以了.此次为直接将EF安装在 ...

  3. 使用angular4和asp.net core 2 web api做个练习项目(二), 这部分都是angular

    上一篇: http://www.cnblogs.com/cgzl/p/7755801.html 完成client.service.ts: import { Injectable } from '@an ...

  4. C++获取基类指针所指子类对象的类名

    我们在程序中定义了一个基类,该基类有n个子类,为了方便,我们经常定义一个基类的指针数组,数组中的每一项指向都指向一个子类,那么在程序中我们如何判断这些基类指针是指向哪个子类呢? 关键字 typeid, ...

  5. MapReduce简单分析

    在Map端 数据从Map中写入环形缓冲区,进行分区,分区时达到80%后溢出写入到磁盘,这几步同步进行 中间有个Shuffle过程 Reduce端 执行完Map 后到Reduce内存中,进行sort和m ...

  6. css设置黑体宋体等(转)

    代码如下: .selector{ font-family:"Microsoft YaHei",微软雅黑,"MicrosoftJhengHei",华文细黑,STH ...

  7. shadow dom 隔离代码 封装

    Shadow DOM是指浏览器的一种能力,它允许在文档(document)渲染时插入一棵DOM元素子树,但是这棵子树不在主DOM树中.   Shadow DOM 解决了 DOM 树的封装问题.     ...

  8. Java--集合(一)

    一.前序     前几篇文章我结合数据结构说了一些常用的集合,但是我感觉那样可能不系统,于是乎想着重写,按照由整体到细节的方式去写,这样才能更好的把握集合,废话不多说开始吧: 二.集合框架      ...

  9. php 常用 常量集合

    DIRECTORY_SEPARATOR 常量 DIRECTORY_SEPARATOR  目录分割符

  10. 聊下 git 多账户问题

    git 多账户问题 标签(空格分隔):git github gitlab git多账户 背景 git 多账号配置 ssh 多密钥对配置 背景 在使用 git 的时候我们都会面临多账户问题,比较常见的就 ...