次短路(POJ - 3255 Roadblocks )

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

思路:能做这个题的人肯定都知道最短路的基本解法,也就是O(n2)的算法,可能也有少部分人知道用优先队列实现的堆优化后的dijkstra,复杂度降到了O(nlogn),此题就是在堆优化的dijkstra算法的基础上做了简单的修改

由于我水平很浅,遇到这个题之后去网上找了很多博客,都没找到一个思路详细,代码风格好的博客,唉,只能自己慢慢啃咯,话不多说,进入正题。

此题要求的是次短路,我们可以想,当这条路能走通且比我们的最短路长的时候,我们找出多条(如果有的话)符合这个条件的路,然后在其中选一条最短的,那么这应该就是次短路了。

所以我们再开一个disc数组表示从起点到i点的次短路(形式类似于最短路),每次把满足条件的最短路和次短路都扔进优先队列,更新到最后,最后就可以得到次短路啦,其实不需要太多去考虑挑出每条最短路的过程,肯定最后会把每条边都遍历一遍的(因为更新最短路的时候会遍历到每个点,这样就肯定会找到每条边),最终就是正确的答案了。

AC代码:

#include<queue>
#include<vector>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
 
const int INF=1000000000;
const int max_e=200000+5;
typedef pair<int,int>P;
struct edge{
    int to,dis;
    edge(int to,int dis){
        this -> to = to;
        this -> dis = dis;
    }
};
int N,R;
int a,b,c;
int dis[5005];         //记录最短路径
int disc[5005];       //记录次短路径
vector<edge>G[max_e];
 
void dijkstra(){
    fill(dis+1,dis+N+1,INF);
    fill(disc+1,disc+N+1,INF);
    priority_queue<P,vector<P>,greater<P> >q;
    dis[1]=0;
    q.push(P(0,1));
    while(q.size()){
        P p=q.top();
        q.pop();
        int dd=p.first;
        int v=p.second;
        if(disc[v]<dd) 
        {
        	continue;  //如果这条路比次短的路径都长,那么肯定不用考虑了 
		}
        for(int i=0;i<G[v].size();i++){
            edge& e=G[v][i];   //起点为v的第i条边 
            int d=dd+e.dis;
        //    cout<<"d"<<d<<endl;
            if(dis[e.to]>d){//保证dis[e.to]是最小的 
                swap(dis[e.to],d); //这里交换是为了把当前的dis[e.to]保存下来判断次短路 
                q.push(P(dis[e.to],e.to));
            }
            if(disc[e.to]>d&&dis[e.to]<d){   //只有满足情况,才更新最短路, 
                disc[e.to]=d;
                q.push(P(disc[e.to],e.to));
            }
        }
    }
    cout<<disc[N]<<endl;
}
 
int main()
{
    scanf("%d%d",&N,&R);
    for(int i=1;i<=R;i++){
        scanf("%d%d%d",&a,&b,&c);
        G[a].push_back(edge(b,c));
        G[b].push_back(edge(a,c));
    }
    dijkstra();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zvenWang/article/details/85201654