#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 200010, M = 2 * N;

int head[N], ver[M], e[M], ne[M], idx;
int dist[N];
bool st[N];
int n, m, s;

void add(int a, int b, int c)
{
    ver[idx] = b, e[idx] = c, ne[idx] = head[a], head[a] = idx ++;
}

void dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    dist[s] = 0;
    for (int i = 0; i < n; i ++ )
    {
        int t = -1;
        for (int j = 1; j <= n; j ++ )
            if (!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        st[t] = true;
        for (int j = head[t]; ~j; j = ne[j])
        {
            int k = ver[j];
            if (dist[k] > dist[t] + e[j])
                dist[k] = dist[t] + e[j];
        }
    }
}

int main()
{
    memset(head, -1, sizeof head);
    cin >> n >> m >> s;
    for (int i = 0; i < m; i ++ )
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }
    dijkstra();
    for (int i = 1; i <= n; i ++ ) cout << dist[i] << ' ';
    return 0;
}

原文地址: https://www.cveoy.top/t/topic/bPwZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录