Problem:
Single Source Shortest Path

For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).

Input

An edge-weighted graph G (V, E) and the source r.

|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1

|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.

si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.

Output

Print the costs of SSSP in the following format.

c0
c1
:
c|V|-1

The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.

Constraints

1 ≤ |V| ≤ 100000

0 ≤ di ≤ 10000

0 ≤ |E| ≤ 500000

There are no parallel edges

There are no self-loops

Sample Input 1

4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5

Sample Output 1

0
1
3
4

Sample Input 2

4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5

Sample Output 2

3
0
2
INF

Solution in C (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
 
struct _node_table {
    int* next_node;
    int* cost;
    int size;
    int capacity;
} node_table[100005];
 
long long int dp[100005];
 
void init_node(int i) {
    node_table[i].capacity = 10;
    node_table[i].next_node = (int*)malloc(node_table[i].capacity*sizeof(int));
    node_table[i].cost = (int*)malloc(node_table[i].capacity*sizeof(int));
    node_table[i].size = 0;
}
 
void free_node(int i) {
    free(node_table[i].next_node);
    free(node_table[i].cost);
}
 
void grow_node(int s) {
    if(node_table[s].capacity < node_table[s].size * 1.25) {
        int size, i;
        int* next_node = node_table[s].next_node;
        int* cost = node_table[s].cost;
        node_table[s].capacity *= 1.5;
        node_table[s].next_node = (int*)malloc(node_table[s].capacity*sizeof(int));
        node_table[s].cost = (int*)malloc(node_table[s].capacity*sizeof(int));
        for(i=0,size=node_table[s].size;i<size;i++) {
            node_table[s].next_node[i] = next_node[i];
            node_table[s].cost[i] = cost[i];
        }
        free(next_node);
        free(cost);
    }
}
 
void add_next_node(int s, int t, int d) {
    int size = node_table[s].size;
    node_table[s].next_node[size] = t;
    node_table[s].cost[size] = d;
    node_table[s].size++;
 
    grow_node(s);
}
 
#define QUEUE_MAX 1000000
void solve(int r, int V) {
    int i, size, from, to, cost;
    int queue[QUEUE_MAX];
    long long int wp, rp;
 
    for(i=0;i<V;i++) dp[i] = INT_MAX;
    dp[r] = 0;
 
    wp = rp = 0;
    queue[wp++%QUEUE_MAX] = r;
 
    while(wp>rp) {
        from = queue[rp++%QUEUE_MAX];
 
        for(i=0,size=node_table[from].size;i<size;i++) {
            to = node_table[from].next_node[i];
            cost = node_table[from].cost[i];
 
            if(cost+dp[from] < dp[to]) {
                queue[wp++%QUEUE_MAX] = to;
                dp[to] = cost+dp[from];
            }
        }
    }
}
 
int main(void) {
    int i, V, E, r;
    scanf("%d %d %d\n", &V, &E, &r);
    for(i=0;i<V;i++) init_node(i);
 
    for(i=0;i<E;i++) {
        int s, t, d;
        scanf("%d %d %d\n", &s, &t, &d);
        add_next_node(s, t, d);
    }
 
    solve(r, V);
 
    for(i=0;i<V;i++) {
        free_node(i);
        if(dp[i] == INT_MAX) {
            printf("INF\n");
        } else {
            printf("%d\n", dp[i]);
        }
    }
 
    return 0;
}