ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

次短路

2021-10-03 23:02:06  阅读:199  来源: 互联网

标签:int 短路 path d2 shortest d1


题目链接

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;
}

标签:int,短路,path,d2,shortest,d1
来源: https://www.cnblogs.com/zyyun/p/15365328.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有