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. Singleton设计模式 分类: 设计模式 2014-12-03 17:54 59人阅读 评论(0) 收藏

    实现方法: public class SingleTon<T> where T : class, new() {     protected SingleTon() { }     pri ...

  2. Android手势解锁, 九宫格解锁

    给大家介绍一个很好用的手势解锁控件ShapleLocker, 废话不多先上效果图: 这是一个第三方库, 可自己根据UI需求替换图标: 圆圈, 小箭头等等.. github地址: http://pane ...

  3. SQL通过传递参数方式备份数据库.

    存储过程的SQL代码: ALTER PROCEDURE USP_DBBackup ), --存储目录. ) --存储数据库名. AS SET NOCOUNT ON ) select @name = r ...

  4. [解答]对‘’未定义的引用 collect2: 错误: ld 返回 1

    写的makefile适用于32位,但是放到64位机器上make就有问题. 需要在makefile中gcc -o....的结尾加上-pthread,例如: bloomfilter.o: bloomfil ...

  5. QT UI 使一个QWidget里面的元素自动填充满本QWidget

    使一个QWidget里面的元素自动填充满本QWidget: 对象查看器,右键点击本QWidget,选择"布局",为此QWidget增加一个布局. 如果该QWidget只有一个对象, ...

  6. dp题目

    从别的地方看来,最近一直在啃DP,有个目标,更有动力了. 1.Robberies 连接 :http://acm.hdu.edu.cn/showproblem.php?pid=2955      背包; ...

  7. 批量缩放PNG图片.

    最近需要缩放N多图片, 找遍了互联网也没有找到方便使用的批量缩放工具.. 趁着周末写一个练手.. #include <iostream> #include <vector> # ...

  8. Win32中GDI+应用(四)--- 位图的打开与显示

    显示位图,你应该使用GDI+里面的Bitmap类或者Image类,这两个类都提供了方法从硬盘上的一个文件打开文件,创建相应的内存中的位图对象的工作.然后你可以使用Graphics类的DrawImage ...

  9. [转]svn 命令大全

    博客地址:http://blog.csdn.net/gexiaobaohelloworld/article/details/7752862

  10. javascript 一串DIV跟随鼠标移动

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...