洛谷题解 P2865 【[USACO06NOV]路障Roadblocks】
链接:https://www.luogu.org/problemnew/show/P2865
题目描述
Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.
The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.
The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).
贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友。贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样,选择最短路。 贝茜所在的乡村有R(1<=R<=100,000)条双向道路,每条路都联结了所有的N(1<=N<=5000)个农场中的某两个。贝茜居住在农场1,她的朋友们居住在农场N(即贝茜每次旅行的目的地)。 贝茜选择的第二短的路径中,可以包含任何一条在最短路中出现的道路,并且,一条路可以重复走多次。当然咯,第二短路的长度必须严格大于最短路(可能有多条)的长度,但它的长度必须不大于所有除最短路外的路径的长度。
输入输出格式
输入格式:
Line 1: Two space-separated integers: N and R
Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)
输出格式:
Line 1: The length of the second shortest path between node 1 and node N
题解:
在做本题前,一定理解最短路算法的核心思想。 当然,并不需要对所有的最短路算法有深刻了解。 所以今天我就给大家带来优先队列优化的dijsktra。(SPFA我不会)
那么问题来了,经典的dijkstra只能维护最短路,怎么去维护次短路呢?其实很简单,只要同时维护两个数组就行了。
那怎么更新呢?我们设dis1数组存最短路,dis2数组存最长路,当找到一条路,比dis1和dis2都短时,就先把目前次短路赋为目前最短路,也就是让dis2=dis1,再把目前最短路赋为找到的更短路。剩下的就是dijkstra的板子了。
当我们存边时,还是推荐大家用邻接表存边,只用写一个函数,跑的好像还比vector快,下面就为大家带来一个不怎么好看的dijkstra板:
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<stack>
#include<vector>
#include<cstdio>
#define ll long long
#define inf 1e9+10
using namespace std;
const int maxn=1e5+;
struct mxr
{
int to,next,val;
}e[*maxn];
int n,cnt,m,vis[maxn],head[maxn],s;
int dis[maxn];
priority_queue<pair<ll,int> >q;
inline int read()
{
int s=,f=;char ch=getchar();
while(ch<''||ch>''){ch=getchar();
if(ch=='-')f=-;}
while(ch>=''&&ch<='')
s=(s<<)+(s<<)+(ch^),ch=getchar();
return s*f;
}
inline void add(int u,int v,int w)
{
e[cnt].to=v;
e[cnt].val=w;
e[cnt].next=head[u];
head[u]=cnt++;
return ;
}
inline void dij()
{
q.push(make_pair(,s));
dis[s]=;
while(!q.empty())
{
int now=q.top().second;
q.pop();
if(vis[now]) continue;
vis[now]=;
for(int i=head[now];i!=-;i=e[i].next)
{
int v=e[i].to;
if(dis[v]>dis[now]+e[i].val)
{
dis[v]=dis[now]+e[i].val;
q.push(make_pair(-dis[v],v));
}
}
}
return ;
}
int main()
{
memset(head,-,sizeof(head));
cnt=;
n=read(),m=read(),s=read();
for(int i=;i<=n;i++) dis[i]=inf;
for(int i=;i<=m;i++)
{
int a,b,w;
a=read(),b=read(),w=read();
add(a,b,w);
}
dij();
for(int i=;i<=n;i++) printf("%d ",dis[i]);
return ;
}
下面是本题代码:
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<stack>
#include<vector>
#include<cstdio>
#define ll long long
#define inf 1e9+10
using namespace std;
const int maxn=1e5+;
struct mxr
{
int to,next,val;
}e[*maxn];
int n,cnt,m,head[maxn];
int dis1[maxn],dis2[maxn];
priority_queue<pair<ll,int> >q;
inline char nc()//fread,跑的快
{
static char buf[],*L=buf,*R=buf;
return L==R&&(R=(L=buf)+fread(buf,,,stdin),L==R)?EOF:*L++;
}
inline int read()
{
int ret=,f=;char ch=nc();
while (ch<''||ch>''){if (ch=='-') f=-;ch=nc();}
while (ch>=''&&ch<='') ret=ret*+ch-'',ch=nc();
return ret*f;
}
inline void write(int x)
{
if(x<) putchar('-'),x=-x;
if(x>) write(x/);
putchar(x%+'');
return ;
}
inline void add(int u,int v,int w)//邻接表存边,貌似比vector快
{
e[cnt].to=v;
e[cnt].val=w;
e[cnt].next=head[u];
head[u]=cnt++;
return ;
}
inline void dij()//优先队列优化dijkstra
{
for(register int i=;i<=n;++i) dis1[i]=inf,dis2[i]=inf;
dis1[]=;
q.push(make_pair(,));
while(!q.empty())
{
int now=q.top().second,dis=-q.top().first;
q.pop();
for(register int i=head[now];i!=-;i=e[i].next)
{
int v=e[i].to;
if(dis1[v]>dis+e[i].val)
dis2[v]=dis1[v],dis1[v]=dis+e[i].val,q.push(make_pair(-dis1[v],v));//找到更优,更新dis1与dis2
if(dis2[v]>dis+e[i].val&&dis1[v]<dis+e[i].val)
dis2[v]=dis+e[i].val,q.push(make_pair(-dis2[v],v));//找到比dis2优,比dis1劣,更新dis2
}
}
return ;
}
int main()
{
memset(head,-,sizeof(head));
cnt=;
n=read(),m=read();
for(register int i=;i<=m;++i)
{
int a,b,w;
a=read(),b=read(),w=read();
add(a,b,w),add(b,a,w);
}
dij();
write(dis2[n]),puts(" ");
return ;
}
谢谢大家!
洛谷题解 P2865 【[USACO06NOV]路障Roadblocks】的更多相关文章
- 洛谷——P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...
- 络谷 P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...
- P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 最短路(次短路) 直接在dijkstra中维护2个数组:d1(最短路),d2(次短路),然后跑一遍就行了. attention:数据有不同 ...
- 洛谷P2865 [USACO06NOV]路障Roadblocks——次短路
给一手链接 https://www.luogu.com.cn/problem/P2865 这道题其实就是在维护最短路的时候维护一下次短路就okay了 #include<cstdio> #i ...
- 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 ...
- 洛谷 题解 UVA572 【油田 Oil Deposits】
这是我在洛谷上的第一篇题解!!!!!!!! 这个其实很简单的 我是一只卡在了结束条件这里所以一直听取WA声一片,详细解释代码里见 #include<iostream> #include&l ...
- 洛谷 题解 P1600 【天天爱跑步】 (NOIP2016)
必须得说,这是一道难题(尤其对于我这样普及组205分的蒟蒻) 提交结果(NOIP2016 天天爱跑步): OJ名 编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间 Libre ...
- 洛谷题解P4314CPU监控--线段树
题目链接 https://www.luogu.org/problemnew/show/P4314 https://www.lydsy.com/JudgeOnline/problem.php?id=30 ...
- 【洛谷 P2865】 [USACO06NOV]路障Roadblocks(最短路)
题目链接 次短路模板题. 对每个点记录最短路和严格次短路,然后就是维护次值的方法了. 和这题一样. #include <cstdio> #include <queue> #in ...
随机推荐
- 1073 Scientific Notation
题意: 给出科学计数法的形式,转化成常规的表示,要求保留所有的有效位数 思路:纯粹的字符串处理问题,注意边界条件即可.如+1.23E+02这种,转化后是123,既不需要补0,也不需要添加小数点. 代码 ...
- thinkphp线上自动加载异常与修复
项目遇到一个奇怪的问题,本地代码正常,服务器上却不正常. 经过测试,应该是自动加载出了问题,尝试了各种方法, 1.手动加载,发现好麻烦,没完没了. 2.自己写自动加载,写不出来,尴尬. 3.修改配置, ...
- ARM-Linux移植之(三)——init进程启动流程分析
我们通常使用Busybox来构建根文件系统的必要的应用程序.Busybox通过传入的参数来决定执行何种操作.当init进程启动时,实际上调用的是Busybox的init_main()函数,下面我们来分 ...
- oracle自动创建表分区
创建一个table,记录哪些表需要创建表分区 create table STAT_TABLE ( tablename VARCHAR2(), pre_partition_name VARCHAR2() ...
- MongoDB 学习笔记(一)—— 安装入门
注:我的环境是win7 32位. 下载安装 http://www.mongodb.org/downloads 解压即可.这里我重命名“mongodb”,存放的目录为E:\mongodb. 新建数据文件 ...
- el表达式动态拼接变量_c:set的用法
转自:https://blog.csdn.net/xb12369/article/details/39581955如 何在${}中使用${},例:${user.name_${user.id}},use ...
- 预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)转
vs2010的mfc项目中编译c语言出现错误: "...预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)" 解决方法: 建工程时 建立空项目 ...
- mysql 批量插入与单条插入 的效率比较
1.数据插入性能(单个插入和批量插入) public class Test { private Long id; private String test; public Long getId() { ...
- 浅谈PL/SQL语言基础
在前面的学习中,我们大部分接触的都是SQL语言,但是,在实现复杂操作的时候,SQL语言就无能为力了,这时候就需要引入新的语言,PL/SQL语言就是对SQL语言的扩展,可以实现存储过程,函数等的创建.下 ...
- Python模块及其导入
一.模块 1.模块的定义: 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少, 很多编程语言都采用这种组织代码的方式.在Python中,一个.py文件 ...