社区讨论
求思路
P8817[CSP-S 2022] 假期计划参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mhjrxkuk
- 此快照首次捕获于
- 2025/11/04 07:31 4 个月前
- 此快照最后确认于
- 2025/11/04 07:31 4 个月前
CPP
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n, m, k;
const int N = 3000;
LL score[N];
vector<LL> G[N];
LL d[N][N];
void bfs(int start) {
queue<int> q;
for (int i = 1; i <= n; i ++)
d[start][i] = -1; //全部初始化为-1
d[start][start] = 0; //起点到起点的距离100%是0
q.push(start); //把起点放入队列
while (q.size()) {
int f = q.front();
q.pop();
if (d[start][f] == k) //如果从起点到这个位置已经有了k次转乘便跳过
continue;
for (int i = 0; i < G[f].size(); i ++) { //遍历队首的“邻居”
int t = G[f][i]; //获取队首元素的“邻居”
if (d[start][t] == -1) { //倘若队首的“邻居”尚没有被赋值,就将“邻居”入队,并且从起点到达“邻居”的步数是从起点到队首的步数 + 1
q.push(t);
d[start][t] = d[start][f] + 1;
}
}
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m >> k;
k ++;
for (int i = 2; i <= n; i ++)
cin >> score[i];
while (m --) {
//邻接表存储图
LL x, y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
for (int i = 1; i <= n; i ++)
bfs(i); //每个点去寻找自己能在k次转乘内去的地方
//求思路后面不会写了,只能最后想出BFS
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...