luogu 2865 [USACO06NOV]路障Roadblocks (次短路 堆优化Dijkstra)

91 篇文章 1 订阅
77 篇文章 8 订阅

题目描述

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

贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友。贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样,选择最短路。 贝茜所在的乡村有R(1<=R<=100,000)条双向道路,每条路都联结了所有的N(1<=N<=5000)个农场中的某两个。贝茜居住在农场1,她的朋友们居住在农场N(即贝茜每次旅行的目的地)。 贝茜选择的第二短的路径中,可以包含任何一条在最短路中出现的道路,并且,一条路可以重复走多次。当然咯,第二短路的长度必须严格大于最短路(可能有多条)的长度,但它的长度必须不大于所有除最短路外的路径的长度。

输入输出格式

输入格式:

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)

 

输出格式:

Line 1: The length of the second shortest path between node 1 and node N

 

输入输出样例

输入样例#1:

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

输出样例#1:

450

说明

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

题目链接:https://www.luogu.org/problemnew/show/P2865

题目分析:由于同一个边可以走多次,故去掉vis数组的限制,求最短路的过程中同时记录最短路和次短路,注意更新时的条件,还要注意的是起点的次短路不是0!

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
int const INF = 0x3fffffff;
int const MAXN = 5005;
int const MAXM = 1e5 + 5;
int n, m, dis[MAXN][2]; 
int cnt, head[MAXN];

struct EDGE {
    int to, w, nxt;
}e[MAXM << 1];

priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > q;

void Init() {
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void Add(int u, int v, int w) {
    e[cnt].to = v;
    e[cnt].w = w;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}

void SlackPath(int v, int newDis) {
    if (dis[v][0] > newDis) {
        dis[v][1] = dis[v][0];
        dis[v][0] = newDis;
        q.push(make_pair(newDis, v));
    } else if (dis[v][0] < newDis && newDis < dis[v][1]) {
        dis[v][1] = newDis;
        q.push(make_pair(newDis, v));
    }
}

void HeapDijkstra(int v0) {
    for (int i = 1; i <= n; i++) {
        dis[i][0] = INF; 
        dis[i][1] = INF;
    }
    dis[v0][0] = 0;
    q.push(make_pair(0, v0));
    while (!q.empty()) {
        int u = q.top().second;
        q.pop();
        for (int i = head[u]; i != -1; i = e[i].nxt) {
            int v = e[i].to;
            SlackPath(v, dis[u][0] + e[i].w);
            SlackPath(v, dis[u][1] + e[i].w);
            // printf("dis[%d][0] = %d  dis[%d][1] = %d\n", v, dis[v][0], v, dis[v][1]);
        }
    }
}

int main() {
    Init();
    int u, v, w;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d %d %d", &u, &v, &w);
        Add(u, v, w);
        Add(v, u, w);   
    }
    HeapDijkstra(1);
    printf("%d\n", dis[n][1]);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值