HDU5669-Road
题意
给一个\(n\)个点的图,标号为\(1\)到\(n\),进行\(m\)次连边\((a,b,c,d,w)\):
for i in range[a,b]:
for j in range[c,d]:
add_edge(i,j,w)
有\(K\)次机会可以消除一条边的权值(即走过但不算),问\(1\)到\(n\)的最短路。
\(n\le 5\times 10^4,m\le 10^4,0\le K\le 10,w\le 10^3\)
分析
\(K\)次消除机会用一个类似dp的东西处理(也被叫做分层图最短路),\(d[i][j]\)表示从1走到\(i\)点用了\(j\)次机会的最短路,显然这个东西是可以dp的。现在我们只需要考虑如何求最短路。
这个连边方式是区间对区间连同一种边,跟bzoj3218的可持久化线段树维护网络流的连边有点像。考虑线段树。
一个区间可以表示为线段树上的最多\(\log n\)个节点。我们一开始给线段树编号,用它来维护连边不就好啦!
一开始是这样想的,但其实只开一颗线段树是不对的,因为无法上下走,于是取看题解。
我们有两颗线段树,一颗连出去,一颗连入,分别称为出线段树和入线段树。出线段树的子节点连到父亲,入线段树的父亲连到子节点。然而如果只是这样的话,我们只能走一条边——从入线段树的某个点走到出线段树的点之后就回不来了!
所以我们把入线段树的每个点连到出线段树的相同位置的点即可。
每次连边是否需要\(\log ^2n\)条边呢?其实不需要,我们可以对每次连边建一个中间节点,出入线段树分别连出,从这个点连入即可。
这样总点数是\(4n+m\),总边数最大为\(2m\log n+6n\),所以使用优先队列优化Dijkstra求最短路,总复杂度为\(O(Km\log ^2n)\) 。
这题学习了两个线段树维护出入以及中间节点减少边数的方法(可能还有分层图最短路吧)。
代码
#include<cstdio>
#include<cctype>
#include<cstring>
#include<queue>
#include<utility>
#include<algorithm>
using namespace std;
int read() {
int x=0,f=1;
char c=getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=5e4+1;
const int maxm=1e4+1;
const int maxp=4e5+1;
const int maxe=2e6+1;
const int maxk=11;
int n,m,k,ids=0,d[maxk][maxp],inf;
typedef pair<int,pair<int,int> > data;
inline int Min(int &x,int y) {x=min(x,y);}
priority_queue<data,vector<data>,greater<data> > q;
struct Graph {
struct edge {
int v,w,nxt;
} e[maxe];
int h[maxp],tot;
Graph ():tot(0) {}
void add(int u,int v,int w) {
e[++tot]=(edge){v,w,h[u]};
h[u]=tot;
}
void dj(int S,int T) {
d[0][S]=0;
q.push(make_pair(0,make_pair(S,0)));
while (!q.empty()) {
data dat=q.top();
q.pop();
int x=dat.second.first,tim=dat.second.second;
for (int i=h[x],v=e[i].v;i;i=e[i].nxt,v=e[i].v) {
if (tim<k && d[tim+1][v]>d[tim][x]) {
d[tim+1][v]=d[tim][x];
q.push(make_pair(d[tim+1][v],make_pair(v,tim+1)));
}
if (d[tim][v]>d[tim][x]+e[i].w) {
d[tim][v]=d[tim][x]+e[i].w;
q.push(make_pair(d[tim][v],make_pair(v,tim)));
}
}
}
}
} G;
struct SGT {
int id[maxn<<2];
void build(int x,int l,int r,bool op) {
id[x]=++ids;
if (l==r) return;
int mid=(l+r)>>1;
build(x<<1,l,mid,op),build(x<<1|1,mid+1,r,op);
op?(G.add(id[x],id[x<<1],0),G.add(id[x],id[x<<1|1],0)):(G.add(id[x<<1],id[x],0),G.add(id[x<<1|1],id[x],0));
}
void link(int x,int L,int R,int l,int r,int p,int w,bool op) {
if (L==l && R==r) {
op?G.add(p,id[x],w):G.add(id[x],p,w);
return;
}
int mid=(L+R)>>1;
if (r<=mid) link(x<<1,L,mid,l,r,p,w,op); else
if (l>mid) link(x<<1|1,mid+1,R,l,r,p,w,op); else
link(x<<1,L,mid,l,mid,p,w,op),link(x<<1|1,mid+1,R,mid+1,r,p,w,op);
}
int ID(int x,int l,int r,int p) {
if (l==r) return id[x];
int mid=(l+r)>>1;
return p<=mid?ID(x<<1,l,mid,p):ID(x<<1|1,mid+1,r,p);
}
} a,b;
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif
read(),n=read(),m=read(),k=read();
a.build(1,1,n,false);
int tmp=ids;
b.build(1,1,n,true);
for (int i=1;i<=tmp;++i) G.add(tmp+i,i,0);
while (m--) {
int l1=read(),r1=read(),l2=read(),r2=read(),w=read();
++ids;
a.link(1,1,n,l1,r1,ids,w,false);
b.link(1,1,n,l2,r2,ids,0,true);
++ids;
a.link(1,1,n,l2,r2,ids,w,false);
b.link(1,1,n,l1,r1,ids,0,true);
}
memset(d,0x3f,sizeof d),inf=d[0][0];
int S=a.ID(1,1,n,1),T=b.ID(1,1,n,n);
G.dj(S,T); // d[S][0]=0
int ans=inf;
for (int i=0;i<=k;++i) Min(ans,d[i][T]);
ans==inf?puts("Yww is our red sun!"):printf("%d\n",ans);
return 0;
}
HDU5669-Road的更多相关文章
- HDU5669 Road 分层最短路+线段树建图
分析:(官方题解) 首先考虑暴力,显然可以直接每次O(n^2) 的连边,最后跑一次分层图最短路就行了. 然后我们考虑优化一下这个连边的过程 ,因为都是区间上的操作,所以能够很明显的想到利用线段树来维 ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- POJ 3204 Ikki's Story I - Road Reconstruction
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- Codeforces #380 div2 C(729C) Road to Cinema
C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- dp or 贪心 --- hdu : Road Trip
Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...
- HDU 1598 find the most comfortable road(最小生成树之Kruskal)
题目链接: 传送门 find the most comfortable road Time Limit: 1000MS Memory Limit: 32768 K Description XX ...
- 三分 --- CSU 1548: Design road
Design road Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 Mean: 目的:从(0,0)到 ...
- hdu 5861 Road 两棵线段树
传送门:hdu 5861 Road 题意: 水平线上n个村子间有 n-1 条路. 每条路开放一天的价格为 Wi 有 m 天的操作,每天需要用到村子 Ai~Bi 间的道路 每条路只能开放或关闭一次. ( ...
- HDU4081 Qin Shi Huang's National Road System(次小生成树)
枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出M ...
- 杭电 1595 find the safest road
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- 2017-2018-1 20155307 《信息安全系统设计基础》第san周学习总结
2017-2018-1 20155307 <信息安全系统设计基础>第三周学习总结 教材学习内容总结 无符号数和有符号数的特性,数字如何表示,IEEE标准,浮点数格式.(我感觉我把课下测试做 ...
- libuv源码分析
项目开发过程中经常使用了基于libuv库封装的库接口来实现异步处理,一直没仔细研究过这些接口的内部如何实现,因此也就没有掌握它的设计思想.今天花了点时间研究了其事件循环内部的一些过程,总算有了一些理解 ...
- Drupal7 针对特定条件才显示区块
当D7中开启PHP filter模块. Text format就会多出“PHP Code”选项. 而且,新建block时也会多出"Pages on which PHP code return ...
- bootstrap的Alerts中 可以放置p标签 设置 align="center" 用来设置文本居中
效果
- idea alt+enter导包时被锁定导某一个包时的解决方法
在只有一个包指向的时候,把光标放在Test这种字符之间的话 就会直接导这个 所以把光标放在最后就可以导别的了
- java nio实现文件复制
public class TransferTo { public static void main(String[] args) throws Exception { FileChannel in = ...
- Linux大全
Linux 基本指令介紹 一定要先學會的指令:ls, more, cd, pwd, rpm, ifconfig, find 登入與登出(開機與關機):telnet, login, exit, sh ...
- LJ语录
"保持安静,不要打扰我睡觉." ( 半分钟后) "哎呦喂~可以睡觉喽~" "考场上遇到这种题目,你们应该高兴." "因为考场上那群 ...
- 使用performance进行前端性能监控
该文章仅作为自己的总结 1.performance.timing对象 navigationStart:当前浏览器窗口的前一个网页关闭,发生unload事件时的Unix毫秒时间戳.如果没有前一个网页,则 ...
- Tree Traversals Again(根据前序,中序,确定后序顺序)
题目的大意是:进行一系列的操作push,pop.来确定后序遍历的顺序 An inorder binary tree traversal can be implemented in a non-recu ...