Roadblocks

Roadblocks

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)

C++编写:

采用Dijkstra算法,索引值均从0开始,要从1开始的话只需要稍微更改几行代码,看懂了就很好改了

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAX_N=5005;

typedef pair<int,int> P;        //first是最短距离,second是顶点的编号
struct edge{
    int to,cost;
};

int N,R;
vector<edge> G[MAX_N];          //创建一个二维数组表示图的邻接表,其实就是MAX_N个一维数组
int dist[MAX_N];                //存储最短距离
int dist2[MAX_N];               //存储次短距离

void solve()
{
    int m,n,k;
    for(int i=0;i<R;i++)               //将所有读入的数据放到邻接表中
    {
        cin>>m>>n>>k;
        G[m-1].push_back(edge{n-1,k});          //编号从0开始,所以要减1
        G[n-1].push_back(edge{m-1,k});
    }

    priority_queue<P,vector<P>,greater<P> > que;       //堆按照first从小到大的顺序取出值,这里也就说明了为什么用first表示距离
    fill(dist,dist+N,INF);                    //将dist中前N个元素的值全部初始化为INF
    fill(dist2,dist2+N,INF);                  //将dist2中前N个元素的值全部初始化为INF
    dist[0]=0;                                //将dist[0]的值改为0
    que.push(P(0,0));

    while(!que.empty())
    {
        P p=que.top();
        que.pop();
        int d=p.first,v=p.second;
        if(dist2[v]<d)    continue;            //当取出的值不是当前最短距离或次短距离,就舍弃他
        for(int i=0;i<G[v].size();i++)
        {
            edge &e=G[v][i];
            int d2=d+e.cost;
            if(dist[e.to]>d2)
            {
                swap(dist[e.to],d2);
                que.push(P(dist[e.to],e.to));       //以first为距离,second为顶点编号的顺序放入优先队列中
            }

            if(dist2[e.to]>d2 && dist[e.to]<d2)
            {
                dist2[e.to]=d2;
                que.push(P(dist2[e.to],e.to));
            }
        }
    }
    cout<<dist2[N-1]<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>N>>R;
    solve();
    return 0;
}

那么这里给出案例进去程序所得到的邻接表,有助于理解
在这里插入图片描述
如果是第一次做这种题并且光是看代码感觉很难理解的话是很正常的。那么此处推荐一种方法就是,用案例按照程序的步骤来分析理解代码,该案例四个顶点其实带进去看会很耗时也是很麻烦的,推荐少用一个顶点,也就是换成下面这种带入

3 2
1 2 100
2 3 250

这样肯定就容易操作得多,用这个案例我们自己也可以知道顶点1到顶点1、顶点2、顶点3的最短距离分别为0、100和350,次短距离分别为200、300、550。计算正确的话肯定得到的也是这些值,如果对自己的结果有所怀疑,不太确定,那么在solve()函数末尾加几行代码我们看一下实际的最短距离和次短距离,就可以了,所要加的代码如下:

for(int i=0;i<N-1;i++)
    {
        cout<<dist[i]<<" ";
    }
    cout<<dist[N-1]<<endl;
    for(int i=0;i<N-1;i++)
    {
        cout<<dist2[i]<<" ";
    }
    cout<<dist2[N-1]<<endl;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值