Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		m, _ := strconv.Atoi(buf[1])
		if n == 0 && m == 0 {
			break
		}
		c_matrix := make([][]int, m)
		for i := range c_matrix {
			c_matrix[i] = make([]int, m)
		}
		t_matrix := make([][]int, m)
		for i := range t_matrix {
			t_matrix[i] = make([]int, m)
		}
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			a, _ := strconv.Atoi(buf_[0])
			b, _ := strconv.Atoi(buf_[1])
			c, _ := strconv.Atoi(buf_[2])
			t, _ := strconv.Atoi(buf_[3])
			c_matrix[a-1][b-1] = c
			t_matrix[a-1][b-1] = t
		}
		c_dist := make([][]int, m)
		c_prev := make([][]int, m)
		t_dist := make([][]int, m)
		t_prev := make([][]int, m)
		for i := 0; i < m; i++ {
			c_dist[i] = make([]int, m)
			c_prev[i] = make([]int, m)
			t_dist[i] = make([]int, m)
			t_prev[i] = make([]int, m)
		}
		for i := 0; i < m; i++ {
			for j := 0; j < m; j++ {
				if c_matrix[i][j] == 0{
					c_dist[i][j] = INF
				} else {
					c_dist[i][j] = c_matrix[i][j]
				}
				if c_matrix[i][j] == 0{
					c_prev[i][j] = -1
				} else {
					c_prev[i][j] = i
				}
				if t_matrix[i][j] == 0{
					t_dist[i][j] = INF
				} else {
					t_dist[i][j] = t_matrix[i][j]
				}
				if t_matrix[i][j] == 0{
					t_prev[i][j] = -1
				} else {
					t_prev[i][j] = i
				}
			}
			c_dist[i][i] = 0
			t_dist[i][i] = 0
		}
		for k := 0; k < m; k++ {
			for i := 0; i < m; i++ {
				for j := 0; j < m; j++ {
					c_newlen := c_dist[i][k] + c_dist[k][j]
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[k][j]
					}
					if t_newlen < t_dist[i][j] {
						t_dist[i][j] = t_newlen
						t_prev[i][j] = t_prev[k][j]
					}
				}
			}
		}
		scanner.Scan()
		k, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < k; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			p, _ := strconv.Atoi(buf_[0])
			q, _ := strconv.Atoi(buf_[1])
			r, _ := strconv.Atoi(buf_[2])
			switch r {
			case 0: fmt.Println(c_dist[p-1][q-1])
			case 1: fmt.Println(t_dist[p-1][q-1])
			}
		}
	}
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0200

/* http://www.deqnotes.net/acmicpc/dijkstra/
9 6
1 2 5 0
1 3 4 0
1 4 2 0
2 3 2 0
3 4 3 0
2 6 6 0
3 5 2 0
5 6 4 0
4 5 6 0
1
1 6 0
0 0

*/

#include <cstdio>
#include <climits>
#include <cstring>

#define STATION_MAX 101

//#define dprintf(arg1, ... ) printf( arg1, __VA_ARGS__)
#define dprintf(arg1, ... ) 

/*
typedef struct {
	
} LINE;
*/


int main(void) {

	while(1) {
		int line_cnt, stat_cnt;
		scanf("%d %d\n", &line_cnt, &stat_cnt);
		if ( line_cnt==0 && stat_cnt== 0 ) { break; }
		
		int line_cost[2][STATION_MAX][STATION_MAX];
		memset( line_cost, -1, sizeof(int)*2*STATION_MAX*STATION_MAX );
		
		int a; int b; int fee; int time;
		for( int i=0; i<line_cnt; i++ ) {
			scanf("%d %d %d %d\n", &a, &b, &fee, &time );
			line_cost[0][a][b] = line_cost[0][b][a] = fee;
			line_cost[1][a][b] = line_cost[1][b][a] = time;
		}
		
		int query_cnt;
		scanf("%d\n", &query_cnt );
		
		for( int i=0; i<query_cnt; i++ ) {
			int start, end, type;
			
			scanf("%d %d %d\n", &start, &end, &type );
			
			int stat_cost[STATION_MAX];
			bool stat_flag[STATION_MAX];
			memset( stat_cost, -1, sizeof(int)*STATION_MAX );
			memset( stat_flag, false, sizeof(bool)*STATION_MAX );
			
			stat_cost[start] = 0;
			
			while(1) {
				int minstat = -1;
				int minval=INT_MAX;
				for ( int i=1; i<stat_cnt+1; i++ ) {
					if ( stat_cost[i] == -1 ) { continue; }
					if ( stat_flag[i] == true ) { continue; }
					if ( stat_cost[i] < minval ) { minstat = i; minval = stat_cost[i]; }
				}
				if ( minstat == -1 ) { break; }
				dprintf("minstat: %d stat_cost=%d\n", minstat, stat_cost[minstat]);
				
				stat_flag[minstat] = true;
				
				//check_tonari
				for ( int i=1; i<stat_cnt+1; i++ ) {
				if ( line_cost[type][minstat][i] == -1 ) { continue; }
					if ( stat_cost[i] == -1 ||  stat_cost[i] > stat_cost[minstat] + line_cost[type][minstat][i] ) {
						dprintf( "Refresh stat_cost[%d]:  %d (old: %d)\n", i, stat_cost[minstat] + line_cost[type][minstat][i], stat_cost[i]);
						stat_cost[i] = stat_cost[minstat] + line_cost[type][minstat][i];
					}
				}
			}
			
			printf("%d\n", stat_cost[end]);
		}
	}
	
	return 0;
}

