Roadblocks-次最短路,Dijkstra变形+邻接表存储

题目:

    

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

输入:

    Line 1: Two space-separated integers: <i>N</i> and <i>R</i><br>Lines 2..<i>R</i>+1: Each line contains three space-separated integers: <i>A</i>, <i>B</i>, and <i>D</i> that describe a road that connects intersections <i>A</i> and <i>B</i> and has length <i>D</i> (1 ≤ <i>D</i> ≤ 5000)

输出:

扫描二维码关注公众号,回复: 2191417 查看本文章

Line 1: The length of the second shortest path between node 1 and node <i>N</i>

样例:

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


450


题目大意:有R条道路,N个路口,道路是双向的从1号路到N号路口的次最短路是多少。

思路:记最短路径长度为d[ ],次短路径长度为dd[ ],则d[v] = min( d[u] + cost[u][v],  dd[u] + cost[u][v] ),所以我们只需要计算出最短路径和次短路径即可。在实现Dijkstra算法的时候,我们既要记录最短路径,还要记录次短路径。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#define MAX_N 5000
#define INF 500000000
using namespace std;

int N,R;
struct node
{
    int to,cost;
};
vector<node> G[MAX_N]; //邻接表表示
int d[MAX_N],dd[MAX_N];//d最短路,dd次最短路

void solve()
{
    typedef pair<int,int> P;
    priority_queue<P, vector<P>, greater<P> > q;
    fill(d, d+N, INF);        //初始化数组,同memset
    fill(dd, dd+N, INF);

    d[0]=0;
    q.push(P(0,0));

    while(!q.empty())
    {
        P p=q.top();q.pop();
        int v=p.second,d1=p.first;
        if(dd[v]<d1)
            continue;
        for(int i=0;i<G[v].size();i++)
        {
            node e=G[v][i];
            int d2=d1+e.cost;
            if(d[e.to]>d2) //最短路
            {
                swap(d[e.to],d2);
                q.push(P(d[e.to],e.to));
            }
            if(dd[e.to]>d2&&d[e.to]<d2) //次最短路
            {
                dd[e.to]=d2;
                q.push(P(dd[e.to],e.to));
            }
        }
    }
    cout<<dd[N-1]<<endl;
}

int main()
{
    int to,from,cost;
    node foo;
    while(cin>>N>>R&&R&&N)
    {
        for(int i=0;i<R;i++)
        {
            cin>>from>>to>>cost;
            from--;to--;
            foo.to=to;
            foo.cost=cost;
            G[from].push_back(foo); 
            foo.to=from; //道路是双向的
            G[to].push_back(foo);
        }
        solve();
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/wentong_Xu/article/details/81053354