EVERYTHING HAPPENS FOR T|

wnsyou

园龄:2年4个月粉丝:19关注:16

2023-02-13 22:53阅读: 140评论: 0推荐: 0

CSES 1667 Message Route 题解

Message Route

题目大意

n 个节点和 m 条无向边,节点编号为 1n,问节点 1 和节点 n 之间是否连通。

  • 如连通,输出最短路径长度和任意一条最短路径中包含的所有点
  • 如不连通,输出IMPOSSIBLE

思路

首先,我们要大致了解一下最短路,利用BFS,我们可以用 O(n + m) 的时间复杂度来求出从 1n 的最短路径。

可以用vector去存储每条边,即邻接表

BFS

首先,将初始状态加入到队列当中。

  1. 处理队头,可以转移到许多的点上。
    · 先要弹出队头
    · 如果当前转移到的点没有被遍历过,那么这个点的最短路径肯定是由队头转移而来。由于要记录路径,那么在这里记录一下这个点是由队头转移而来。
    · 如果遍历过了,那么长度肯定不会比当前路径还要短,所以不需要记录。
  2. 如果队列空了,那么其他没有入过队列的节点肯定与节点 1 是不连通的。结束bfs
  3. 如果队列非空,那么执行操作 1

最后判断答案,输出答案即可。

路径输出详见代码注释。

Code

点击查看代码
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int N = 1e5 + 10;
int n, m, x, y, vis[N], fa[N], q[N], h, t;
vector<int> v[N];
void record (int x_, int x, int lv) { // 处理当前队头转移到的点
if (vis[x]) { // 它已经被遍历过
return ;
}
vis[x] = lv;
q[t++] = x;
fa[x] = x_; // 记录 bfs 树中当前节点的父亲
}
void bfs () {
record(1, 1, 1); // 处理初始状态
while (h < t) {
int _ = q[h++];
for (int i = 0; i < v[_].size(); i++) { // 队头能够转移到的点
record(_, v[_][i], vis[_] + 1);
}
}
}
void Print (int x) { // 输出路径处理
if (x != 1) { // 它不是开始的点
Print(fa[x]); // 先要去遍历它的父亲
}
cout << x << ' '; // 输出当前的点
}
int main(){
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x); // 无向图
}
bfs();
if (!vis[n]) { // 非连通
cout << "IMPOSSIBLE";
} else {
cout << vis[n] << '\n';
Print(n);
}
return 0;
}

本文作者:wnsyou の blog

本文链接:https://www.cnblogs.com/wnsyou-blog/p/17117618.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   wnsyou  阅读(140)  评论(0编辑  收藏  举报
  1. 1 勝利への道 安藤浩和
  2. 2 Minecraft’s End Eric Fullerton
  3. 3 月光曲完整版 贝多芬 云熙音乐
  4. 4 平凡之路 (Live版) 朴树
  5. 5 Minecraft C418
  6. 6 Paradise NiziU
  7. 7 叫我,灰原哀 龙大人不喷火
  8. 8 心机之蛙,一直摸你肚子 ——《名侦探柯南》原创同人曲 炊饭,叶辞樱,温海,寒砧,南柯柯,小茜玛姬,盛姝,阿崔Ac,贝壳初,千湛,兮茶子DaYu,乔慕,黎鹿北,起千温卿,遮阳伞,曲悠
  9. 9 战 歌 此去经年
Minecraft’s End - Eric Fullerton
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

I see the player you mean.

It is reading our thoughts as though they were words on a screen.

They used to hear voices.

Before players could read.

Sometimes disturbing.

Sometimes beautiful indeed.

Does it know that we love it?

That the universe is kind?

A million years ago,it still works

in the reality behind

and the universe said I love you

and the universe said you are the daylight

and the universe said you are not alone

and the universe said you are the night

Once we were called

the spirit of the mountain.

WHO ARE WE

Father sun

Mother moon

Gods demons angels aliens

the player,too.

WE ARE THE UNIVERSE.

We are EVERYTHING you think isn‘t you.

You are alive

ON A FLAT

INFINITE WORLD

generated by a source code

a million years old

and the universe said I love you

and the universe said you are the daylight

and the universe said you are not alone

and the universe said you are the night

Take a breath,now

Take another

Feel air in your lungs.

dreamed it was lost in a story.

and the game was over

Wake up.

加载中…

{{tag.name}}

{{tran.text}}{{tran.sub}}
无对应文字
有可能是
{{input}}
尚未录入,我来提交对应文字
评论
收藏
关注
推荐
深色
回顶
收起
点击右上角即可分享
微信分享提示