1599 - Ideal Path (UVA)

题目链接如下:

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

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

代码如下:

#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;
}

相关推荐

  1. 1599 - Ideal Path (UVA)

    2024-01-06 09:02:01       63 阅读
  2. P1595 信封问题

    2024-01-06 09:02:01       51 阅读
  3. KY199 查找

    2024-01-06 09:02:01       44 阅读
  4. P1597 语句解析(C++)

    2024-01-06 09:02:01       53 阅读
  5. CODEFORCES --- 1399A.Remove Smallest

    2024-01-06 09:02:01       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-06 09:02:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-06 09:02:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-06 09:02:01       82 阅读
  4. Python语言-面向对象

    2024-01-06 09:02:01       91 阅读

热门阅读

  1. 使用OHOS SDK构建assimp

    2024-01-06 09:02:01       71 阅读
  2. Unity常见错误合集

    2024-01-06 09:02:01       52 阅读
  3. c++ 中多线程的使用

    2024-01-06 09:02:01       55 阅读
  4. K8S学习指南(64)-K8S源代码走读之Kubelet

    2024-01-06 09:02:01       54 阅读
  5. 百度测试开发实习生 一面

    2024-01-06 09:02:01       60 阅读
  6. LeetCode 30. 串联所有单词的子串

    2024-01-06 09:02:01       54 阅读
  7. 代码设计模式

    2024-01-06 09:02:01       60 阅读
  8. 设计模式之单例模式

    2024-01-06 09:02:01       62 阅读
  9. Hive 源码

    2024-01-06 09:02:01       53 阅读