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 ...
随机推荐
- std::unordered_map
map与unordered_map的区别 1.map: map内部实现了一个红黑树,该结构具有自动排序的功能,因此map内部的所有元素都是有序的,红黑树的每一个节点都代表着map的一个元素, 因此,对 ...
- QuartzCode快速开发动画代码工具
QuartzCode快速开发动画代码工具 QuartzCode一款快速,轻量,强大的动画工具,可快速得到原生的ObjC/Siwft代码 我可以用QuartzCode做什么? 应用程序演练动画 动画菜单 ...
- cmd 获取当前登录的用户和远程连接的用户
打开cmd 执行 quser 可以看到我有两个 会话 带> 是我当前的会话 rdp 是远程连接的会话 console 是本机操作 可以知道谁在连接你 状态是 唱片 就是未连接的意思 ...
- 21.Nginx代理缓存
1.环境准备 操作系统 应用服务 外网地址 内网地址 CentOS7.6 LB01 10.0.0.5 172.16.1.5 CentOS7.6 Web01 10.0.0.7 172.16.1.7 2. ...
- Mybaits 源码解析 (二)----- 根据配置文件创建SqlSessionFactory(Configuration的创建过程)
我们使用mybatis操作数据库都是通过SqlSession的API调用,而创建SqlSession是通过SqlSessionFactory.下面我们就看看SqlSessionFactory的创建过程 ...
- PHP使用RabbitMQ消息队列
1.安装amqp拓展 安装流程 2.下载工具包 php-amqplib composer require php-amqplib/php-amqplib 3.代码操作如下 [消费消息] < ...
- [UWP]使用GetAlphaMask制作阴影
1. 前言 最近常常接触到GetAlphaMask,所以想写这篇文章介绍下GetAlphaMask怎么使用.其实GetAlphaMask的使用场景十分有限,Github上能搜到的内容都是用来配合Dro ...
- 使用face-api.js实现人脸识别(一)
功能 第一阶段实现对图片中人脸的识别并打上标签(比如:人名) 第二阶段使用摄像头实现对人物的识别,比如典型的应用做一个人脸考勤的系统 资源 face-api.js https://github.com ...
- js多物体多方向缓动动画加带有回调机制
一.获取一组div元素 var boxs = document.getElementsByTagName('div'); 二.封装获取属性值的函数 function getStyle(dom, att ...
- 本地客户端(自己的电脑)连接远程Oracle数据库(服务器端),客户端安装步骤
如果本地自己的电脑没有安装Oracle(服务器端数据库),那就要单独安装HA-Instant Client-v11.2.0.3.0.exe(oracle_client客户端) 如果本地安装了Oracl ...