poj 3255 Roadblocks(次最短路径)

本文介绍了一种寻找图中两点间次短路径的算法实现。通过不断更新最短及次短路径,确保找到满足条件的路径。该算法适用于允许重复经过同一顶点或边的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Roadblocks
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10836 Accepted: 3852

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)

题目大意:找到从1->N的次最短路径
次最短路的求解:
到某个顶点的次最短路
1、到其他某个顶点u的最短路+u->v的边
2、到某个定点的次最短路+u->v的边
不断更新最短/次最短路的数组


#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <algorithm>
#include <map>
#include <math.h>
#include <stack>
#include <queue>
#define Max 6000
#define inf 100000000
using namespace std;
int N,R;
struct edge{
    int to;
    int cost;
};
vector<edge> G[Max];
int dis[Max];
int dis2[Max];
typedef pair<int, int> P;
void solve(){
    priority_queue<P,vector<P>,greater<P> > q;
    fill(dis, dis+N, inf);
    fill(dis2, dis2+N, inf);
    dis[0] = 0;
    q.push(P(0,0));
    while (!q.empty()) {
        P p = q.top();q.pop();
        int v = p.second, d = p.first;
        if (dis2[v]<d) {
            continue;
        }
        for (int i=0; i<G[v].size(); i++) {
            edge &e = G[v][i];
            int d2 = e.cost + d;
            if (dis[e.to]>d2) {
                swap(dis[e.to], d2);
                q.push(P(dis[e.to],e.to));
            }
            if (dis2[e.to]>d2&&dis[e.to]<d2) {
                dis2[e.to] = d2;
                q.push(P(dis2[e.to],e.to));
            }
        }
    }
    printf("%d\n",dis2[N-1]);
}
int main(){
    while (scanf("%d%d",&N,&R)!=EOF) {
        for (int i=0; i<=N; i++) {
            G[i].clear();
        }
        for (int i=0; i<R; i++) {
            int s,e,c;
            scanf("%d%d%d",&s,&e,&c);
            s--,e--;
            edge p;
            p.to = e,p.cost = c;
            G[s].push_back(p);
            p.to = s;
            G[e].push_back(p);
        }
        solve();
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值