#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector <pair <int, int> > z[N];
int dis[N], id[N];
bool inque[N];
int n, p, cc;
void spfa(int s)
{
memset (dis, 0x3f, sizeof dis);
queue <int> q;
q.push(s);
inque[s] = true;
dis[s] = 0;
while (q.size())
{
int f = q.front();
q.pop();
inque[f] = false;
for (auto &u : z[f])
if (dis[f] + u.second < dis[u.first])
{
dis[u.first] = dis[f] + u.second;
if (!inque[u.first])
{
inque[u.first] = true;
q.push(u.first);
}
}
}
}
signed main()
{
int m, n;
cin >> m >> n;
cin.get();
string s;
while (m --)
{
getline(cin, s);
vector <int> arr;
int cnt = 0;
for (auto &u : s)
if (u == ' ')
{
arr.emplace_back(cnt);
cnt = 0;
}
else if (u != '\n')
cnt = (cnt << 3) + (cnt << 1) + (u ^ 48);
if (cnt)
arr.emplace_back(cnt);
for (int i = 0; i < arr.size() - 1; i ++)
for (int j = i + 1; j < arr.size(); j ++)
z[arr[i]].push_back(make_pair(arr[j], 1));
}
spfa(1);
if (dis[n] == 0x3f3f3f3f)
cout << "NO";
else
cout << dis[n] - 1;
}
洛谷 P5767【最短路】
最新推荐文章于 2022-11-03 21:39:54 发布