次短路 poj 3255

Posted 全速前進yosoro

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了次短路 poj 3255相关的知识,希望对你有一定的参考价值。

 

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: AB, 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)

Source

题目大意很简单,给n个点r条边,求次短路的长度,一开始看起来不是很好下手,最容易想到的方法就是dij,在松弛的时候顺带松弛次优的边,存到d2数组里,每次比较除了最优中,最短的路径,然后输出d[n]。
技术分享图片
//#include <bits/stdc++.h>
#include<iostream>  
#include<cstdio>  
#include<queue>  
#include<cstring>  
using namespace std;

#define maxn 100005
#define INF 9999999

typedef pair<int,int> pii;
int d[100000+5],d2[100000+5];
vector<pii> e[maxn];
int n,k;


void add_edge(int x,int y,int z)
{
    e[x].push_back(make_pair(y,z));
    e[y].push_back(make_pair(x,z));
}

void init(int n)
{
    for(int i=0;i<=n;i++) e[i].clear();
    for(int i=0;i<=n;i++) d[i]=d2[i]=INF;
}

void dij()
{
    priority_queue<pii,vector<pii>,greater<pii> > q;
    d[1]=0;
    q.push(make_pair(0,1));
    while(!q.empty())
    {
        pii p=q.top();
        q.pop();
        int dis=p.first;
        int now=p.second;
        if(d2[now]<dis) continue;
        for(int i=0;i<e[now].size();i++)
        {
            int u=e[now][i].first;
            int v=e[now][i].second;
            int dis2=dis+v;
            if(d[u]>dis2)
            {
                swap(d[u],dis2);
                q.push(make_pair(d[u],u));
            }
            if(d[u]<dis2 && dis2<d2[u])
            {
                d2[u]=dis2;
                q.push(make_pair(d2[u],u));
            }
        }
    }
    cout<<d2[n]<<endl;
}

int main()
{
    cin>>n>>k;
    init(n);
    for(int i=0;i<k;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        add_edge(x,y,z);
    }
    dij();

    return 0;
}
View Code

poj运行结果:4888K 1594ms

 

另一种思路,我尝试用spfa做,因为一共n个点,就有n-1条最优的边,那么如果有一条边不是最优的,且这条边是其他中不优边中最短的,那么也形成了次短路,那么只需要对1

和n都spfa一次,然后次短路一定存在于 d1[i]+e[i][j]+d2[j] 和 d1[j]+e[i][j]+d2[i] 中的一个,接着枚举一遍就好了。

技术分享图片
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;

#define maxn 100005
#define INF 9999999

typedef pair<int,int> pii;
int d1[maxn+5],d2[maxn+5],vis[maxn+5];
vector<pii> e[maxn];
int n,k;


void add_edge(int x,int y,int z)
{
    e[x].push_back(make_pair(y,z));
    e[y].push_back(make_pair(x,z));
}

void init(int n)
{
    for(int i=0; i<=n; i++) e[i].clear();
    for(int i=0; i<=n; i++) d1[i]=d2[i]=INF;
}

void spfa(int s,int d[])
{
    queue<int>q;
    memset(vis,0,sizeof(vis));
    q.push(s);
    d[s]=0;
    vis[s]=1;
    while(!q.empty())
    {
        int now=q.front();
        vis[now]=0;
        q.pop();
        for(int i=0; i<e[now].size(); i++)
        {
            int v=e[now][i].first;
            if(d[v]>d[now]+e[now][i].second)
            {
                d[v]=d[now]+e[now][i].second;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}

int main()
{
    cin>>n>>k;
    init(n);
    for(int i=0; i<k; i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        add_edge(x,y,z);
    }
    spfa(1,d1);
    spfa(n,d2);
    int ans=INF;
    for(int i=1; i<=n; i++)
        for(int j=0; j<e[i].size(); j++)
        {
            int u=e[i][j].first;
            int v=e[i][j].second;
            int t=d1[i]+v+d2[u];
            if(t>d1[n] && t<ans)
            {
                ans=t;
            }
        }
    cout<<ans<<endl;
    return 0;
}
View Code

poj运行结果:4888K 1532ms

两个方法的时间复杂度是一样的。。。。。反复多交了几次 第一种大概只比第二种多个6、70ms 果断还是第二种好233333,毕竟万一有负权边就gg了,而且第二种思路也简单一些。。。


以上是关于次短路 poj 3255的主要内容,如果未能解决你的问题,请参考以下文章

poj3255

POJ-3255 Roadblocks---Dijkstra队列优化+次短路

POJ 3255 Roadblocks (次短路模板)

次短路 poj 3255

POJ3255 Roadblocks 次短路

POJ3255Roadblocks[次短路]