专题训练——最短路1 A到F题解
A. AtCoder-abc302_f Merge Set
核心思路
将数字和集合都看作图的节点:
- 数字节点:
- 集合节点:
- 若数字 属于集合 ,则在 和 之间连无向边。
问题转化:从数字 走到数字 的最短路径长度 (边数)。
路径经过 个集合节点,则 ,合并这 个集合需要 次操作,所以答案 。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, dist[N * 2];
vector<int> adj[N * 2];
queue<int> que;
void record(int x, int l) {
if (dist[x] != -1) {
return;
}
dist[x] = l;
que.push(x);
}
void bfs() {
record(1, 0);
while (!que.empty()) {
auto now = que.front();
que.pop();
for (auto i : adj[now]) {
record(i, dist[now] + 1);
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
for (int j = 1; j <= a; j++) {
int s;
cin >> s;
adj[i + m].push_back(s);
adj[s].push_back(i + m);
}
}
for (int i = 1; i <= n + m; i++) {
dist[i] = -1;
}
bfs();
cout << dist[m] / 2 - 1;
return 0;
}复杂度
时间复杂度:
空间复杂度:
B. CodeForces-1775D Friendly Spiders
核心思路
将每个数和它的质因子都看作图中的节点:
- 数节点:
- 质因子节点:( 为质数)
若数 含有质因子 ,则在 和 之间连无向边。 这样,两个数若有公共质因子,则它们之间的路径长度为 。
因此从 到 的最短路径边数 d 必为偶数,答案 。 若不可达则输出 。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 5;
int n, a[N], s, t, pre[N], dist[N], v;
vector<int> adj[N], p;
queue<int> q;
void record(int x, int l, int fa) {
if (dist[x] != -1) {
return;
}
q.push(x);
dist[x] = l;
pre[x] = fa;
}
void bfs() {
record(s, 0, 0);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto it : adj[u]) {
record(it, dist[u] + 1, u);
}
}
}
int main() {
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
v = max(v, a[i]);
int x = a[i];
for (int j = 2; j * j <= x; j++) {
if (x % j == 0) {
adj[i].push_back(j + n);
adj[j + n].push_back(i);
for (; x % j == 0; x /= j){
}
}
}
if (x > 1) {
adj[x + n].push_back(i);
adj[i].push_back(x + n);
}
}
cin >> s >> t;
for(int i = 0; i <= n + v; i++){
dist[i] = -1;
}
bfs();
if (dist[t] == -1) {
cout << -1;
return 0;
}
cout << dist[t] / 2 + 1 << '\n';
for (int i = t; i != 0; i = pre[i]) {
p.push_back(i);
}
reverse(p.begin(), p.end());
for (auto it : p) {
if(it <= n){
cout << it << ' ';
}
}
return 0;
}复杂度
时间复杂度:(为最大值)
C. CodeForces-1941G Rudolf and Subway
核心思路
将车站和颜色都视为图中的节点:
- 车站节点:编号
- 颜色节点:编号 (为颜色编号)
对于每条边 颜色为 ,连接 与 和 与 。
这样,乘坐一条颜色为 的线路就相当于从车站走到颜色节点再走到另一车站。
问题转化:从起点车站 到终点车站 的最短路径边数 ,答案 。 若不可达则输出 。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
int t, n, m, dist[N], b, e, c[N];
vector<int> adj[N];
queue<int> que;
void record(int x, int l) {
if (dist[x] != -1) {
return;
}
dist[x] = l;
que.push(x);
}
void bfs() {
record(b, 0);
while (!que.empty()) {
auto now = que.front();
que.pop();
for (auto i : adj[now]) {
record(i, dist[now] + 1);
}
}
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v >> c[i];
adj[u].push_back(c[i] + n);
adj[v].push_back(c[i] + n);
adj[c[i] + n].push_back(u);
adj[c[i] + n].push_back(v);
dist[u] = dist[v] = dist[c[i] + n] = -1;
}
cin >> b >> e;
bfs();
cout << dist[e] / 2 << '\n';
for (int i = 1; i <= n; i++) {
adj[i].clear();
}
for (int i = 1; i <= m; i++) {
adj[c[i] + n].clear();
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> t;
while (t--) {
solve();
}
return 0;
}复杂度
时间复杂度 ,空间 。
D. AtCoder-abc257_f Teleporter Setting
核心思路
将问题分为两类路径:
- 不经过虚拟城市 的路径:直接在原图上行走。 预处理从 到所有点的最短距离 ,从 到所有点的最短距离 。 那么经过某个城市 的路径长度为 。 若关闭城市 ,只需选择 的最小值。
- 经过虚拟城市 0 的路径:形如 。 长度为 。
最终答案为两种候选的最小值。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, INF = 1e9;
int n, m;
int dist1[N], dist2[N];
vector<int> adj[N];
queue<int> que;
void record1(int x, int l) {
if (dist1[x] != -1) {
return;
}
dist1[x] = l;
que.push(x);
}
void record2(int x, int l) {
if (dist2[x] != -1) {
return;
}
dist2[x] = l;
que.push(x);
}
void bfs1(int s){
while (!que.empty()) {
que.pop();
}
fill(dist1, dist1 + n + 1, -1);
record1(s, 0);
while (!que.empty()) {
int u = que.front();
que.pop();
for (int v : adj[u]) {
record1(v, dist1[u] + 1);
}
}
}
void bfs2(int s){
while (!que.empty()) {
que.pop();
}
fill(dist2, dist2 + n + 1, -1);
record2(s, 0);
while (!que.empty()) {
int u = que.front();
que.pop();
for (int v : adj[u]) {
record2(v, dist2[u] + 1);
}
}
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
bfs1(1);
bfs2(n);
for(int i = 1; i <= n; i++){
int ans = INF;
if (dist1[n] != -1) {
ans = min(ans, dist1[n]);
}
if (dist1[i] != -1 && dist2[0] != -1) {
ans = min(ans, dist1[i] + dist2[0]);
}
if (dist1[0] != -1 && dist2[i] != -1) {
ans = min(ans, dist1[0] + dist2[i]);
}
if (ans != INF) {
cout << ans << ' ';
} else {
cout << -1 << ' ';
}
}
return 0;
}时间复杂度:。
E. AtCoder-abc244_f Shortest Good Path
核心思路
因为 很小,可以用状态压缩 求解。
- 状态:,表示当前已经访问过的顶点集合为 ,且当前位于顶点 。
- 初始状态:对于每个顶点 ,状态 的距离为 。
- 转移:从当前状态 ,走到邻接点 ,新状态为 ,距离 。
- 保证每个状态第一次访问时的距离就是最短路径长度。
对所有非空 ,取所有 的最小 ,累加即为答案。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 18, M = (1 << 18) + 1, INF = 1e9;
int n, m;
struct Node{
int s, u;
};
vector<int> adj[N];
int dist[M][N];
queue<Node> que;
void record(int s, int x, int l) {
if (dist[s][x] != -1) {
return;
}
dist[s][x] = l;
que.push({s, x});
}
void bfs(){
while (!que.empty()) {
auto now = que.front();
que.pop();
int u = now.u, s = now.s;
for (int v : adj[u]) {
record(s ^ (1 << (v - 1)), v, dist[s][u] + 1);
}
}
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < M; i++) {
for(int j = 0; j < N; j++){
dist[i][j] = -1;
}
}
for (int i = 1; i <= n; i++) {
record(1 << (i - 1), i, 1);
}
bfs();
int ans = 0;
for (int i = 0; i < (1 << n); i++) {
if (i == 0) {
continue;
}
int sum = INF;
for (int j = 1; j <= n; j++) {
if (dist[i][j] != -1) {
sum = min(sum, dist[i][j]);
}
}
ans += (sum == INF ? 0 : sum);
}
cout << ans;
return 0;
}复杂度
时间复杂度:
空间复杂度:
F. AtCoder-abc376_d Cycle
核心思路
要形成一个经过 的环,只需满足:存在一条从 到某个顶点 u 的路径,且图中有一条边 。 那么环的长度 。
目标:找到所有满足 的顶点 u,取 的最小值。
- 先用 从 出发,计算到所有顶点的最短距离 。
- 遍历所有边 ,如果 且 可达,则候选答案为 。
- 取最小值,若没有候选则输出 。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, INF = 1e9;
int n, m, dist[N];
vector<int> adj[N], s;
queue<int> que;
void record(int x, int l) {
if (dist[x] != INF) {
return;
}
dist[x] = l;
que.push(x);
}
void bfs() {
record(1, 0);
while (!que.empty()) {
auto now = que.front();
que.pop();
for (auto i : adj[now]) {
record(i, dist[now] + 1);
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
if (v == 1) {
s.push_back(u);
}
}
for (int i = 0; i <= n; i++) {
dist[i] = INF;
}
bfs();
int ans = INF;
for (int it : s) {
if (dist[it] != -1) {
ans = min(ans, dist[it]);
}
}
cout << (ans == INF ? -1 : ans + 1);
return 0;
}复杂度
- BFS:
- 扫描边:
- 总时间 ,空间 。