BZOJ 1726 洛谷 2865 [USACO06NOV]路障Roadblocks【次短路】

·求1到n的严格次短路。
【题解】
dijktra魔改?允许多次入队,改了次短路的值也要入队。
#include<cstdio>
#include<algorithm>
#define M 200010
#define N 5010
#define rg register
using namespace std;
int n,m,tot,last[N],dis[N],d2[N],pos[N];
struct edge{
int to,pre,dis;
}e[M];
struct heap{
int poi,dis;
}h[N];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline void add(int x,int y,int z){
e[++tot]=(edge){y,last[x],z}; last[x]=tot;
}
inline void up(int x){
int fa;
while((fa=x>>)&&h[fa].dis>h[x].dis){
swap(h[fa],h[x]); swap(pos[h[fa].poi],pos[h[x].poi]);
x=fa;
}
}
inline void down(int x){
int son;
while((son=x<<)<=tot&&h[son].dis<h[x].dis){
if(h[son+].dis<h[son].dis) son++;
if(h[son].dis<h[x].dis){
swap(h[son],h[x]); swap(pos[h[son].poi],pos[h[x].poi]);
x=son;
}
else return;
}
}
inline void dijkstra(int x){
h[pos[x]=tot=]=(heap){x,dis[x]=};
while(tot){
int now=h[].poi; h[]=h[tot--]; if(tot) down();
for(rg int i=last[now];i;i=e[i].pre){
int tmp=dis[now]+e[i].dis,tmp2=d2[now]+e[i].dis,to=e[i].to;
bool flag=;
if(dis[to]<tmp&&d2[to]>tmp) d2[to]=tmp,flag=;
if(dis[to]<tmp2&&d2[to]>tmp2) d2[to]=tmp2,flag=;
if(dis[to]>tmp) d2[to]=min(dis[to],tmp2),dis[to]=tmp,flag=;
if(flag){
if(!pos[to]) h[pos[to]=++tot]=(heap){to,dis[to]};
else h[pos[to]].dis=dis[to];
up(pos[to]);
}
pos[now]=;
}
}
}
int main(){
n=read(); m=read();
for(rg int i=;i<=n;i++) dis[i]=d2[i]=1e9;
for(rg int i=;i<=m;i++){
int u=read(),v=read(),w=read();
add(u,v,w); add(v,u,w);
}
dijkstra();
printf("%d",d2[n]);
return ;
}
BZOJ 1726 洛谷 2865 [USACO06NOV]路障Roadblocks【次短路】的更多相关文章
- 洛谷P2865 [USACO06NOV]路障Roadblocks——次短路
给一手链接 https://www.luogu.com.cn/problem/P2865 这道题其实就是在维护最短路的时候维护一下次短路就okay了 #include<cstdio> #i ...
- 洛谷——P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...
- POJ——T 3255 Roadblocks|| COGS——T 315. [POJ3255] 地砖RoadBlocks || 洛谷—— P2865 [USACO06NOV]路障Roadblocks
http://poj.org/problem?id=3255 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15680 ...
- 络谷 P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...
- luogu2865 [USACO06NOV]路障Roadblocks 次短路
注意:如果是这么个写法,堆数组要开成n+m的. 为什么呢?设想一下从1到2有m条长度递减的路,这岂不是要入队m次-- #include <algorithm> #include <i ...
- P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 最短路(次短路) 直接在dijkstra中维护2个数组:d1(最短路),d2(次短路),然后跑一遍就行了. attention:数据有不同 ...
- 【POJ3255/洛谷2865】[Usaco2006 Nov]路障Roadblocks(次短路)
题目: POJ3255 洛谷2865 分析: 这道题第一眼看上去有点懵-- 不过既然要求次短路,那估计跟最短路有点关系,所以就拿着优先队列优化的Dijkstra乱搞,搞着搞着就通了. 开两个数组:\( ...
- [BZOJ 3039&洛谷P4147]玉蟾宫 题解(单调栈)
[BZOJ 3039&洛谷P4147]玉蟾宫 Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地. ...
- 洛谷P1879 [USACO06NOV]玉米田Corn Fields(状压dp)
洛谷P1879 [USACO06NOV]玉米田Corn Fields \(f[i][j]\) 表示前 \(i\) 行且第 \(i\) 行状态为 \(j\) 的方案总数.\(j\) 的大小为 \(0 \ ...
随机推荐
- SYSUCPC2017 online round La La string 应用manacher算法
manacher算法给出一个字符串中 以每个位置为对称中心的回文串长度,但是大部分时候我们只需要知道以每个位置为起点的回文串长度,感觉有点浪费. 那么来看看这个不难也不太简单的题目 第一步,我们要想办 ...
- win7安装oracle
1.下载 2.安装 主目录关键重要
- 观光公交 2011年NOIP全国联赛提高组(贪心,递推)
观光公交 2011年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 风景迷人的小城 Y 市 ...
- jQuery插件之jqzoom的使用和参数设置
jqzoom是一款基于jQuery的图片方法插件. 使用方法:1.引入jQuery与jqzoom,jqzoom.css 2.准备两张一大一小大小相同的图片,小图片放在<img>标签的&qu ...
- [C++ STL] set使用详解
一.set介绍: set容器内的元素会被自动排序,set与map不同,set中的元素即是键值又是实值,set不允许两个元素有相同的键值.不能通过set的迭代器去修改set元素,原因是修改元素会破坏se ...
- 289 Game of Life 生命的游戏
假设有一个大小为m*n的板子,有m行,n列个细胞.每个细胞有一个初始的状态,死亡或者存活.每个细胞和它的邻居(垂直,水平以及对角线).互动规则如下:1.当前细胞存活时,周围低于2个存活细胞时,该细胞死 ...
- Laravel5.1学习笔记23 Eloquent 序列化
Eloquent: Serialization Introduction Basic Usage Hiding Attributes From JSON Appending Values To JSO ...
- [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)
\(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...
- C++学习笔记(三)之函数库
1.标准库函数 begin end begin 返回数组首地址 end 返回数组尾地址 2.const 在声明变量时对变量限制为只读,不允许修改 const int i = 5; 单个const作 ...
- Codeforces_768_D_(概率dp)
D. Jon and Orbs time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...