HDU 4280 Island Transport(dinic+当前弧优化)
Island Transport
Description
In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the ships. 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
The first line contains one integer T (1<=T<=20), the number of test cases. 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
For each test case, output an integer in one line, the transport capacity.
Sample Input
2
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
9
6
该题是最大流裸体,数据范围很大,可以用dinic但要注意数组要开的够大,且必须要当前弧优化,且最好不用STL(别用queue),方能把时间卡进去(反正我交了一次9400ms左右..)
因为是双向边, 相当于无向图,且题目保证无重边,可以不用每条边再加一个cap为0的残边,
不知把BFS写进主函数会不会节省些时间
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#define MIN(x,y) ((x)>(y))?(y):(x)
#define MAX(x,y) ((x)>(y))?(x):(y) using namespace std; const int inf = 0x3f3f3f3f;
const int ninf = 0xc0c0c0c0; //无穷小
const int vspot = ;
const int espot = ; struct Edges{
int to, next, cap;
}edges[vspot]; int V, E, cnt;
int S, T, ans;
int linjie[vspot], dist[vspot], cur[vspot];
int Q[espot]; void addEdges( int from, int to, int cap )
{
edges[cnt].to = to;
edges[cnt].cap = cap;
edges[cnt].next = linjie[from];
linjie[from] = cnt++; edges[cnt].to = from;
edges[cnt].cap = cap;
edges[cnt].next = linjie[to];
linjie[to] = cnt++; //直接加两条边就行
} int BFS()
{
memset( dist, -, sizeof(dist) );
int run, head = , rear = ; Q[head] = S;
dist[S] = ; while(head<rear)
{
run = Q[head++];
for( int i = linjie[run]; i+; i = edges[i].next )
{
int v = edges[i].to, flow = edges[i].cap;
if( dist[v] < && flow > )
{
dist[v] = dist[run] + ;
Q[rear++] = v;
}
}
} if( dist[T] > )
return ;
else
return ;
} int find( int s, int low )
{
int ff = ;
if( s == T )
return low;
for( int& i = cur[s]; i+; i = edges[i].next ) //注意int& i = cur[s] 当前弧优化
{
int v = edges[i].to, cap = edges[i].cap;
if( cap >
&& dist[v] == dist[s] +
&& (ff = find(v,MIN(cap,low))) )
{
edges[i].cap -= ff;
edges[i^].cap += ff;
return ff;
}
}
return ;
} void dinic()
{
ans = ;
int tans;
while(BFS())
{
for( int i = ; i <= V; i++ ) //当前弧优化
cur[i] = linjie[i];
while( tans = find(S,inf) )
ans += tans;
}
} int main()
{
int N;
cin >> N;
while( N-- )
{
scanf( "%d %d", &V, &E );
cnt = ;
memset( linjie, -, sizeof(linjie) ); int x, y, val, x2 = ninf, x1 = inf;
for( int i = ; i <= V; i++ )
{
scanf( "%d %d", &x, &y );
if( x < x1 )
{
x1 = x;
S = i;
} if( x > x2 )
{
x2 = x;
T = i;
}
} for( int i = ; i <= E; i++ )
{
scanf( "%d %d %d", &x, &y, &val );
addEdges(x,y,val);
}
/////////////////////////////////////////////////////////////////
dinic();
cout << ans << endl;
} return ;
}
HDU 4280 Island Transport(dinic+当前弧优化)的更多相关文章
- HDU 4280 Island Transport(网络流,最大流)
HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...
- HDU 4280 Island Transport
Island Transport Time Limit: 10000ms Memory Limit: 65536KB This problem will be judged on HDU. Origi ...
- HDU 4280 Island Transport(无向图最大流)
HDU 4280:http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意: 比较裸的最大流题目,就是这是个无向图,并且比较卡时间. 思路: 是这样的,由于是 ...
- Hdu 4280 Island Transport(最大流)
Island Transport Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 4280 Island Transport(网络流)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=4280">http://acm.hdu.edu.cn/showproblem.php ...
- HDU 4280 Island Transport(HLPP板子)题解
题意: 求最大流 思路: \(1e5\)条边,偷了一个超长的\(HLPP\)板子.复杂度\(n^2 \sqrt{m}\).但通常在随机情况下并没有isap快. 板子: template<clas ...
- ARC085E(最小割规划【最大流】,Dinic当前弧优化)
#include<bits/stdc++.h>using namespace std;typedef long long ll;const ll inf=0x3f3f3f3f;int cn ...
- 解题报告:hdu 3572 Task Schedule(当前弧优化Dinic算法)
Problem Description Our geometry princess XMM has stoped her study in computational geometry to conc ...
- Dinic当前弧优化 模板及教程
在阅读本文前,建议先自学最大流的Ek算法. 引入 Ek的核心是执行bfs,一旦找到增广路就停下来进行增广.换言之,执行一遍BFS执行一遍DFS,这使得效率大大降低.于是我们可以考虑优化. 核心思路 在 ...
随机推荐
- phpstorm中完成一键快速注释函数头
先保存函数,再在函数头写/**+enter就行了 /** * @param $num1 * @param $num2 * @param $opt * @return float|int */ func ...
- System.Web.Mvc.ViewResult.cs
ylbtech-System.Web.Mvc.ViewResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicK ...
- 我能不能理解成 ssh中service就相当于与jsp+servlet+dao中的servlet???
转文 首先解释面上意思,service是业务层,dao是数据访问层.(Data Access Objects) 数据访问对象 1.Dao其实一般没有这个类,这一般是指java中MVC架构中的model ...
- 史上最贵域名诞生!360斥资1700万美元买360.com
昨日,360公司官方人士向腾讯科技确认,公司已斥巨资收购国际顶级域名360.com.传闻这一收购价格为1700万美元,约合人民币1.1亿元. 史上最贵域名诞生!360斥资1700万美元买360.com ...
- ElasticSearch入门之花落红尘(三)
上篇文章散仙介绍了ElasticSearch的入门安装和使用,那么本篇我们来看下,如何使用java api来和ElasticSearch进行交互,简单点说,就是实现一个增删改查,来找找入门的感觉. 在 ...
- activiti 连线
实际使用中工作流往往不是一条直线进行下去,例如请假的话可以部门经理直接同意,或者报总经理同意,流程图示意如下. 可以看到,面对一个事情,重要的才要总经理审批否则仅部门经理审批即可.因此在完成任务的时候 ...
- Store工作原理
- const 有什么用途
可以定义const 常量:const可以修饰函数的参数.返回值,甚至函数的定义体.被const 修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性
- EPSG、SRID、WKT的概念
转自:http://www.cnblogs.com/jackdong/archive/2010/12/20/1911558.html EPSG:European Petroleum Survey Gr ...
- AppbarLayout的简单用法
在许多App中看到, toolbar有收缩和扩展的效果, 例如: appbar.gif 要实现这样的效果, 需要用到: CoordinatorLayout和AppbarLayout的配合, 以及实 ...