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 \ ...
随机推荐
- c#.net常用函数列表
.DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System.Da ...
- jQuery:has()和jQuery:contains()及jQuery:empty
jQuery:has()和jQuery:contains()两个方法比较类似.不同点在于: has是判断标签的 contains是判断文本的 1.jQuery:has() <div>< ...
- Linux 下編輯 PDF 檔的工具(PDF editor under Linux)(转载)
转自:http://www.gtwang.org/2011/05/linux-pdf.html PDF 檔雖然是一個跨平台的檔案格式,但 Adobe 只有提供免費的 Adobe Reader,要看 P ...
- bzoj 1093: [ZJOI2007]最大半连通子图【tarjan+拓扑排序+dp】
先tarjan缩成DAG,然后答案就变成了最长链,dp的同时计数即可 就是题面太唬人了,没反应过来 #include<iostream> #include<cstdio> #i ...
- 在CentOS下安装VMware tool
VMware tools是虚拟机VMware Workstation自带的一款工具.它的作用就是使用户可以从物理主机直接往虚拟机里面拖文件.如果不安装它,我们是无法进行虚拟机和物理机之间的文件传输的. ...
- curl 做爬虫 用服务器代理ip
有时候会ip会封锁,所以会用一些模拟代理ip进行抓取测试 从网上找了一下代码 function curl_string ($url,$user_agent,$proxy){ $ch = ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
There are nn rectangles on the plane. The problem is to find the area of the union of these rectangl ...
- linux学习之路7 linux下获取帮助
help 帮助 ls -h或者ls - -help man 最常用的帮助命令 man (+数字 )+命令 (数字代表文档帮助类型) man -k 关键字 可以用来查询包含该关键字的文档 info 与m ...
- 51nod1265判断四点共面
1265 四点共面 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出三维空间上的四个点(点与点的位置均不相同),判断这4个点是否在同一个平面内(4点共 ...
- E - Easy Dijkstra Problem(求最短路)
Description Determine the shortest path between the specified vertices in the graph given in the inp ...