Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 6336    Accepted Submission(s): 2943

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

思路:最大网络流模板题,都是FF方法,根据增广路求增广流量,算法的关键在于如何快速求增广路。Edmond-Karp是根据BFS求增广路,Dinic则先通过BFS将残余网络进行层次划分,即将距离源点S边数为x的点标记为level[i] = x,如果level[j] = level[i] + 1且res[i][j] > 0,那么<i,j>称为可行边。再对残余网络进行一遍DFS,搜索的时候就是根据level[i] = level[s]+1且res[s][i] > 0来向下递归的,所以相对于EK算法可以减少无谓的搜索,这样只要汇点T在层次图中,总会找到它。

Edmod-Karp算法:

 #include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
int res[][], vis[], pre[];
queue<int>q;
int N, M;
bool bds(int s, int t)
{
memset(vis, , sizeof(vis));
memset(pre, -, sizeof(pre));
while(!q.empty())
q.pop();
q.push(s);
vis[s] = ;
while(!q.empty())
{
int p = q.front();
q.pop();
for(int i = ;i <= N;i ++)
{
if(!vis[i] && res[p][i] > )
{
vis[i] = ;
pre[i] = p;
q.push(i);
if(i == t)
return true;
}
}
}
return false;
} int EK(int s, int t)
{
int flow = , d, i, u;
while(bds(s, t))
{
d = << ;
u = t;
while(pre[u] != -)
{
d = min(d, res[pre[u]][u]);
u = pre[u];
}
u = t;
while(pre[u] != -)
{
res[pre[u]][u] -= d;
res[u][pre[u]] += d;
u = pre[u];
}
flow += d;
}
return flow;
} int main(int argc, char const *argv[])
{
int T, u, v, w, cnt = ;
//freopen("in.c", "r", stdin);
scanf("%d", &T);
while(T--)
{
memset(res, , sizeof(res));
scanf("%d%d", &N, &M);
for(int i = ;i < M;i ++)
{
scanf("%d%d%d", &u, &v, &w);
res[u][v] += w;
}
printf("Case %d: %d\n", ++cnt, EK(, N));
}
return ;
}

Dinic算法:

 #include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
int res[][], level[], N, M, flow;
queue<int>q;
bool bfs(int s)
{
while(!q.empty())
q.pop();
memset(level, -, sizeof(level));
level[s] = ;
q.push(s);
while(!q.empty())
{
int p = q.front();
q.pop();
for(int i = ;i <= N;i ++)
{
if(level[i] == - && res[p][i] > )
{
level[i] = level[p] + ;
q.push(i);
}
}
}
if(level[N] >= )
return true;
return false;
} int dinic(int s, int sum)
{
if(s == N)
return sum;
int os = sum;
for(int i = ;i <= N;i ++)
{
if(level[i] == level[s] + && res[s][i] > )
{
int temp = dinic(i, min(sum, res[s][i]));
res[s][i] -= temp;
res[i][s] += temp;
sum -= temp;
}
}
return os - sum;
} int main(int argc, char const *argv[])
{
int T, u, v, w,cnt = ;
// freopen("in.c", "r", stdin);
scanf("%d", &T);
while(T--)
{
flow = ;
memset(res, , sizeof(res));
scanf("%d%d", &N, &M);
for(int i = ; i < M;i ++)
{
scanf("%d%d%d", &u, &v, &w);
res[u][v] += w;
}
while(bfs())
flow += dinic(, << );
printf("Case %d: %d\n", ++cnt,flow);
}
return ;
}

HDU --3549的更多相关文章

  1. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  2. Flow Problem HDU - 3549

    Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...

  3. 网络流 HDU 3549 Flow Problem

    网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...

  4. HDU 3549 网络最大流再试

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 同样的网络最大流 T了好几次原因是用了cout,改成printf就A了 还有HDU oj的编译器也不支持以 ...

  5. hdu 3549 Flow Problem

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

  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 模板题,白书上的代码... #include <iostream> #include & ...

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

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

  9. hdu 3549 Flow Problem【最大流增广路入门模板题】

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

  10. HDU 3549 Flow Problem (dinic模版 && isap模版)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 题意: 给你一个有向图,问你1到n的最大流. dinic模版 (n*n*m) #include ...

随机推荐

  1. 对于百川SDK签名验证的问题

    SDK是要在wantu.taobao.com生成的.而生成这个SDK其实是要上传一个apk,而这个上传其实就是取他的签名而已.验证就是那张yw222那张图片.重点是你上传的apk的签名是不是跟你的生成 ...

  2. sql server split函数

    --创建分割函数CREATE FUNCTION dbo.Split(@String varchar(8000),@Delimiter char(1))returns @temptable TABLE ...

  3. SQL2005 学习笔记 窗口函数(OVER)【转】

    1.简介: SQL Server 2005中的窗口函数帮助你迅速查看不同级别的聚合,通过它可以非常方便地累计总数.移动平均值.以及执行其它计算. 窗口函数功能非常强大,使用起来也十分容易.可以使用这个 ...

  4. PHP 学习笔记 (四)

    Wordpress 框架中的一些函数: wp_get_nav_menu($default): 根据条件$default 获取menu, 默认获取所有的menu 其中 $default 默认如下所示: ...

  5. SGU 246. Black & White(数论)

    题意: 有2*n-1个黑色和白色的珠子组成的环形项链,求至少需要多少颗黑色珠子才能使任意排列的项链中都存在两个黑珠间有n个珠子. (2*n-1<=2^31-1); Solution: 先分析n= ...

  6. ACM YTU 2018 母牛的故事

    母牛的故事 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  7. php中如何实现网上商城用户历史浏览记录的代码

    /如是COOKIE 里面不为空,则往里面增加一个商品ID if (!empty($_COOKIE['SHOP']['history'])){ //取得COOKIE里面的值,并用逗号把它切割成一个数组 ...

  8. oracle创建第三方数据接口表,指定特定用户访问某张表

    /*****创建用户并指定操作哪张表开始******/ --1.创建用户并设置默认表空间 CREATE USER CHENGDWY IDENTIFIED BY CHENGDWY DEFAULT TAB ...

  9. javascript 自定义鼠标右键菜单

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. StrongReference

    原创作品:未经本人允许,不得转载前段时间写项目时遇到了一个问题,就是从网络获取图片资源的问题,总是出现OOM异常,经过几天的努力,终于处理的还算是可以使用,OOM的处理一直都是很头疼的问题.对于三级缓 ...