POJ3255 Roadblocks [Dijkstra,次短路]

  题目传送门

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

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)


  分析:

  一句话题意,次短路模板。

  一般来说做次短路可以通过先跑一遍最短路,然后记录路径,在最短路路径上每次删去其中一条边,然后再跑一遍最短路求出次短路。但是有一种更加简洁快速的方法,用$Dijkstra$一次求出最短路与次短路。

  思路非常简单,在求最短路的同时开另外一个数组记录次短路,每次被更新的最短路就可以更新到次短路里面,或者是更新的路径比最短路长但比当前记录的次短路要短时也要更新。不过还有一些细节要注意,具体可以看代码。

  Code:

//It is made by HolseLee on 17th Aug 2018
//POJ3255
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<queue>
#include<algorithm>
#define Max(a,b) (a)>(b)?(a):(b)
#define Min(a,b) (a)<(b)?(a):(b)
#define Swap(a,b) (a)^=(b)^=(a)^=(b)
using namespace std;

const int N=5005;
const int M=1e5+7;
typedef pair<int,int> P;
int n,m,head[N],siz,dis[N],dist[N];
struct Node{
    int to,val,nxt;
}edge[M<<1];
priority_queue<P,vector<P>,greater<P> > T;

inline int read()
{
    char ch=getchar();int num=0;bool flag=false;
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)flag=true;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){num=num*10+ch-‘0‘;ch=getchar();}
    return flag?-num:num;
}

inline void add(int x,int y,int z)
{
    edge[++siz].to=y;
    edge[siz].val=z;
    edge[siz].nxt=head[x];
    head[x]=siz;
}

void dijkstra()
{
    memset(dis,0x7f,sizeof(dis));
    memset(dist,0x7f,sizeof(dist));
    dis[1]=0;
    T.push(P(1,0));
    int x,y,d,dt;
    while(!T.empty()){
        x=T.top().first,d=T.top().second;T.pop();
        if(dist[x]<d)continue;
        for(int i=head[x];i!=-1;i=edge[i].nxt){
            y=edge[i].to;
            dt=d+edge[i].val;
            if(dis[y]>dt){
                Swap(dis[y],dt);
                T.push(P(y,dis[y]));
            }
            if(dist[y]>dt&&dis[y]<dt){
                dist[y]=dt;
                T.push(P(y,dist[y]));
            }
        }
    }
}

int main()
{
    n=read();m=read();
    int x,y,z;
    memset(head,-1,sizeof(head));
    for(int i=1;i<=m;++i){
        x=read(),y=read(),z=read();
        add(x,y,z);add(y,x,z);
    }
    dijkstra();
    printf("%d\n",dist[n]);
    return 0;
}

原文地址:https://www.cnblogs.com/cytus/p/9494875.html

时间: 2024-11-10 18:04:21

POJ3255 Roadblocks [Dijkstra,次短路]的相关文章

POJ3255 Roadblocks 【次短路】

Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7760   Accepted: 2848 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 q

POJ3255:Roadblocks(次短路 SPFA+A星)

给出1-N 个点 的距离, 求从1号到N号的次短路, 直接用k短路来做了,,dj会TLE, 用spfa就过了 题目: I - RoadblocksTime Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Bessie has moved to a small farm and sometimes enjoys returning to visit o

poj 1062 昂贵的聘礼 (dijkstra最短路)

题目链接:http://poj.org/problem?id=1062 昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36799   Accepted: 10616 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:"嗯,如果你能够替我弄到大祭司的

uva 11374 最短路+记录路径 dijkstra最短路模板

UVA - 11374 Airport Express Time Limit:1000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description ProblemD: Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to t

hdu 1690 Bus System(Dijkstra最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6569    Accepted Submission(s): 1692 Problem Description Because of the huge popula

poj 2253 Frogger (dijkstra最短路)

题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25773   Accepted: 8374 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on an

POJ3255 Roadblocks 次短路Dijkstra做法

有段时间没做题了,这几天一直在寻找感觉,尽量多看书,这题目就是n个地方,编号从1到n,然后有r条路,问你从1号到达n号地方的次短路长度为多少,同一条边可以重复走,直接在dijkstra算法里同时记录一个次短路就可以了,但是一直WA,后来去看了讨论面板,那里有人给了测试数据,我不知道那数据的对错,但是干扰了我很久,也许那些数据实在题目案例之外的吧,对我的程序没有任何影响,我只是太久没错 一时 忘了双向边了,一开始只建立了单向边~代码写的也比较冗长~ int n,r; typedef struct

poj3255 Roadblocks 次短路

Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10098   Accepted: 3620 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

【啊哈!算法】算法7:Dijkstra最短路算法

上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短路”.本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径”.例如求下图中的1号顶点到2.3.4.5.6号顶点的最短路径. <ignore_js_op> 与Floyd-Warshall算法一样这里仍然使用二维数组e来存储顶点之间边的关系,初始值如下. <ignore_js_op> 我们还需要用一个一维数组dis来存储1号顶点到其余各个顶点的初始路程,如下.