You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.3 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
2 years ago
const int M = N * 2;
2 years ago
int n; // 总共有N个车站
int m; // 开通了M条单程巴士线路
int h[N], e[M], w[M], ne[M], idx;
2 years ago
int dis[N]; // 最小距离数组
2 years ago
int stop[N]; // 站点数组
bool st[N]; // 是否在队列中
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
// 求1号点到n号点的最短路距离如果从1号点无法走到n号点则返回-1
void dijkstra() {
2 years ago
memset(dis, 0x3f, sizeof dis); // 求最小设最大
dis[1] = 0; // 1到自己乘车数0
2 years ago
priority_queue<PII, vector<PII>, greater<PII>> q; // 小顶堆
q.push({0, 1}); // 1号入队列
while (q.size()) {
auto t = q.top();
q.pop();
int u = t.second;
2 years ago
// int d = t.first; // 此处 d=t.first没有用上经测试其实d=dis[u],用哪个都是一样的
2 years ago
if (st[u]) continue;
st[u] = true;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
2 years ago
if (dis[v] > dis[u] + w[i]) {
dis[v] = dis[u] + w[i];
q.push({dis[v], v});
2 years ago
}
}
}
}
int main() {
memset(h, -1, sizeof h); // 初始化邻接表
2 years ago
cin >> m >> n; // 总共有N个车站,开通了M条单程巴士线路
2 years ago
while (m--) { // m条边
// ① 先读入第一个数字
int cnt = 0; // cnt一定要清零
2 years ago
cin >> stop[++cnt];
2 years ago
char ch = getchar();
while (ch == ' ') {
// ② 读入其它数字
2 years ago
cin >> stop[++cnt]; // 还有就继续读
ch = getchar(); // 为下一次做准备
2 years ago
}
// 这个建图建的妙啊!
// 通过多条边成功映射了问题将一趟车问题转化为多个点之间边是1问题
for (int i = 1; i <= cnt; i++)
for (int j = i + 1; j <= cnt; j++)
add(stop[i], stop[j], 1);
}
dijkstra();
2 years ago
if (dis[n] == INF)
2 years ago
puts("NO");
else
2 years ago
printf("%d\n", dis[n] - 1);
2 years ago
return 0;
}