Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Single Source Shortest Path II

For a given weighted graph $G = (V, E)$, find the shortest path from a source to each vertex. For each vertex $u$, print the total weight of edges on the shortest path from vertex $0$ to $u$.

Input

In the first line, an integer $n$ denoting the number of vertices in $G$ is given. In the following $n$ lines, adjacency lists for each vertex $u$ are respectively given in the following format:

$u$ $k$ $v_1$ $c_1$ $v_2$ $c_2$ ... $v_k$ $c_k$

Vertices in $G$ are named with IDs $0, 1, ..., n-1$. $u$ is ID of the target vertex and $k$ denotes its degree. $v_i (i = 1, 2, ... k)$ denote IDs of vertices adjacent to $u$ and $c_i$ denotes the weight of a directed edge connecting $u$ and $v_i$ (from $u$ to $v_i$).

Output

For each vertex, print its ID and the distance separated by a space character in a line respectively. Print in order of vertex IDs.

Constraints

$1 \leq n \leq 10,000$

$0 \leq c_i \leq 100,000$

$|E| < 500,000$

All vertices are reachable from vertex $0$

Sample Input 1

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

Sample Output 1

0 0
1 2
2 2
3 1
4 3

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in C (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>
#define INF 1<<30
#define int long long

typedef struct {
  int vertics;
  int cost;
}pair;
void Search_path(int **,int*,int,int*);
void minHeapify(pair *,int,int);
void swap(pair *,int,int);
signed main(){
  int i,j,k,n,m,dast;
  int *flag,*distant,**array;

  scanf("%lld",&n);
  flag = malloc(sizeof(int)*n);
  distant = malloc(sizeof(int)*n);
  for(i=0;i<n;i++){
    flag[i]=0;
    distant[i]=INF;
  }
  array = malloc(sizeof(int *)*n);
  for(i=0;i<n;i++){
    scanf("%lld %lld",&dast,&m);
    array[i]=malloc(sizeof(int)*(2*m+1));
    array[i][2*m]=-1;
    for(j=0;j<m;j++)scanf("%lld %lld",&array[i][j*2],&array[i][j*2+1]);
  }

  //for(i=0;i<n;i++){
  // for(j=0;array[i][j*2]!=-1;j++)printf(" %lld",array[i][j*2]);
  //printf("\n");
  //}
  
  //
  Search_path(array,flag,n,distant);
  
   for(i=0;i<n;i++)printf("%lld %lld\n",i,distant[i]);
  //
 
  for(i=0;i<n;i++)free(array[i]);
  free(array);
  free(flag);
  free(distant);

  return 0;
}

void Search_path(int **array,int *flag,int n,int *distant){
  int queue_count=0,i,j,num=0,tmp_vertics,tmp_cost,p;
  pair queue[500001];
  
  flag[num]=1;
  distant[num]=0;
  while(1){
    for(i=0;array[num][i*2]!=-1;i++){
      if(flag[array[num][i*2]] == 0){
	
	j=++queue_count;
	queue[j].vertics=array[num][i*2];
	queue[j].cost=distant[num]+array[num][i*2+1];
	while(j > 1 && queue[j/2].cost > queue[j].cost){
	  swap(queue,j,j/2);

	  //tmp_vertics=queue[j].vertics;
	  //tmp_cost=queue[j].cost;
	  
	  //queue[j].vertics=queue[j/2].vertics;
	  //queue[j].cost=queue[j/2].cost;
	  
	  //queue[j/2].vertics=tmp_vertics;
	  //queue[j/2].cost=tmp_cost;
	  
	  j = j/2;
	}
      }
    }
    /**
    printf("%lld",queue_count);
    for(p=0;p<=queue_count;p++)printf("B(%lld %lld)",queue[p].vertics,queue[p].cost);
    printf("\n");
    **/
    if(queue_count == 0)break;
    p=num;
    num = queue[1].vertics;
    if(flag[num]==0)  distant[num]= queue[1].cost;
    
    flag[num]=1;

    queue[1].vertics= queue[queue_count].vertics;
    queue[1].cost = queue[queue_count--].cost;
    /**
    printf("%lld",queue_count);
    for(p=0;p<=queue_count;p++)printf("A(%lld %lld)",queue[p].vertics,queue[p].cost);
    printf("\n");
    **/
    minHeapify(queue,1,queue_count);
    
  }
};
      
void minHeapify(pair *queue,int i,int queue_count){
  int l,r,min;

  l = i*2;
  r = i*2+1;
  if( l <= queue_count && queue[l].cost < queue[i].cost)min=l;
  else min = i;
  if( r <= queue_count && queue[r].cost < queue[min].cost)min=r;
  if(min != i){
    swap(queue,i,min);
    minHeapify(queue,min,queue_count);
  }
}

void swap(pair *queue,int i,int j){
  int tmp_vertics,tmp_cost;
  
  tmp_vertics=queue[i].vertics;
  tmp_cost=queue[i].cost;
	  
  queue[i].vertics=queue[j].vertics;
  queue[i].cost=queue[j].cost;
	  
  queue[j].vertics=tmp_vertics;
  queue[j].cost=tmp_cost;
}