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. gitlab 配置 ssh && ubuntu

    1,在你的电脑上生成密钥ssh-keygen -t rsa -C "youeamil@explode.com" 2,在 ubuntu系统中 ~/.ssh目录中生成了两个文件id_r ...

  2. U3D 收藏一个飞机随机运动的方法

    文章转载:http://www.manew.com/thread-43578-1-1.html 前面的学习中已经涉及到了随机运动,这一篇主要还是前面的随机运动的改进,不废话直接上效果图吧,对比前面的随 ...

  3. Quarzt.NET的Cron表达式理解

    网上关于Quarzt.NET的Cron表达式介绍有很多,但都是基本的语法,稍微深入一些的就没有了. 基本语法介绍请参看: http://www.cnblogs.com/lzrabbit/archive ...

  4. js跨浏览器事件对象、事件处理程序

    项目中有时候会不用jquery这么好用的框架,需要自己封装一些事件对象和事件处理程序,像封装AJAX那样:这里面考虑最多的还是浏览器的兼容问题,原生js封装如下:var EventUtil={ //节 ...

  5. Ubuntu下解压rar文件的方法

    原帖地址:http://hi.baidu.com/remoteexp/item/1c32d0ffb92e946c3c148596 一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了ra ...

  6. C++专题 - 面向对象总结

    1.         什么是类?什么是对象?对象与类的关系是什么? 答:类就是相同的数据和相同的一组对象的集合,即类是对具有相同数据结构和相同操作的一类对象的描述: 对象是描述其属性的数据以及对这些数 ...

  7. createjs 下雪 实例

    demo:  http://output.jsbin.com/davixatona <!DOCTYPE html> <html> <head> <meta c ...

  8. jquery中mouseout和mouseleave 事件的区别

    今天用jQuery写了一个选项卡的效果,用mouseout事件控制了鼠标的移出,结果发现在移出时div会发生闪动,于是网上各种查资料觉得用mouseleave更合适一些,

  9. 浅谈ListBox控件,将对象封装在listBox中,在ListBox中显示对象中某个属性,在ListBox中移除和移动信息

    大家好,俗称万事开头难,不经历风雨,怎能见彩虹.在此小编给大家带来一个自己练习的小实例,希望与大家一起分享与交流.下面进入应用场景,从SQL2008数据库取出数据,给ListBox赋值在界面并显示出来 ...

  10. 解决SDK Manager无法更新问题

    因为google被封了,导致Android SDK Manager无法更新,解决方案如下: 1.选择tools->options,跳出Settings页面 2.设置HTTP Proxy代理,设置 ...