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 ...
- 图书-技术-SpringBoot:《Spring Boot 企业级应用开发实战》
ylbtech-图书-技术-SpringBoot:<Spring Boot 企业级应用开发实战> Spring Boot 企业级应用开发实战,全书围绕如何整合以 Spring Boot 为 ...
- 服务器迁移部署OmsEdi
基本配置 绑定 高级设置
- MyBatis - sqlMapConfig.xml主配置文件
SqlMapConfig.xml配置文件的内容和配置顺序如下 ① properties(读取配置文件):定义配置,配置的属性可以在整个配置文件中其他位置进行引用: ② settings(全局配置参数) ...
- python 将值相同的key分组的方法
方法一: 使用 itertools.groupby() rows = [ {'address': '5412 N CLARK ', 'date ': '07/12/2012 ’ }, {'addres ...
- C开发系列-指针
指针 通过一段简单的程序,引入指针的概念 #include <iostream> using namespace std; // changeValue函数的定义 void changeV ...
- 基于在树上走的DP问题
笔者已经很久没有打过题解了,如果打题解,就总是要连着一个知识点来打题解. 最近做过一共两道这样的题目.笔者认为这样的题有较强的可拓展性,比较有意义. 所以就打一篇博客. 问题概述 先说说这是个什么样的 ...
- springcloud之配置中心用法
一.配置文件服务器server端 1.构建server端所需jar <dependencies> <dependency> <groupId>org.springf ...
- Django项目:CRM(客户关系管理系统)--67--57PerfectCRM实现admin批量生成上课记录
#admin.py # ————————01PerfectCRM基本配置ADMIN———————— from django.contrib import admin # Register your m ...
- [洛谷]P1505 [国家集训队]旅游
题目链接: 传送门 题目分析: 树剖板,支持单点修改,区间取反,区间求最大值/最小值/和 区间取反取两次等于没取,维护一个\(rev\ tag\),每次打标记用\(xor\)打,记录是否需要翻转,\( ...