MnZn 乃大龙 90pts 求条
查看原帖
MnZn 乃大龙 90pts 求条
1378591
Barewalk楼主2025/1/23 15:46

In

9 42 47
48 3 7 14 17 20 42
1 48
6 7 11 12 13 14 15 17 22 23 33 34 37 40 41 47 50

myOut

3

expect

2

code

#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#define maxN 550
using namespace std;
int n, m;
int head[maxN], nxt[maxN * maxN * 105], to[maxN * maxN * 105], cnt;
void addEdge(int x, int y) {
    nxt[++ cnt] = head[x], head[x] = cnt, to[cnt] = y;
}
int a[maxN], tot;
int Input() {
    string s;
    getline(cin, s); s += ' ';
    int x = 0; tot = 0;
    for (int i = 0; i < s.size(); ++ i) {
        if (!isdigit(s[i])) a[++ tot] = x, x = 0;
        else x = (x << 1) + (x << 3) + s[i] - '0';
    }
    return tot;
}
int dist[maxN], vis[maxN];
priority_queue<pair<int, int> > q;
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> m >> n;
    for (int i = 0; i <= m ; ++ i) {
        tot = Input();
        for (int j = 1; j <= tot; ++ j) {
            for (int k = j + 1; k <= tot; ++ k)
                addEdge(a[j], a[k]);
        }
    }
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    q.push({0, 1});
    while (!q.empty()) {
        int x = q.top().second;
        q.pop();
        if (vis[x]) continue;
        vis[x] = true;
        for (int i = head[x]; i; i = nxt[i]) {
            int v = to[i];
            if (!vis[v] && dist[x] + 1 < dist[v]) {
                dist[v] = dist[x]  + 1;
                q.push({dist[v], v});
            }
        }
    }
    if (dist[n] == 0x3f3f3f3f) puts("NO");
    else cout << dist[n] - 1 << '\n';
    return 0;
}
2025/1/23 15:46
加载中...