C++: #include<iostream>
using namespace std;

const int INF = 10000000;
int d[101][101];

int main(){
  int W_cost[101][101];
  int W_time[101][101];
  int n,m;
  int a,b,cost,tim;
  cin >> n >> m;
  for(int i=1;i<=m;i++){
    for(int j=1;j<=m;j++){
      W_cost[i][j] = INF;
      W_time[i][j] = INF;
    }
  }

  for(int i=1;i<=m;i++){
    W_cost[i][i] = 0;
    W_time[i][i] = 0;
  }

  for(int i=0;i<n;i++){
    cin >> a >> b >> cost >> tim;
    W_cost[a][b] = cost;
    W_time[a][b] = tim;
  }

  int k;
  cin >> k;
  int p,q,r;
  int ans[50];
  int nn=0;
  while(1){
    cin >> p >> q;
    if(p==0&&q==0)
      break;
    cin >> r;
    if(r){
      for(int i=1;i<=m;i++){
        for(int j=1;j<=m;j++){
          d[i][j] = W_time[i][j];
        }
      }
    }
    else{
      for(int i=1;i<=m;i++){
        for(int j=1;j<=m;j++){
          d[i][j] = W_cost[i][j];
        }
      }
    }

    for(int k=1;k<=m;k++){
      for(int i=1;i<=m;i++){
        for(int j=1;j<=m;j++){
          if(d[i][j]>d[i][k]+d[k][j]){
            d[i][j] = d[i][k]+d[k][j];
          }
          if(d[i][j]>d[j][k]+d[k][i]){
            d[i][j] = d[j][k]+d[k][i];
          }
        }
      }
    }
    nn++;
    ans[nn] = d[p][q];

  }
  for(int i=1;i<=k;i++){
    cout << ans[i] << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef struct E{
  int to,cost,tim;
}edge;
vector<vector<edge> >root(310);
int d[2][301];
void dijkstraco(int s){
  priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >que;
  d[0][s] = 0;
  que.push(pair<int,int>(0,s));
  while(!que.empty()){
    pair<int,int> p=que.top();que.pop();
    int v = p.second;
    if(d[0][v]<p.first)continue;
    for(int j=0;j<root[v].size();j++){
      edge t = root[v][j];
      if(d[0][t.to]>d[0][v]+t.cost){
	d[0][t.to]=d[0][v]+t.cost;
	que.push(pair<int,int>(d[0][t.to],t.to));
      }
    }
  }
}
void dijkstrati(int s){
  priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >que;
  d[1][s] = 0;
  que.push(pair<int,int>(0,s));
  while(!que.empty()){
    pair<int,int> p=que.top();que.pop();
    int v = p.second;
    if(d[1][v]<p.first)continue;
    for(int j=0;j<root[v].size();j++){
      edge t = root[v][j];
      if(d[1][t.to]>d[1][v]+t.tim){
	d[1][t.to]=d[1][v]+t.tim;
	que.push(pair<int,int>(d[1][t.to],t.to));
      }
    }
  }
}
int main(){
  int n,m;
  while(cin>>n>>m,n+m!=0){
    for(int j=0;j<2;j++){
      for(int a=0;a<301;a++)
	d[j][a]=1000000000;
    }
    for(int i=0;i<301;i++)
      root[i].clear();
    for(int i=0;i<n;i++){
      int f;cin>>f;
      edge e;cin>>e.to>>e.cost>>e.tim;
      root[f].push_back(e);
      int tmp=f;
      f=e.to;
      e.to=tmp;
      root[f].push_back(e);
    }
    int k;cin>>k;
    for(int i=0;i<k;i++){

      int p,q,r;cin>>p>>q>>r;
      if(r==0)
	dijkstraco(p);
      else
	dijkstrati(p);
      cout<<d[r][q]<<endl;
    }
  }
  return 0;
}