poj 3967 Ideal Path 字典序最小最短路

本文介绍了一种寻找图中无权最短路径中字典序最小路径的方法。通过对反向BFS和正向BFS的应用,结合最短路径树的构建,实现了高效的算法解决方案。

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

题目

题目链接:http://poj.org/problem?id=3967

题目来源:群中一人问的,卡了很久才A了。

简要题意:找到无权最短路(即权看作 1 )中字典序最小的(按照路径的边权序列)。

数据范围:2n100000;1m200000Edge(ai,bi) weighed ci1ai,bin;1ci109

题解

这题跪了很多遍,中间重写了很多次。

首先肯定是要bfs求下最短路的。

然后在最短路中选取字典序最小的即可,但是整个东西比较难处理。

做法是进行反向bfs然后再正向bfs构建出序列。

反向的原因是正向扫的话没法确定节点是否不经过 1 可达n,而反向的话是可以确定在经过那些没法直接达到 n 的点之前会经过1

正向的话一样还要反向一次才能构建出最短路径树。

反向bfs后正向再扫一遍,构建出字典序最小的就行了,通过 dis[cur]1=dis[nxt] 来判断是否在最短路上。

实现

实际上这题的数据量比较大,对于两次bfs的要求都是需要完完全全的线性,也就是每个节点一次。

反向的比较好处理,可以用距离数组直接标记。

正向我开始是只有个结果的数组,但是TLE了几次,然后之后经过改进对每个节点搞个数组记录走到某节点前一步的边的最小值(最短路径树上),通过这个数组可以只入队一次,然后之后判断走的上一步是不是字典序最小的就行了。

读入数据比较大,使用外挂效果显著,不用外挂 8.2 秒,用了输入挂是 3.5 秒,加上输出挂 2.6 秒。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
struct Edge {
    int to, nxt, c;
    Edge(int to, int nxt, int c) : to(to), nxt(nxt), c(c) {}
    Edge() {}
};

const int N = 1E5+5;
const int M = N*4;
const int INF = 0x3f3f3f3f;

int head[N];
Edge e[M];

inline void addEdge(int from, int to, int c, int cnt) {
    e[cnt] = Edge(to, head[from], c);
    head[from] = cnt;
}

int dis[N];
void bkbfs(int n) {
    dis[n] = 1;
    queue<int> q;
    q.push(n);
    int cur, nxt;
    while (!q.empty()) {
        cur = q.front();
        q.pop();
        if (cur == 1) continue;
        for (int i = head[cur]; ~i; i = e[i].nxt) {
            nxt = e[i].to;
            if (!dis[nxt]) {
                dis[nxt] = dis[cur] + 1;
                q.push(nxt);
            }
        }
    }
}
int res[N];
int pre[N];

void solve(int n) {
    memset(res, INF, sizeof res);
    memset(pre, INF, sizeof pre);
    queue<int> q;
    q.push(1);
    int cur, nxt;
    while (!q.empty()) {
        cur = q.front();
        q.pop();
        if (pre[cur] > res[dis[cur]] || cur == n) continue;
        for (int i = head[cur]; ~i; i = e[i].nxt) {
            nxt = e[i].to;
            if (dis[cur] == dis[nxt]+1 && e[i].c <= res[dis[nxt]]) {
                if (pre[nxt] == INF) q.push(nxt);
                pre[nxt] = min(pre[nxt], e[i].c);
                res[dis[nxt]] = e[i].c;
            }
        }
    }
}

const int MX = 25;
char out[MX+1];
template<typename T>
inline char* wt(T x) {
    int cnt = 0;
    if (x == 0) out[MX-++cnt] = '0';
    while (x) out[MX-++cnt] = x%10+'0', x /= 10;
    out[MX] = '\0';
    for (int i = MX-cnt; i < MX; i++) {
        putchar(out[i]);
    }
    return out+MX-cnt;
}

void p(int n) {
    printf("%d\n", dis[1]-1);
    for (int i = dis[1]-1; i > 0; i--) {
        wt(res[i]);
        putchar(i==1?'\n':' ');
    }
}
template <class T>
inline bool rd(T &x) {
    char c;
    while (c<'0'||c>'9') {
        if (c=getchar(), c==EOF) return false;
    }
    x = c-'0';
    while (c=getchar(), c>='0'&&c<='9') x=x*10+(c-'0');
    return true;
}

int main() {
    int n, m, u, v, c;
    while (scanf("%d%d", &n, &m) == 2) {
        memset(head, -1, sizeof head);
        int cnt = 0;
        for (int i = 0; i < m; i++) {
            rd(u), rd(v), rd(c);
            addEdge(u, v, c, cnt++);
            addEdge(v, u, c, cnt++);
        }
        bkbfs(n);
        solve(n);
        p(n);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值