Dijkstra 啦啦啦

xyz.5386發表於2020-11-19
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <utility>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
struct node
{
    int data, w, next;
}e[N << 2];
int head[N], cnt;
int n, m, s;
inline void Add(int u, int v, int w)
{
    e[cnt].data = v;
    e[cnt].w = w;
    e[cnt].next = head[u];
    head[u] = cnt++;
}
priority_queue<pair<int, int >,
 vector<pair<int, int > >, greater<pair<int, int > > > que;
inline void Dijkstra()
{
    bool book[N] = {0};
    int dist[N];
    memset(dist, 0x3f, sizeof(dist));
    que.push(make_pair(0, s));
    dist[s] = 0;
    while (!que.empty()) {
        int cur = que.top().second;
        que.pop();
        if (book[cur])
            continue;
        book[cur] = 1;
        for (int i = head[cur]; i != -1; i = e[i].next) {
            int v = e[i].data;
            if (dist[v] > dist[cur] + e[i].w) {
                dist[v] = dist[cur] + e[i].w;
                que.push(make_pair(dist[v], v));
            }
        }
    }
    for (int i = 1; i <= n; i++)
        printf("%d%c", dist[i], i == n ? '\n' : ' ');
}
inline void Init()
{
    cnt = 1;
    memset(head, -1, sizeof(head));
}
int main()
{
    scanf("%d %d %d", &n, &m, &s);
    Init();
    while (m--) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        Add(u, v, w);
    }
    Dijkstra();
    return 0;
}