hdu 3549 Flow Problem (Dinic)
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 21438 Accepted Submission(s): 10081
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.
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)
For each test cases, you should output the maximum flow from source 1 to sink N.
2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1
Case 1: 1
Case 2: 2
C/C++:
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int my_max = ; int N, M, my_map[my_max][my_max], my_source, my_sink
, my_dis[my_max]; int my_dfs(int my_step, int my_ans)
{
if (my_step == my_sink) return my_ans; int my_temp = my_ans;
for (int i = ; i <= N && my_ans; ++ i)
{
if (my_dis[my_step] == my_dis[i] + && my_map[my_step][i])
{
int t = my_dfs(i, min(my_ans, my_map[my_step][i]));
my_map[my_step][i] -= t;
my_map[i][my_step] += t;
my_ans -= t;
}
}
return my_temp - my_ans;
} bool my_bfs()
{
memset(my_dis, -, sizeof(my_dis));
queue <int> Q;
my_dis[my_sink] = ;
Q.push(my_sink);
while (!Q.empty())
{
int now = Q.front();
for (int i = ; i <= N; ++ i)
{
if (my_map[i][now] > && my_dis[i] == -)
{
my_dis[i] = my_dis[now] + ;
Q.push(i);
}
}
if (now == my_source) return true;
Q.pop();
}
return false;
} int my_dinic()
{
int my_ans = ;
while (my_bfs())
my_ans += my_dfs(my_source, INF); return my_ans;
} int main()
{
int t;
scanf("%d", &t);
for (int i = ; i <= t; ++ i)
{
memset(my_map, , sizeof(my_map));
scanf("%d%d", &N, &M);
my_source = , my_sink = N; while (M --)
{
int x, y, x_y;
scanf("%d%d%d", &x, &y, &x_y);
my_map[x][y] += x_y;
}
printf("Case %d: %d\n", i, my_dinic());
}
return ;
}
hdu 3549 Flow Problem (Dinic)的更多相关文章
- HDU 3549 Flow Problem (dinic模版 && isap模版)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 题意: 给你一个有向图,问你1到n的最大流. dinic模版 (n*n*m) #include ...
- HDU 3549 Flow Problem(最大流)
HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...
- 网络流 HDU 3549 Flow Problem
网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...
- hdu 3549 Flow Problem
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...
- hdu 3549 Flow Problem【最大流增广路入门模板题】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...
- 网络流--最大流--HDU 3549 Flow Problem
题目链接 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, y ...
- hdu 3549 Flow Problem Edmonds_Karp算法求解最大流
Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...
- HDU 3549 Flow Problem 网络流(最大流) FF EK
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- hdu 3549 Flow Problem (网络最大流)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
随机推荐
- How to Compute The Derivatives (如何求导数)(TBC)
A video by 3Blue1Brown in Bilibili\text{A video by 3Blue1Brown in Bilibili}A video by 3Blue1Brown in ...
- Prometheus 源码解读(一)
Prometheus 源码解读(一) Prometheus 是云原生监控领域的事实标准,越来越来的开源项目开始支持 Prometheus 监控数据格式.从本篇开始,我将和大家一起阅读分析 Promet ...
- 可实现的全局唯一有序ID生成策略
在博客园搜素全局唯一有序ID,罗列出来的文章大致讲述了以下几个问题,常见的生成全局唯一id的常见方法 :使用数据库自动增长序列实现 : 使用UUID实现: 使用redis实现: 使用Twitter的 ...
- 设计模式(八)Abstract Factory模式
抽象工厂的工作是将“抽象零件”组装为“抽象产品”.在抽象工厂模式中将会出现抽象工厂,它会将抽象零件组装为抽象产品.也就是说,我们并不关心零件的具体实现,而是只关心接口.我们仅适用该接口将零件组装起来成 ...
- unity www下载导致内存占用增加问题
服务端或者数据库更改导致客户端更改,最合理的处理方法是客户端时刻检测版本号(可以通过实时检测版本号),如果实时刷新数据库的数据开销比较大,尤其是有图片元素时. 采用unity www类下载时,虽然结束 ...
- react框架安装和使用
react 其实react跟vue差不多, 区别:vue- 双向数据绑定, react 单向数据绑定. 中文文档:https://react.docschina.org/ 第一步:安装方式,不能直 ...
- ios Autolayout 按比例相对布局
看到一篇讲ios storyboard 按比例相对布局的博客,挺不错的转下来了! 可到liumh.com查看. 本文记录如何在 UIStoryboard 或者 xib 中进行百分比布局,包括 View ...
- zookeeper+springboot+dubbo简单实现
第一步:在虚拟机中搭建zookeeper. 第二步:本地创建3个maven工程,分别为wxh-dubbo-api(对外暴露的接口),wxh-dubbo-provider(服务提供者,接口的具体实现), ...
- 【EmguCV视频教程】VS2017+EmguCV3.4(C# OpenCV)高清入门视频教程
视频采用VS2017 + EmguCV3.4版本录制,内容类似本人的Python和C++版本,如果需要的朋友可加我咨询,视频共40讲,从按照到读取显示图片,图形预处理,边缘检测,形态学,角点检测,轮廓 ...
- 【Medium翻译】Java抽象类有什么用?
今天安利一个网站,其实很多朋友应该早就知道了,我之前ARTS打卡,英文文档的 很多出处就来自于这个网站,叫 「Medium」. 这个网站需要一定的技术去访问,但是为什么说他好呢,因为他号称全球最大的高 ...