1599 - Ideal Path (UVA)

题目链接如下:

https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=448&page=show_problem&problem=4474

这道题也是看了刘汝佳的思路才写出来的....

代码如下:

cpp 复制代码
#include <cstdio>
#include <deque>
#include <vector>
#include <algorithm>
#include <map>
const int maxx = 100005;
const int maxColor = 1e9 + 1;
// #define debug

struct node{
    int id, color;
    node(int _id, int _color): id(_id), color(_color){}
};
int n, m, u, v, c, k, curr, minn;
std::map<int, std::vector<node>> mp;
int d[maxx], pre[maxx], preColor[maxx];
bool vis[maxx];

void bfs1(){
    std::deque<int> dq;
    dq.push_back(n);
    d[n] = 0;
    vis[n] = true;
    while (!dq.empty()){
        curr = dq.front();
        dq.pop_front();
        for (int i = 0; i < mp[curr].size(); ++i){
            int temp = mp[curr][i].id;
            if (!vis[temp]){
                d[temp] = d[curr] + 1;
                vis[temp] = true;
                dq.push_back(temp);
            }
        }
    }
}

void bfs2(){
    std::deque<int> dq;
    dq.push_back(1);
    while (!dq.empty()){
        curr = dq.front();
        dq.pop_front();
        minn = maxColor;
        for (int i = 0; i < mp[curr].size(); ++i){
            int temp = mp[curr][i].id;
            if (d[temp] == d[curr] - 1){
                minn = std::min(minn, mp[curr][i].color);
            }
        }
        for (int i = 0; i < mp[curr].size(); ++i){
            int temp = mp[curr][i].id;
            if (d[temp] == d[curr] - 1 && mp[curr][i].color == minn){
                if (!pre[temp]){
                    dq.push_back(temp);
                }
                if (!pre[temp] || preColor[temp] > minn){
                    pre[temp] = curr;
                    preColor[temp] = minn;
                }
            }
        }
    }
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (scanf("%d %d", &n, &m) == 2){
        mp.clear();
        std::fill(d, d + n + 1, -1);
        std::fill(vis, vis + n + 1, false);
        std::fill(pre, pre + n + 1, 0);
        std::fill(preColor, preColor + n + 1, -1);
        while (m--){
            scanf("%d %d %d", &u, &v, &c);
            if (u != v){
                mp[u].push_back(node(v, c));
                mp[v].push_back(node(u, c));
            }
        }
        bfs1();
        bfs2();
        std::vector<int> path;
        curr = n;
        do {
            path.push_back(preColor[curr]);
            curr = pre[curr];
        } while (curr != 1);
        reverse(path.begin(), path.end());
        printf("%d\n", path.size());
        for (int i = 0; i < path.size(); ++i){
            printf("%d%s", path[i], i == path.size() - 1 ? "\n" : " ");
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
相关推荐
闻缺陷则喜何志丹12 天前
【并集查找】P10729 [NOISG 2023 Qualification] Dolls|普及+
c++·算法·图论·洛谷·并集查找
CodeWithMe12 天前
【Algorithm】图论入门
c++·图论
东方芷兰14 天前
Leetcode 刷题记录 13 —— 图论
算法·leetcode·图论
蒙奇D索大16 天前
【数据结构】图论实战:DAG空间压缩术——42%存储优化实战解析
数据结构·笔记·学习·考研·图论·改行学it
蒙奇D索大19 天前
【数据结构】图论最短路圣器:Floyd算法如何用双矩阵征服负权图?
数据结构·算法·矩阵·图论·图搜索算法
芜湖xin20 天前
【题解-洛谷】B4292 [蓝桥杯青少年组省赛 2022] 路线
算法·图论·bfs·图的遍历
LunaGeeking21 天前
重要的城市(图论 最短路)
c++·算法·编程·图论·最短路·floyd
闻缺陷则喜何志丹21 天前
【强连通分量 拓扑序】P9431 [NAPC-#1] Stage3 - Jump Refreshers|普及+
c++·算法·图论·拓扑序·洛谷·强连通分量
蒙奇D索大24 天前
【数据结构】图论最短路径算法深度解析:从BFS基础到全算法综述
数据结构·算法·图论·广度优先·图搜索算法
计信金边罗1 个月前
是否存在路径(FIFOBB算法)
算法·蓝桥杯·图论