#include #include #include #include typedef struct binaryHeap{ void *array; size_t heap_size; size_t max_size; size_t val_size; int (*cmp) (const void *, const void *); } heap; heap* new_binary_heap (const size_t val_size, int (*cmpF) (const void *, const void *)) { heap *h = (heap *) calloc (1, sizeof (heap)); h->array = malloc (val_size * (1 + 1)); h->heap_size = 0; h->max_size = 1; h->val_size = val_size; h->cmp = cmpF; return h; } int is_empty (const heap *h) { return h->heap_size == 0; } void free_heap (heap *h) { free (h->array); free (h); } void init_heap (heap *h) { h->heap_size = 0; } static void heap_func_swap (void * restrict a, void * restrict b, size_t val_size) { uint8_t *p = (uint8_t *) a; uint8_t *q = (uint8_t *) b; while (val_size--) { uint8_t tmp = *p; *p++ = *q; *q++ = tmp; } } static void heap_func_copy (void * restrict dst, const void * restrict src, size_t val_size) { uint8_t *p = (uint8_t *) src; uint8_t *q = (uint8_t *) dst; while (val_size--) { *q++ = *p++; } } void push (heap *h, const void *val) { if (h->heap_size == h->max_size) { h->max_size = 2 * h->max_size + 1; h->array = realloc (h->array, h->val_size * (h->max_size + 1)); } h->heap_size++; uint8_t *array = (uint8_t *) h->array; size_t k = h->heap_size; const size_t val_size = h->val_size; int (*cmp) (const void *, const void *) = h->cmp; heap_func_copy(array + k * val_size, val, val_size); while(k>1){ size_t parent = k / 2; if (cmp (array + parent * val_size, array + k * val_size) <= 0) { return; } heap_func_swap (array + parent * val_size, array + k * val_size, val_size); k = parent; } } void pop (heap *h, void *res) { uint8_t *array = (uint8_t *) h->array; const size_t val_size = h->val_size; if (res != NULL) { heap_func_copy (res, array + val_size, val_size); } heap_func_copy (array + val_size, array + val_size * h->heap_size, val_size); h->heap_size--; int (*cmp) (const void *, const void *) = h->cmp; const size_t n = h->heap_size; size_t k = 1; while (k <= (n - 1) / 2) { int c = cmp (array + val_size * 2 * k, array + val_size * (2 * k + 1)); size_t next = 2 * k + (c <= 0 ? 0 : 1); if (cmp (array + val_size * k, array + val_size * next) <= 0) return; heap_func_swap (array + val_size * k, array + val_size * next, val_size); k = next; } if (2 * k <= n && cmp (array + val_size * k, array + val_size * 2 * k) > 0) { heap_func_swap (array + val_size * k, array + val_size * 2 * k, val_size); } } typedef struct directed_edge { int32_t vertex; int32_t cost; int32_t next; } graph_edge; typedef struct directedGraph { graph_edge *edge; int32_t *start; int32_t pointer; int32_t vertex_num; int32_t max_size; } graph; graph* newGraph (const int vertex_num) { graph *g = (graph *) calloc (1, sizeof (graph)); g->edge = (graph_edge *) calloc (1, sizeof (graph_edge)); g->start = (int32_t *) calloc (vertex_num, sizeof (int32_t)); g->pointer = 0; g->vertex_num = vertex_num; g->max_size = 1; for (int32_t i = 0; i < vertex_num; ++i) { g->start[i] = -1; } return g; } void addEdge (graph *g, int32_t from, int32_t to, int32_t cost) { if (g->pointer == g->max_size) { g->max_size *= 2; g->edge = (graph_edge *) realloc (g->edge, sizeof (graph_edge) * g->max_size); } g->edge[g->pointer] = (graph_edge) {to, cost, g->start[from]}; g->start[from] = g->pointer++; } typedef int32_t i32; typedef int64_t i64; typedef struct node { i32 v; i64 d; } node; int cmpNode (const void *a, const void *b) { i64 d = ((node *)a)->d - ((node *)b)->d; return d == 0 ? 0 : d < 0 ? -1 : 1; } void run (void) { i32 n, m; scanf("%" SCNi32 "%" SCNi32, &n, &m); graph *g = newGraph (2 * n); while (m--) { i32 a, b, c; scanf("%" SCNi32 "%" SCNi32 "%" SCNi32, &a, &b, &c); a--; b--; addEdge (g, a, b, c); addEdge (g, b, a, c); addEdge (g, a + n, b + n, c); addEdge (g, b + n, a + n, c); addEdge (g, a, b + n, 0); addEdge (g, b, a + n, 0); } i64 *dp = (i64 *) calloc (2 * n, sizeof (i64)); uint8_t *used = (uint8_t *) calloc (2 * n, sizeof (uint8_t)); for (i32 i = 1; i < n; ++i) { dp[i] = (i64) 1000000000 * n; dp[i + n] = dp[i]; } heap *h = new_binary_heap (sizeof (node), cmpNode); push (h, &((node){0, 0})); while (!is_empty (h)) { node t; pop (h, &t); const i32 v = t.v; if (used[v]) continue; used[v] = 1; for (i32 p = g->start[v]; p != -1; p = g->edge[p].next) { i32 u = g->edge[p].vertex; i64 d = t.d + g->edge[p].cost; if (d >= dp[u]) continue; dp[u] = d; push (h, &((node){u, d})); } } for (i32 i = 0; i < n; ++i) { printf("%" PRIi64 "\n", dp[i] + dp[i + n]); } } int main (void) { run (); return 0; }