1 条题解

  • 1
    @ 2024-7-25 13:39:22

    参考答案:

    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    const int N = 510;
    int g[N][N];
    int dis[N];
    int n, m;
    int st[N];
    
    int dijkstra()
    {
        memset(dis, 0x3f, sizeof dis);
        dis[1] = 0;
    
        for (int i = 1; i < n; ++i)
        {
            int t = -1;
            for (int j = 1; j <= n; ++j)
            {
                if (!st[j] && (t == -1 || dis[j] < dis[t]))
                    t = j;
            }
            st[t] = 1;
            for (int j = 1; j <= n; ++j)
            {
                dis[j] = min(dis[j], dis[t] + g[t][j]);
            }
        }
    
        return dis[n];
    }
    int main()
    {
        cin >> n >> m;
    
        memset(g, 0x3f, sizeof g);
        for (int i = 1; i <= m; ++i)
        {
            int x, y, z;
            cin >> x >> y >> z;
            g[x][y] = min(g[x][y], z);
        }
    
        if (dijkstra() == 0x3f3f3f3f) cout << "-1";
        else cout << dis[n];
    
        return 0;
    }
    
    • 1

    信息

    ID
    94
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    5
    已通过
    2
    上传者