题意

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(即贝茜每次旅行的目的地)。 贝茜选择的第二短的路径中,可以包含任何一条在最短路中出现的道路,并且,一条路可以重复走多次。当然咯,第二短路的长度必须严格大于最短路(可能有多条)的长度,但它的长度必须不大于所有除最短路外的路径的长度。

分析

类似最短路松弛,维护最短路和次短路,跑SPFA,松弛的时候分类讨论就行了。

注意要保证次短路严格大于最短路。

时间复杂度\(O(nm)\),当然,此题数据很水。

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read()
{
rg T data=0;
rg int w=1;
rg char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
{
data=data*10+ch-'0';
ch=getchar();
}
return data*w;
}
template<class T>il T read(rg T&x)
{
return x=read<T>();
}
typedef long long ll;
co int INF=0x3f3f3f3f; co int N=5001;
typedef std::pair<int,int> pii;
std::vector<pii>g[N];
int fir[N],sec[N];
bool inq[N];
std::queue<int>Q; int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int n,r;
read(n),read(r);
for(int i=1;i<=r;++i)
{
int x,y,w;
read(x),read(y),read(w);
g[x].push_back(pii(y,w));
g[y].push_back(pii(x,w));
}
std::fill(fir+2,fir+n+2,INF);
std::fill(sec+1,sec+n+2,INF);
Q.push(1);
inq[1]=1;
while(Q.size())
{
int x=Q.front();Q.pop();
inq[x]=0;
for(int i=0;i<g[x].size();++i)
{
int y=g[x][i].first,w=g[x][i].second;
if(fir[x]+w<fir[y])
{
sec[y]=fir[y],fir[y]=fir[x]+w; // edit 1
if(sec[x]+w<sec[y])
sec[y]=sec[x]+w;
if(!inq[y])
{
Q.push(y);
inq[y]=1;
}
}
else if(fir[x]+w>fir[y]&&fir[x]+w<sec[y])
{
sec[y]=fir[x]+w;
if(!inq[y])
{
Q.push(y);
inq[y]=1;
}
}
else if(sec[x]+w<sec[y])
{
sec[y]=sec[x]+w;
if(!inq[y])
{
Q.push(y);
inq[y]=1;
}
}
}
}
printf("%d\n",sec[n]);
return 0;
}

LG2865 [USACO06NOV]路障Roadblocks的更多相关文章

  1. P2865 [USACO06NOV]路障Roadblocks

    P2865 [USACO06NOV]路障Roadblocks 最短路(次短路) 直接在dijkstra中维护2个数组:d1(最短路),d2(次短路),然后跑一遍就行了. attention:数据有不同 ...

  2. 洛谷——P2865 [USACO06NOV]路障Roadblocks

    P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...

  3. 络谷 P2865 [USACO06NOV]路障Roadblocks

    P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...

  4. P2865 【[USACO06NOV]路障Roadblocks】(次短路)

    传送门 算法Dijkstra要求次短路 那么在不考虑重复走一条边的情况下 肯定是把最短路中的一段改成另一段 至少要换另一条边到路径里所以可以枚举所有不属于最短路的每条边(a,b) 那么dis(1,a) ...

  5. 【洛谷 P2865】 [USACO06NOV]路障Roadblocks(最短路)

    题目链接 次短路模板题. 对每个点记录最短路和严格次短路,然后就是维护次值的方法了. 和这题一样. #include <cstdio> #include <queue> #in ...

  6. 洛谷题解 P2865 【[USACO06NOV]路障Roadblocks】

    链接:https://www.luogu.org/problemnew/show/P2865 题目描述 Bessie has moved to a small farm and sometimes e ...

  7. luogu2865 [USACO06NOV]路障Roadblocks 次短路

    注意:如果是这么个写法,堆数组要开成n+m的. 为什么呢?设想一下从1到2有m条长度递减的路,这岂不是要入队m次-- #include <algorithm> #include <i ...

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

    ·求1到n的严格次短路. [题解] dijktra魔改?允许多次入队,改了次短路的值也要入队. #include<cstdio> #include<algorithm> #de ...

  9. 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   ...

随机推荐

  1. PHP下使用Redis消息队列发布微博

    phpRedisAdmin :github地址  图形化管理界面 git clone [url]https://github.com/ErikDubbelboer/phpRedisAdmin.git[ ...

  2. Linux基本命令 帮助命令

    命令名称:man 英文原意:manual 命令所在路径:/usr/bin/man 执行权限:所有用户 语法:man [命令或者配置文件] 功能描述:获取帮助信息 例如:man ls 查看ls命令的帮助 ...

  3. 20165101刘天野 2017-2018-2 《Java程序设计》第8周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第8周学习总结 教材学习内容总结 第十二章Java多线程机制 一.进程与线程 1.1.任务调动 大部分操作系统(如Wi ...

  4. 斯坦福机器学习视频笔记 Week9 异常检测和高斯混合模型 Anomaly Detection

    异常检测,广泛用于欺诈检测(例如“此信用卡被盗?”). 给定大量的数据点,我们有时可能想要找出哪些与平均值有显着差异. 例如,在制造中,我们可能想要检测缺陷或异常. 我们展示了如何使用高斯分布来建模数 ...

  5. Kubernetes Ingress

    Kubernetes关于服务的暴露主要是通过NodePort方式,通过绑定node主机的某个端口,然后进行pod的请求转发和负载均衡,但这种方式下缺陷是 Service可能有很多个,如果每个都绑定一个 ...

  6. javascript 内置对象及常见API

    javascript 内置对象及常见API 2012-09-02 15:17 571人阅读 评论(0) 收藏 举报 javascript正则表达式文档浏览器urlstring Javascript内置 ...

  7. Java中使用注释

    在编写程序时,经常需要添加一些注释,用以描述某段代码的作用. 一般来说,对于一份规范的程序源代码而言,注释应该占到源代码的 1/3 以上.因此,注释是程序源代码的重要组成部分,一定要加以重视哦! Ja ...

  8. JMeter正则表达式提取器说明

    Apply to:应用范围 要检查的响应字段:样本数据源. 引用名称:其他地方引用时的变量名称,引用方法:${引用名称} 正则表达式:数据提取器,如上图的 "sysNo":&quo ...

  9. sublime 编译C++

    http://www.yalewoo.com/sublime_text_3_gcc.html 这篇博客很强 完美解决在sublime里写C++无法输入的问题 sublime text 3配置c/c++ ...

  10. Python OS导入一个文件夹所有文件

    import os path = 'F:/save_file/seminarseries/' for root, dirs, files in os.walk(path): print(root) 这 ...