题解【洛谷P5767】[NOI1997]最优乘车

题面

一道很经典的最短路模型转换问题。

考虑如何建图。

我们可以发现,对于每一条公交线路,可以将这条线路上 可以到达的两个点 连一条权值为 \(1\) 的边。

获取一条公交线路上的每一个点可以使用读取每一个字符的方式,注意要先读取第一行的换行符。

然后就是普通的 BFS 求图的最短路问题了。

最后注意特判输出 NO\(0\) 的情况。

#include <bits/stdc++.h>

using namespace std;

const int N = 503;

int n, m; 
int a[N], tot; //存储每一条公交线路
bool g[N][N]; //存图的邻接矩阵
int dist[N]; //1 号点到每个点的距离
int q[N], hh, tt; //BFS 的队列

inline void bfs() //BFS 求最短路
{
    hh = tt = 0;
    q[0] = 1;
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    while (hh <= tt)
    {
        int u = q[hh++];
        for (int i = 1; i <= n; i+=1)
            if (g[u][i] && dist[i] > dist[u] + 1) 
            {
                dist[i] = dist[u] + 1;
                q[++tt] = i;
            }
    }
}

int main()
{
    cin >> m >> n;
    string h;
    getline(cin, h); //注意要先读取换行符
    for (int i = 1; i <= m; i+=1)
    {
        getline(cin, h);
        int len = h.size();
        tot = 0;
        for (int j = 0; j < len; j+=1)
        {
            int now = 0;
            while (h[j] >= '0' && h[j] <= '9')
                now = now * 10 + h[j] - '0', ++j;
            a[++tot] = now; //获取每一个站点
        }
        for (int j = 1; j < tot; j+=1)
            for (int k = j + 1; k <= tot; k+=1)
                g[a[j]][a[k]] = true; //建图
    }
    bfs(); //求最短路
    if (dist[n] == 0x3f3f3f3f) puts("NO"); //无解
    else cout << max(0, dist[n] - 1) << endl; //注意要与 0 取 max
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/xsl19/p/12375163.html