HDU4280(KB11-G 最大流)
Island Transport
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 9151 Accepted Submission(s): 2958
Problem Description
You have a transportation company there. Some routes are opened for passengers. Each route is a straight line connecting two different islands, and it is bidirectional. Within an hour, a route can transport a certain number of passengers in one direction. For safety, no two routes are cross or overlap and no routes will pass an island except the departing island and the arriving island. Each island can be treated as a point on the XY plane coordinate system. X coordinate increase from west to east, and Y coordinate increase from south to north.
The transport capacity is important to you. Suppose many passengers depart from the westernmost island and would like to arrive at the easternmost island, the maximum number of passengers arrive at the latter within every hour is the transport capacity. Please calculate it.
Input
Then T test cases follow. The first line of each test case contains two integers N and M (2<=N,M<=100000), the number of islands and the number of routes. Islands are number from 1 to N.
Then N lines follow. Each line contain two integers, the X and Y coordinate of an island. The K-th line in the N lines describes the island K. The absolute values of all the coordinates are no more than 100000.
Then M lines follow. Each line contains three integers I1, I2 (1<=I1,I2<=N) and C (1<=C<=10000) . It means there is a route connecting island I1 and island I2, and it can transport C passengers in one direction within an hour.
It is guaranteed that the routes obey the rules described above. There is only one island is westernmost and only one island is easternmost. No two islands would have the same coordinates. Each island can go to any other island by the routes.
Output
Sample Input
5 7
3 3
3 0
3 1
0 0
4 5
1 3 3
2 3 4
2 4 3
1 5 6
4 5 3
1 4 4
3 4 2
6 7
-1 -1
0 1
0 2
1 0
1 1
2 3
1 2 1
2 3 6
4 5 5
5 6 3
1 4 6
2 5 5
3 6 4
Sample Output
6
Source
//2017-08-24
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int N = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int next, to, w;
}edge[N<<]; void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} struct Dinic{
int level[N], S, T;
void init(int _S, int _T){
S = _S;
T = _T;
tot = ;
memset(head, -, sizeof(head));
}
bool bfs(){
queue<int> que;
memset(level, -, sizeof(level));
level[S] = ;
que.push(S);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(level[v] == - && w > ){
level[v] = level[u]+;
que.push(v);
}
}
}
return level[T] != -;
}
int dfs(int u, int flow){
if(u == T)return flow;
int ans = , fw;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to, w = edge[i].w;
if(!w || level[v] != level[u]+)
continue;
fw = dfs(v, min(flow-ans, w));
ans += fw;
edge[i].w -= fw;
edge[i^].w += fw;
if(ans == flow)return ans;
}
if(ans == )level[u] = -;
return ans;
}
int maxflow(){
int flow = , tmp;
while(bfs())
while((tmp = dfs(S, INF)) > )
flow += tmp;
return flow;
}
}dinic; int main()
{
//std::ios::sync_with_stdio(false);
//freopen("inputG.txt", "r", stdin);
int T, n, m;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
int x, y, s = , t = , mininum = INF, maxinum = -INF;
for(int i = ; i <= n; i++){
scanf("%d%d", &x, &y);
if(x < mininum){
mininum = x;
s = i;
}
if(x > maxinum){
maxinum = x;
t = i;
}
}
dinic.init(s, t);
int u, v, w;
for(int i = ; i < m; i++){
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, w);
add_edge(v, u, w);
}
printf("%d\n", dinic.maxflow());
}
return ;
}
HDU4280(KB11-G 最大流)的更多相关文章
- HDU4280 Island Transport —— 最大流 ISAP算法
题目链接:https://vjudge.net/problem/HDU-4280 Island Transport Time Limit: 20000/10000 MS (Java/Others) ...
- hdu4280 Island Transport 最大流
In the vast waters far far away, there are many islands. People are living on the islands, and all t ...
- Ford-Fulkerson 最大流算法
流网络(Flow Networks)指的是一个有向图 G = (V, E),其中每条边 (u, v) ∈ E 均有一非负容量 c(u, v) ≥ 0.如果 (u, v) ∉ E 则可以规定 c(u, ...
- 关于最大流的EdmondsKarp算法详解
最近大三学生让我去讲课,我就恶补了最大流算法,笔者认为最重要的是让学弟学妹们入门,知道算法怎么来的?为什么是这样?理解的话提出自己的改进,然后再看看Dinic.SAP和ISAP算法….. 一.概念引入 ...
- [数据结构]最大流之Ford-Fulkerson算法
本文主要讲解最大流问题的Ford-Fulkerson解法.可以说这是一种方法,而不是算法,因为它包含具有不同运行时间的几种实现.该方法依赖于三种重要思想:残留网络,增广路径和割. 在介绍着三种概念之前 ...
- Java学习 · 初识 IO流
IO流 1. 原理与概念 a) 流 i. 流动,流向 ii. 从一端移动到另一端 源头到目的地 iii. 抽象.动态概念,是一连 ...
- Java8 新特性 Stream() 创建流
通过Controllere类的Stream()和parallelStream()创建流 //通过集合创建流 @Test public void test1() { String arr[] = new ...
- sh2.sed脚本练习
1,删除/etc/grub.conf文件中行首的空白字符: sed -r 's@^[[ :spapce: ]] +@@g' /etc/grub.conf 2.替换/etc/inittab 文件中&qu ...
- hiho一下,第115周,FF,EK,DINIC
题目1 : 网络流一·Ford-Fulkerson算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho住在P市,P市是一个很大很大的城市,所以也面临着一个 ...
- 最大流问题Ford-Fulkerson方法(转)
本篇主要讲解最大流问题的Ford-Fulkerson解法.可是说这是一种方法,而不是算法,因为它包含具有不同运行时间的几种实现.该方法依赖于三种重要思想:残留网络,增广路径和割.本文将会详细介绍这些内 ...
随机推荐
- 试着用java实现DNS(一)——DatagramSocket, DatagramPacket, Message
一般来说,自己编写DNS是没有必要的,目前开源的dns服务软件很多,功能也很强大.但是,有时候又是很有必要的,有着诸多好处.比如说,用于企业内网,简化DNS配置,可以根据企业需求添加新的功能,非常灵活 ...
- 详述MSSQL服务在渗透测试中的利用 (下篇)
part3 MSSQL写文件 步骤1 sp_makewebtask写文件 因为是`SA`权限,如果目标服务器是web服务器,我们也不用去备份了,可以直接写个一句话木马进去到web目录. 在不知道web ...
- updateByPrimaryKey 和 updateByPrimaryKeySelective
1. 数据库记录 2. updateByPrimaryKey Preparing: UPDATE t_token_info SET entity_id = ?,entity_type = ?,time ...
- python iter函数用法
iter函数用法简述 Python 3中关于iter(object[, sentinel)]方法有两个参数. 使用iter(object)这种形式比较常见. iter(object, sentinel ...
- 【并发】3、LockSupport阻塞与唤醒,相较与wait和notify
我们可以使用wait和notify分别对象线程进行阻塞或者唤醒,但是我们也可以使用LockSupport实现一样的功能,并且在实际使用的时候,个人感觉LockSupport会更加顺手 范例1,wait ...
- 改变IIS的Framework版本
Changing the Framework version requires a restart of the W3SVC service. Alternatively, you can chang ...
- Redis笔记(2)单机数据库实现
1.前言 上节总结了一下redis的数据结构和对象构成,本章介绍redis数据库一个基本面貌,是如何设计的. 2.数据库 服务器结构redisServer: redisDb *db: 一个数组,保存服 ...
- Install nginx on centos
1. 安装wget. sudo yum -y install wget 2. 下载nginx发布代码. sudo wget http://nginx.org/download/nginx-1.8.0. ...
- 使用 IntelliJ IDEA 导入 Spark 最新源码及编译 Spark 源代码(博主强烈推荐)
前言 其实啊,无论你是初学者还是具备了有一定spark编程经验,都需要对spark源码足够重视起来. 本人,肺腑之己见,想要成为大数据的大牛和顶尖专家,多结合源码和操练编程. 准备工作 1.sca ...
- 深度理解C# 的执行原理
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由鹅厂优文发表于云+社区专栏 从编译原理说起 虚拟机是什么 C# 是什么,IL 又是什么 .Net Framework vs Mono ...