1 条题解
-
0
参考答案:
#include<iostream> #include<string> #include<queue> #include<cstring> using namespace std; const int N = 510; int g[N][N]; int a[N]; int dis[N]; int n, m, cnt; string s; int res; void bfs() { memset(dis, 0x3f, sizeof(dis)); dis[1] = 0; queue<int> q; q.push(1); while (q.size()) { int t = q.front(); q.pop(); for (int i = 1; i <= n; ++i) { if (dis[i] > dis[t] + g[t][i]) { dis[i] = dis[t] + g[t][i]; q.push(i); } } } } int main() { memset(g, 0x3f, sizeof(g)); cin >> m >> n; getline(cin, s); while (m--) { if (s.size()) s.clear(); getline(cin, s); int t = 0; cnt = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] != ' ') t = t * 10 + (s[i] - '0'); else { a[cnt++] = t; t = 0; } } if (t) a[cnt++] = t; for (int i = 0; i < cnt; ++i) { for (int j = i + 1; j < cnt; ++j) { g[a[i]][a[j]] = 1; } } } bfs(); if (dis[n] == 0x3f3f3f3f) cout << "NO" << endl; else cout << max(0, dis[n] - 1) << endl; return 0; }
- 1
信息
- ID
- 5362
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 4
- 已通过
- 1
- 上传者