Solution -「Gym 102759F」Interval Graph
\(\mathcal{Description}\)
Link.
给定 \(n\) 个区间,第 \(i\) 个为 \([l_i,r_i]\),有权值 \(w_i\)。设一无向图 \(G=(V=\{1,2,\dots,n\},E)\),\((u,v)\in E\Leftrightarrow [l_u,r_u]\cap[l_v,r_v]\not=\varnothing\),求删除若干区间使得 \(G\) 无环的被删除区间权值和的最小值。
\(n\le2.5\times10^5\)。
\(\mathcal{Solution}\)
不要学了 DP 就只想 DP,优化不来麻烦推翻重来。
首先 \(G\) 的合法条件等价于不存在三区间交于一点,这是一个经典费用流流模型,建图方法略。
当然直接 Dinic 啥的直接挂掉,考虑到只需要增广两次,可以使用势能 Dijkstra + EK 算法求最小费用最大流。概括上来说,令 \(u\) 的势能 \(h_u\) 为累加的 \(d_u\) 之和,使得此时图上 \(w(u,v)+h_u-h_v\) 非负,就能跑 Dijkstra 了。本题只用增广两次,所以在初始 DAG 上求出 \(h\) 后甚至不必更新。
具体讲解:OneInDark %%%.
\(\mathcal{Code}\)
/*~Rainybunny~*/
#include <queue>
#include <cstdio>
#include <iostream>
#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i )
typedef long long LL;
typedef std::pair<LL, int> PLI;
#define fi first
#define se second
template<typename Tp>
inline Tp tmin( const Tp& a, const Tp& b ) { return a < b ? a : b; }
const int MAXN = 2.5e5, MAXV = 5e5;
const LL LINF = 1ll << 60;
int n, ecnt = 1, mxp, head[MAXV + 5];
struct Edge { int to, flw; LL cst; int nxt; } graph[( MAXV + MAXN ) * 2 + 5];
LL hgt[MAXV + 5], dis[MAXV + 5];
int pre[MAXV + 5], flw[MAXV + 5];
inline void link( const int s, const int t, const int f, const LL c ) {
graph[++ecnt] = { t, f, c, head[s] }, head[s] = ecnt;
graph[++ecnt] = { s, 0, -c, head[t] }, head[t] = ecnt;
}
inline void getHeight() {
hgt[0] = 0;
rep ( u, 0, mxp - 1 ) {
for ( int i = head[u]; i; i = graph[i].nxt ) if ( graph[i].flw ) {
hgt[graph[i].to] = tmin( hgt[graph[i].to], hgt[u] + graph[i].cst );
}
}
}
inline bool dijkstra() {
static bool vis[MAXV + 5];
static std::priority_queue<PLI, std::vector<PLI>, std::greater<PLI> > heap;
rep ( i, 0, mxp ) pre[i] = flw[i] = 0, dis[i] = LINF, vis[i] = false;
heap.push( { dis[0] = 0, 0 } ), flw[0] = 2;
while ( !heap.empty() ) {
PLI p( heap.top() ); heap.pop();
if ( vis[p.se] ) continue;
vis[p.se] = true;
for ( int i = head[p.se], v; i; i = graph[i].nxt ) {
LL d = p.fi + graph[i].cst + hgt[p.se] - hgt[v = graph[i].to];
if ( graph[i].flw && dis[v] > d ) {
heap.push( { dis[v] = d, v } );
pre[v] = i, flw[v] = tmin( flw[p.se], graph[i].flw );
}
}
}
return dis[mxp] != LINF;
}
int main() {
std::ios::sync_with_stdio( false ), std::cin.tie( 0 );
std::cin >> n;
rep ( i, 1, n ) {
int s, e; LL w; std::cin >> s >> e >> w, ++e;
link( s, e, 1, -w ), mxp = mxp < e ? e : mxp;
}
rep ( i, 0, mxp - 1 ) link( i, i + 1, 2, 0 );
getHeight();
LL ans = 0;
while ( dijkstra() ) {
for ( int u = mxp; u; u = graph[pre[u] ^ 1].to ) {
graph[pre[u]].flw -= flw[mxp], graph[pre[u] ^ 1].flw += flw[mxp];
}
ans += ( dis[mxp] + hgt[mxp] - hgt[0] ) * flw[mxp];
}
std::cout << -ans << '\n';
return 0;
}
Solution -「Gym 102759F」Interval Graph的更多相关文章
- Solution -「Gym 102956F」Find the XOR
\(\mathcal{Description}\) Link. 给定 \(n\) 个点 \(m\) 条边的连通无向图 \(G\),边有边权.其中 \(u,v\) 的距离 \(d(u,v)\) ...
- Solution -「Gym 102759I」Query On A Tree 17
\(\mathcal{Description}\) Link. 给定一棵含 \(n\) 个结点的树,结点 \(1\) 为根,点 \(u\) 初始有点权 \(a_u=0\),维护 \(q\) 次 ...
- Solution -「Gym 102979E」Expected Distance
\(\mathcal{Description}\) Link. 用给定的 \(\{a_{n-1}\},\{c_n\}\) 生成一棵含有 \(n\) 个点的树,其中 \(u\) 连向 \([1, ...
- Solution -「Gym 102979L」 Lights On The Road
\(\mathcal{Description}\) Link. 给定序列 \(\{w_n\}\),选择 \(i\) 位置的代价为 \(w_i\),要求每个位置要不被选择,要不左右两个位置至少被 ...
- Solution -「Gym 102956B」Beautiful Sequence Unraveling
\(\mathcal{Description}\) Link. 求长度为 \(n\),值域为 \([1,m]\) 的整数序列 \(\lang a_n\rang\) 的个数,满足 \(\not\ ...
- Solution -「Gym 102956F」Border Similarity Undertaking
\(\mathcal{Description}\) Link. 给定一张 \(n\times m\) 的表格,每个格子上写有一个小写字母.求其中长宽至少为 \(2\),且边界格子上字母相同的矩 ...
- Solution -「Gym 102956A」Belarusian State University
\(\mathcal{Description}\) Link. 给定两个不超过 \(2^n-1\) 次的多项式 \(A,B\),对于第 \(i\in[0,n)\) 个二进制位,定义任意一个二元 ...
- Solution -「Gym 102798I」Sean the Cuber
\(\mathcal{Description}\) Link. 给定两个可还原的二阶魔方,求从其中一个状态拧到另一个状态的最小步数. 数据组数 \(T\le2.5\times10^5\). ...
- Solution -「Gym 102798K」Tree Tweaking
\(\mathcal{Description}\) Link. 给定排列 \(\{p_n\}\),求任意重排 \(p_{l..r}\) 的元素后,将 \(\{p_n\}\) 依次插入二叉搜索树 ...
随机推荐
- 总结 sql 的 并集、交集、差集
有两个表 ,表a ,表b , create table a { age int , name varchar(20) } ending=innodb; insert into a values(13 ...
- java 字符串 大小写转换 、去掉首末端空格 、根据索引切割字符 、判断是否含有某连续字符串
1. 字符串转大写: toUpperCase() 字符串转小写: toLowerCase() @Test public void tt(){ String d = "sdgGHJGjghGH ...
- SpringBoot学习笔记二之Spring整合Mybatis
原文链接: https://www.toutiao.com/i6803235766274097678/ 在learn-admin-component子工程中加入搭建环境所需要的具体依赖(因为比较长配置 ...
- 在 python 项目中如何记录日志
一. 概述 写本文的目的是我在写 python 项目的时候需要记录日志,我忘记怎么处理了,每次都需要去网上查一遍,好记性不如烂笔头, 这里把查阅的内容记录下来,方便以后查找. python 项目中记录 ...
- 备忘录——基于rdlc报表实现打印产品标签
目录 0. 背景说明 1. 条形码生成 2. 获取产品的小程序码 3. 报表设计器设计标签模版 3.1 为WinForm控件工具箱添加ReportViewer控件 3.2 为VS2019安装RDLC报 ...
- leetcode 83. 删除排序链表中的重复元素 及 82. 删除排序链表中的重复元素 II
83. 删除排序链表中的重复元素 问题描述 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: 输入: ...
- golang中匿名函数的应用-回调函数-闭包
package main import ( "fmt" "strconv" ) type funcType func(int, int) int // 自定义函 ...
- 深入了解promise
1. Promise基础 什么是回调地狱? 当使用回调函数来进行事件处理的时候,如果嵌套多层回调函数的时候,就会出现回调地狱,例如: method1(function(err, result) { i ...
- .Net Api 之如何使用Elasticsearch存储文档
.Net Api 之如何使用Elasticsearch存储文档 什么是Elasticsearch? Elasticsearch 是一个分布式.高扩展.高实时的搜索与数据分析引擎.它能很方便的使大量数据 ...
- 第06讲:Flink 集群安装部署和 HA 配置
Flink系列文章 第01讲:Flink 的应用场景和架构模型 第02讲:Flink 入门程序 WordCount 和 SQL 实现 第03讲:Flink 的编程模型与其他框架比较 第04讲:Flin ...