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;
}
相关推荐
CodeWizard~1 天前
AtCoder Beginner Contest 430赛后补题
c++·算法·图论
天选之女wow2 天前
【代码随想录算法训练营——Day58】图论——117.软件构建、47. 参加科学大会
算法·图论
earthzhang20212 天前
【2051】【例3.1】偶数
开发语言·数据结构·算法·青少年编程·图论
apcipot_rain3 天前
CSP集训错题集 第八周 主题:基础图论
算法·图论
天选之女wow3 天前
【代码随想录算法训练营——Day57(Day56周日休息)】图论——53.寻宝
算法·图论
极客数模3 天前
2025年(第六届)“大湾区杯”粤港澳金融数学建模竞赛准备!严格遵循要求,拿下大奖!
大数据·python·数学建模·金融·分类·图论·boosting
岑梓铭3 天前
《考研408数据结构》第七章(6.1~6.3图的概念、存储方式、深/广度遍历)复习笔记
数据结构·笔记·考研·算法·图论·408·ds
天选之女wow3 天前
【代码随想录算法训练营——Day53】图论——110.字符串接龙、105.有向图的完全可达性、106.岛屿的周长
算法·深度优先·图论
武子康4 天前
Java-165 Neo4j 图论详解 欧拉路径与欧拉回路 10 分钟跑通:Python NetworkX 判定实战
java·数据库·性能优化·系统架构·nosql·neo4j·图论
极客数模4 天前
【浅析赛题,一等奖水平】思路模型数据相关资料!2025 年“大湾区杯”粤港澳金融数学建模竞赛B 题 稳定币的综合评价与发展分析~
大数据·算法·数学建模·金融·数据挖掘·图论·1024程序员节