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 ...
随机推荐
- 3M隔音耳罩拆分、清洗教程
简介 3M隔音耳罩用起来真爽,十分适合喜欢安静的人.再者,被动和别人同住(学校.厂里宿舍.公司宿舍).公共场合时使用,效果刚刚的.题外话:有些人的素质……%¥%(*¥*! 3M隔音耳罩平时戴着也没觉得 ...
- 开普勒云平台:如何配置gitlab与Jenkins
一.Kplcloud是什么? kplcloud是一个基于Kubernetes的轻量级PaaS平台,通过可视化的界面对应用进行管理,降低应用容器化的对度,从而减少应用容器化的时间成本. Kplcloud ...
- java集合第一节,List简单介绍
Java中List集合的常用方法 List接口是继承Collection接口,所以Collection集合中有的方法,List集合也继承过来. package 集合; import java.ut ...
- 百万年薪python之路 -- 递归
递归(每当有一个函数被递归调用,就应该要有一个返回值,才能正常把递归的返回值'归'回来) 一个正经的递归: 1.不断调用自己本身 2.有明确的结束条件 递归注重于"一递 一归&quo ...
- dubbo初学采坑记
写在前面的话 dubbo 现在是apache组织旗下的项目,相信国内也有很多人使用.最近一个同事离职,我就接手了他的项目.远程通讯就是用的dubbo框架来实现的.使用Intelij idea 写了一个 ...
- CheckBox状态多选
前: <StackPanel Margin="> <Label FontWeight="Bold">Application Options< ...
- 将windows项目移植到linux上
提要:由于项目使用java开发,移植中没有什么编译问题(移植很快,但小问题多) 1.移植过程中遇到的问题: (1).由于项目中使用了 1024以下的端口号,导致网络通信一直出错 原因:因为Linux要 ...
- Java基础(二十九)Java IO(6)对象序列化(Object Serialization)
参考之前整理过的Java序列化与反序列化:https://www.cnblogs.com/BigJunOba/p/9127414.html 使用对象输入输出流可以实现对象序列化与反序列化,可以直接存取 ...
- 如何上传项目至GitHub
1.下载 https://gitforwindows.org/ 2.打开Git Bash 把git绑定到GitHub 3.打开GitHub登陆后 点击settings 点击SSH and GPG ke ...
- 如何在Spring Boot中使用Cookies
一. 导读 本文大纲 读取HTTP Cookie 设置HTTP Cookie 读取所有Cookie[] 为Cookie设置过期时间 Https与Cookie HttpOnly Cookie 删除Coo ...