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. [Excel] C#DataToExcel帮助类 (转载)

    点击下载 DataToExcel.rar 看下面代码吧 /// <summary> /// 类说明:DataToExcel /// 编 码 人:苏飞 /// 联系方式:361983679 ...

  2. js - get-the-value-from-the-url-parameter(可以在非模态对话框中使用)

    ref: http://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-url-parameter 函数: funct ...

  3. hibernate 对象状态异常:object references an unsaved transient instance - save the transient instance before flushing

    我的问题出在,删除的对象对应的表中有一个外键,关联着另外一个表,可是另外一个表中没有数据,所以报了这个错误. 参考http://www.cnblogs.com/onlywujun/archive/20 ...

  4. ios专题 - socket(1)

    二,BSD socket API 简介 BSD socket API 和 winsock API 接口大体差不多,下面将列出比较常用的 API: API接口 讲解 int socket(int add ...

  5. 手机定位原理 - GPS/GLONASS/北斗 + WIFI + 基站

    卫星定位系统 - GPS/GLONASS/北斗: 关于GPS.GLONASS.北斗.伽利略系统的科普请自行谷歌. GPS是使用最广泛的全球定位网络,几乎是所有智能手机的标配.进几年,俄罗斯的GLONA ...

  6. 24种设计模式--模版方法模式【Template Method Pattern】

    周三,9:00,我刚刚坐到位置,打开电脑准备开始干活.“小三,小三,叫一下其它同事,到会议室,开会”老大跑过来吼,带着淫笑.还不等大家坐稳,老大就开讲了,“告诉大家一个好消息,昨天终于把牛叉模型公司的 ...

  7. mount的艺术

    在阅读本文之前,我假设你已经对Linux系统下的硬盘.光盘的设备命令规则有所了解,比如sda和sda1的关系,以及hda.sda.fd.cdrom等设备. === 1 我把U盘插到USB口上了,下一步 ...

  8. python模块学习 hashlib

    一.hashlib概述 涉及加密服务:14. Cryptographic Services 其中 hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法借口,如SHA1.SHA224.SHA2 ...

  9. nginx服务器,php-fpm重启

    1.重启nginx服务器:首先whereis nginx找到你的nginx命令执行文件所在目录,直接/usr/local/nginx/sbin/nginx -s reload 这个路径可能每个人不一样 ...

  10. ASP.NET 后台不识别ASPX中的控件

    请问后台不识别ASPX中的控件,怎么解决 这个程序是在网上下载的 C# code <asp:DataGrid runat="server" ID="dgList1& ...