题意:

求最大流

思路:

\(1e5\)条边,偷了一个超长的\(HLPP\)板子。复杂度\(n^2 \sqrt{m}\)。但通常在随机情况下并没有isap快。

板子:

template<class T = int>
struct HLPP{
const int MAXN = 1e5 + 5;
const T INF = 0x3f3f3f3f;
struct edge{
int to, rev;
T f;
};
vector<edge> adj[maxn];
deque<int> lst[maxn];
vector<int> gap[maxn];
T excess[maxn];
int highest, height[maxn], cnt[maxn], ptr[maxn], work, N; void addEdge(int u, int v, int f, bool isdirected = true){
adj[u].push_back({v, adj[v].size(), f});
adj[v].push_back({u, adj[u].size() - 1, isdirected? 0 : f});
} void clear(int n){
N = n;
for(int i = 0; i <= n; i++){
adj[i].clear(), lst[i].clear();
gap[i].clear();
}
} void upHeight(int v, int nh){
++work;
if(height[v] != N) --cnt[height[v]];
height[v] = nh;
if(nh == N) return;
cnt[nh]++, highest = nh;
gap[nh].push_back(v);
if(excess[v] > 0){
lst[nh].push_back(v);
++ptr[nh];
}
} void glovalRelabel(int s, int t){
work = 0;
fill(height, height + N + 1, N);
fill(cnt, cnt + N + 1, 0);
for(int i = 0; i <= highest; i++){
lst[i].clear();
gap[i].clear();
ptr[i] = 0;
}
height[t] = 0;
queue<int> q({t});
while(!q.empty()){
int v = q.front();
q.pop();
for(auto &e : adj[v])
if(height[e.to] == N && adj[e.to][e.rev].f > 0){
q.push(e.to);
upHeight(e.to, height[v] + 1);
}
highest = height[v];
}
}
void push(int v, edge& e){
if(excess[e.to] == 0){
lst[height[e.to]].push_back(e.to);
++ptr[height[e.to]];
}
T df = min(excess[v], e.f);
e.f -= df;
adj[e.to][e.rev].f += df;
excess[v] -= df;
excess[e.to] += df;
}
void discharge(int v){
int nh = N;
for(auto &e : adj[v]){
if(e.f > 0){
if(height[v] == height[e.to] + 1){
push(v, e);
if(excess[v] <= 0) return;
}
else{
nh = min(nh, height[e.to] + 1);
}
}
}
if(cnt[height[v]] > 1){
upHeight(v, nh);
}
else{
for(int i = height[v]; i < N; i++){
for(auto j : gap[i]) upHeight(j, N);
gap[i].clear(); ptr[i] = 0;
}
}
} T hlpp(int s, int t){
fill(excess, excess + N + 1, 0);
excess[s] = INF, excess[t] = -INF;
glovalRelabel(s, t);
for(auto &e : adj[s]) push(s, e);
for(; highest >= 0; -- highest){
while(lst[highest].size()){
int v = lst[highest].back();
lst[highest].pop_back();
discharge(v);
if(work > 4 * N) glovalRelabel(s, t);
}
}
return excess[t] + INF;
} }; int read() {
int f = 1, x = 0; char ch = getchar();
while(! isdigit(ch)) {if(ch == '-' ) f = -f; ch = getchar();}
while(isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
} HLPP<int> hlpp; //hlpp.clear(n)

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <queue>
#define isdigit(a) (a>='0' && a<='9')
typedef long long LL;
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5+5; template<class T = int>
struct HLPP{
const int MAXN = 1e5 + 5;
const T INF = 0x3f3f3f3f;
struct edge{
int to, rev;
T f;
};
vector<edge> adj[maxn];
deque<int> lst[maxn];
vector<int> gap[maxn];
T excess[maxn];
int highest, height[maxn], cnt[maxn], ptr[maxn], work, N; void addEdge(int u, int v, int f, bool isdirected = true){
adj[u].push_back({v, adj[v].size(), f});
adj[v].push_back({u, adj[u].size() - 1, isdirected? 0 : f});
} void clear(int n){
N = n;
for(int i = 0; i <= n; i++){
adj[i].clear(), lst[i].clear();
gap[i].clear();
}
} void upHeight(int v, int nh){
++work;
if(height[v] != N) --cnt[height[v]];
height[v] = nh;
if(nh == N) return;
cnt[nh]++, highest = nh;
gap[nh].push_back(v);
if(excess[v] > 0){
lst[nh].push_back(v);
++ptr[nh];
}
} void glovalRelabel(int s, int t){
work = 0;
fill(height, height + N + 1, N);
fill(cnt, cnt + N + 1, 0);
for(int i = 0; i <= highest; i++){
lst[i].clear();
gap[i].clear();
ptr[i] = 0;
}
height[t] = 0;
queue<int> q({t});
while(!q.empty()){
int v = q.front();
q.pop();
for(auto &e : adj[v])
if(height[e.to] == N && adj[e.to][e.rev].f > 0){
q.push(e.to);
upHeight(e.to, height[v] + 1);
}
highest = height[v];
}
}
void push(int v, edge& e){
if(excess[e.to] == 0){
lst[height[e.to]].push_back(e.to);
++ptr[height[e.to]];
}
T df = min(excess[v], e.f);
e.f -= df;
adj[e.to][e.rev].f += df;
excess[v] -= df;
excess[e.to] += df;
}
void discharge(int v){
int nh = N;
for(auto &e : adj[v]){
if(e.f > 0){
if(height[v] == height[e.to] + 1){
push(v, e);
if(excess[v] <= 0) return;
}
else{
nh = min(nh, height[e.to] + 1);
}
}
}
if(cnt[height[v]] > 1){
upHeight(v, nh);
}
else{
for(int i = height[v]; i < N; i++){
for(auto j : gap[i]) upHeight(j, N);
gap[i].clear(); ptr[i] = 0;
}
}
} T hlpp(int s, int t){
fill(excess, excess + N + 1, 0);
excess[s] = INF, excess[t] = -INF;
glovalRelabel(s, t);
for(auto &e : adj[s]) push(s, e);
for(; highest >= 0; -- highest){
while(lst[highest].size()){
int v = lst[highest].back();
lst[highest].pop_back();
discharge(v);
if(work > 4 * N) glovalRelabel(s, t);
}
}
return excess[t] + INF;
} }; int read() {
int f = 1, x = 0; char ch = getchar();
while(! isdigit(ch)) {if(ch == '-' ) f = -f; ch = getchar();}
while(isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
} HLPP<int> hlpp; int main() {
int T;
scanf("%d", &T);
while(T--) {
int n, m;
scanf("%d%d", &n, &m);
int x, y, s, t;
int startX = inf, endX = -inf;
for(int i = 1; i <= n; ++i) {
x = read(), y = read();
if(x < startX) s = i, startX = x;
if(x > endX) t = i, endX = x;
}
int u, v, f;
hlpp.clear(n);
for(int i = 1; i <= m; ++i) {
u = read(), v = read(), f = read();
hlpp.addEdge(u, v, f, false);
} printf("%d\n", hlpp.hlpp(s, t));
}
return 0;
}

HDU 4280 Island Transport(HLPP板子)题解的更多相关文章

  1. HDU 4280 Island Transport(网络流,最大流)

    HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...

  2. HDU 4280 Island Transport

    Island Transport Time Limit: 10000ms Memory Limit: 65536KB This problem will be judged on HDU. Origi ...

  3. Hdu 4280 Island Transport(最大流)

    Island Transport Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. HDU 4280 Island Transport(无向图最大流)

    HDU 4280:http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意: 比较裸的最大流题目,就是这是个无向图,并且比较卡时间. 思路: 是这样的,由于是 ...

  5. HDU 4280 Island Transport(dinic+当前弧优化)

    Island Transport Description In the vast waters far far away, there are many islands. People are liv ...

  6. HDU 4280 Island Transport(网络流)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=4280">http://acm.hdu.edu.cn/showproblem.php ...

  7. 【HDUOJ】4280 Island Transport

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:有n个岛屿,m条无向路,每个路给出最大允许的客流量,求从最西的那个岛屿最多能运用多少乘客到 ...

  8. HDU 1385 Minimum Transport Cost 最短路径题解

    本题就是使用Floyd算法求全部路径的最短路径,并且须要保存路径,并且更进一步须要依照字典顺序输出结果. 还是有一定难度的. Floyd有一种非常巧妙的记录数据的方法,大多都是使用这种方法记录数据的. ...

  9. HDU4280:Island Transport(最大流)

    Island Transport Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

随机推荐

  1. 入门OJ:八中生成树2

    题目描述 八中里面有N个建设物,M条边.对于这种要建最小生成树的问题,你应该很熟练了.现在老大决定降低某条边的费用,然后这条边必须要被选中,因为这条路他每天都要走,自然......问选了这条边后是否可 ...

  2. [Usaco2007 Feb]Cow Party

    题目描述 农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X牛棚举行的奶牛派对.共有M(1≤M≤100000)条单向路连接着牛棚,第i条踣需要Ti的时间来通过.牛们都很懒,所以不管是前 ...

  3. uni-app开发经验分享二十: 微信小程序 授权登录 获取详细信息 获取手机号

    授权页面 因为微信小程序提供的 权限弹窗 只能通用户确认授权 所以可以 写一个授权页面,让用户点击 来获取用户相关信息 然后再配合后台就可以完成登录 <button class="bt ...

  4. ubuntu 更改U盘设备分区/dev/sdb4 标识

    备份u盘分区表 代码: sudo sfdisk -d /dev/sdb > sdb_table 修改sdb_table文件 代码: gedit sdb_table 恢复u盘分区表 代码: sud ...

  5. 阿里巴巴微服务与配置中心技术实践之道 配置推送 ConfigurationManagement ConfigDrivenAnyting

    阿里巴巴微服务与配置中心技术实践之道 原创: 坤宇 InfoQ 2018-02-08 在面向分布式的微服务系统中,如何通过更高效的配置管理方式,帮助微服务系统架构持续"无痛"的演进 ...

  6. vue-router实现路由懒加载( 动态加载路由 )

    三种方式第一种:vue异步组件技术 ==== 异步加载,vue-router配置路由 , 使用vue的异步组件技术 , 可以实现按需加载 .但是,这种情况下一个组件生成一个js文件.第二种:路由懒加载 ...

  7. Python程序中#-*-coding: UTF-8 -*-的作用

    1.通常我们在pycharm中写程序的时候会加上#-*coding: UTF-8 -*- 如: #!/usr/bin/env python3#-*-coding: UTF-8 -*-#Author x ...

  8. LOJ10066

    LOJ10066 新的开始 题目描述 发展采矿业当然首先得有矿井,小 F 花了上次探险获得的千分之一的财富请人在岛上挖了 n 口矿井,但他似乎忘记考虑的矿井供电问题-- 为了保证电力的供应,小 F 想 ...

  9. I - I(Highways)

    N个点,给你N个点的坐标,现在还有Q条边已经连接好了.问把N个点怎么连接起来的花费的距离最短? The island nation of Flatopia is perfectly flat. Unf ...

  10. 2019牛客暑期多校训练营(第六场)D-Move

    >传送门< 题意: 你有n件行李,有k个箱子体积相同的箱子,遵循下面的规则将行李放进箱子里面 每次都取当前最大的可以放进箱子的行李放进箱子,如果该箱子放不进任何行李那么就换一个新的箱子再按 ...