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. Linux同步时间命令ntpdate

    转自:http://orgcent.com/linux-ntpdate/ 由于要同步Linux服务器的时间,为了保证时间的高度准确性,放弃date命令而转向ntpdate(同步时间命令). 方案如下: ...

  2. C蛮的全栈之路-node篇(二) 实战一:自动发博客

    目录 C蛮的全栈之路-序章 技术栈选择与全栈工程师C蛮的全栈之路-node篇(一) 环境布置C蛮的全栈之路-node篇(二) 实战一:自动发博客 ---------------- 我是分割线 ---- ...

  3. 关于项目既要使用ant脚本又要使用maven pom.xml文件的问题

    背景:项目使用的是ant脚本打包,但又需要maven去执行sonar代码扫描.所以项目中既有build.xml又有pom.xml build.xml设置的打包后产物文件夹为target,maven运行 ...

  4. Codeforces 549C The Game Of Parity(博弈)

    The Game Of Parity Solution: 这个题只需要分类讨论就可以解决. 先分别统计奇数和偶数的个数. 然后判断谁走最后一步,如果走最后一步时候同时有偶数和奇数,那么走最后一步的赢. ...

  5. ecshop 函数列表大全

    lib_time.phpgmtime() P: 获得当前格林威治时间的时间戳 /$0server_timezone() P: 获得服务器的时区 /$0local_mktime($hour = NULL ...

  6. Webstorm6的汉化以及主题设置

    Webstorm6.0.2界面截图: webstorm作为一款前端开发软件,被业内称为神器,下面是下载地址. 需要的人太多,邮件不过来,传到这边方便大家下载 汉化包 http://www.jetbra ...

  7. 看es6 字符串新方法有感

    'x'.repeat(3) // "xxx" 'hello'.repeat(2) // "hellohello" 'na'.repeat(0) // " ...

  8. Balsamiq Mockups

    Balsamiq Mockups完全手册 2010.03.18 发布 48,066 浏览 什么是Balsamiq Mockups Balsamiq Mockups出自加利福尼亚州的Balsamiq工作 ...

  9. paramiko模块实现堡垒机

    通过SSHClient 执行命令 """通过用户名密码验证""" import paramiko # 创建 SSH 对象 ssh = par ...

  10. 不同优化选项对ARM下C语言编译的影响

    我们知道在C语言编译时,有那么几个常用的优化编译选项,分别是-O0,-O1,-O2,-O3以及-Os.之前一直觉得既然是优化选项,顶多是优化一下逻辑,提高一些效率或者减少一下程序大小而已.很少会觉得它 ...