Roadblocks(Dijkstra变式)

题目链接: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: 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)

题意

  某街道共有R条道路,N个路口。道路双向通道。问1号路口到N号路口的次短路长度是多少?同一条边可以经过多次。

思路

  到某一顶点v的次短路要么是到其他某个顶点u的最短路再加上u->v的边,要么是到u的次最短路再加上u->v的边,所以需要维护两个数字,最短路数组(dis1[maxn])和次短路数组(dis2[maxn]);
小技巧:
  为了节省时间,使用的堆优化,其中typedef pair<int,int> P;first为最短路,second为顶点编号
AC代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;	//first为最短路,second为顶点编号
const int maxn = 5010;
const int inf = 0x3f3f3f3f;
const double dnf = 1.0e5;
struct edge{
    int to,cost;
};
vector<edge> Adj[maxn];	//链表构图
int n,m;
int dis1[maxn],dis2[maxn];	//最短路,次短路数组
void Init()
{
    for(int i=1;i<=n;i++)
        Adj[i].clear();
}
void add(int u,int v,int c)
{
    edge tmp;
    tmp.to = v;
    tmp.cost = c;
    Adj[u].push_back(tmp);
}
void Dijkstra()
{
    priority_queue<P,vector<P>,greater<P> > q;
    for(int i=1;i<=n;i++){
        dis1[i] = dis2[i] = inf;
    }
    dis1[1] = 0;
    q.push(P(0,1));

    while(!q.empty()){
        P p = q.top();
        q.pop();
        int u = p.second;
        int d = p.first;
        if(dis2[u]<d) continue;	
        for(int i=0;i<Adj[u].size();i++){
            int v = Adj[u][i].to;
            int d2 = d + Adj[u][i].cost;
            if(dis1[v]>d2){	//先更新最短路
                swap(dis1[v],d2);	//因为还要更新次短路,所以不能覆盖原来的dis1[v]
                q.push(P(dis1[v],v));	//入队
            }
            if(dis2[v]>d2&&dis1[v]<d2){	//更新次短路
                dis2[v] = d2;
                q.push(P(dis2[v],v));	//入队
            }
        }
    }
    printf("%d\n",dis2[n]);	//输出
}
int main(void)
{
    int u,v,c;
    scanf("%d%d",&n,&m);
    Init();
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&u,&v,&c);
        add(u,v,c);
        add(v,u,c);
    }
    Dijkstra();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逃夭丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值