次短路

题目链接

poj3255

Roadblocks

Time Limit: 2000MS Memory Limit: 65536K

Description

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

Input

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)
Output

Line 1: The length of the second shortest path between node 1 and node N

Sample Input

4 4 1 2 100 2 4 200 2 3 250 3 4 100

Sample Output

450

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

解题思路

跟求次小值类似,即:更新最短路的同时更新次短路,再更新两次次短路:次短路与前一次次短路,当前没有被更新的最短路与次短路~
spfa算法为例,其他算法最短路模板同理改动即可~

  • 时间复杂度:O(kn)

代码

#include<queue> #include<cstdio> #include<cstring> using namespace std; const int N=5010; int n,m; vector<pair<int,int> > adj[N]; int d1[N],d2[N]; bool v[N]; void spfa() { memset(v,0,sizeof v); memset(d1,0x3f,sizeof d1); memset(d2,0x3f,sizeof d2); queue<int> q; q.push(1); d1[1]=0; v[1]=true; while(q.size()) { int x=q.front(); q.pop(); v[x]=false; for(int i=0;i<adj[x].size();i++) { int y=adj[x][i].first,w=adj[x][i].second; if(d1[y]>d1[x]+w) { d2[y]=d1[y]; d1[y]=d1[x]+w; if(!v[y]) q.push(y),v[y]=true; } if(d2[y]>d2[x]+w) { d2[y]=d2[x]+w; if(!v[y]) q.push(y),v[y]=true; } if(d1[y]<d1[x]+w&&d2[y]>d1[x]+w) { d2[y]=d1[x]+w; if(!v[y]) q.push(y),v[y]=true; } } } } int main() { scanf("%d%d",&n,&m); while(m--) { int x,y,w; scanf("%d%d%d",&x,&y,&w); adj[x].push_back(make_pair(y,w)); adj[y].push_back(make_pair(x,w)); } spfa(); printf("%d",d2[n]); return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/15365328.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(85)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示