当前位置: 首页 > news >正文

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 debugstruct 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 debugfreopen("0.txt", "r", stdin);freopen("1.txt", "w", stdout);#endifwhile (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 debugfclose(stdin);fclose(stdout);#endifreturn 0;
}

http://www.lryc.cn/news/276775.html

相关文章:

  • 计算机网络(超级详细笔记)
  • 老杨说运维 | 年末大讲回顾:运维的尽头也是大模型吗?
  • Unity 利用UGUI之Scrollbar制作进度条
  • MySQL之导入、导出
  • 【unity小技巧】FPS游戏实现相机的偏移震动、武器射击后退和后坐力效果
  • MINCO+汽车
  • 大模型机器人发展史:从VoxPoser、RT2到斯坦福Mobile ALOHA、Google机器人
  • Ubunutu18.04 ROS melodic 无人机 XTDrone PX4 Vins-Fuison 运行配置
  • Linux 常见服务配置
  • Flutter基础
  • MySQL-数据库概述
  • HTML---JQurey的基本使用
  • 搜索docker镜像
  • 旋变检测AD2s1205手册学习笔记
  • 【温故而知新】JavaScript的防抖与节流
  • C++模板——(3)类模板
  • 深度学习中Epoch和Batch Size的关系
  • Python采集微博评论做词云图
  • 一文详解VScode 的远程开发
  • 捕捉“五彩斑斓的黑”:锗基短波红外相机的多种成像应用
  • 解读 Sobit v2:铭文资产跨链更注重安全、易用性
  • [开源]万界星空开源MES系统,支持低代码大屏设计
  • 开源软件运维安全防护的六个手段
  • 开启Android学习之旅-5-Activity全屏
  • 运行时类型信息 typeid、type_info...(C++)
  • 2023-12-02 青少年软件编程(C语言)等级考试试卷(七级)解析
  • 计算机网络-以太网交换基础
  • C++系列十六:枚举
  • flask web学习之flask与http(四)
  • 电子签章Java后端与前端交互签名位置计算