Go: // Warshall–Floyd Algorithm

package main

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

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
					c_prev[i][j] = -1
				} else {
					c_dist[i][j] = c_matrix[i][j]
					c_prev[i][j] = i
				}
				if t_matrix[i][j] == 0 {
					t_dist[i][j] = INF
					t_prev[i][j] = -1
				} else {
					t_dist[i][j] = t_matrix[i][j]
					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]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[k][j]
					}
				}
			}
		}
		for k := 0; k < m; k++ {
			for i := 0; i < m; i++ {
				for j := 0; j < m; j++ {
					t_newlen := t_dist[i][k] + t_dist[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++: #include<iostream>
#include<algorithm>
#define MAX_M 101
#define MAX_N 301
#define MAX_K 201
#define MAX_INT 1 << 25;
using namespace std;

//dx[wa][wb][XCb`(0:àz, 1:Ô)] = (waÆwbÔÌàz or Ô)
int dx[MAX_N][MAX_N][2];

//p©çqÜÅÌÅ¬àzÜ½ÍÅZÔðÔ·
int tell(int p, int q, int n, int m, int r);

int main(){
	//n = üHÌîñÌ, m = wÌ
	int n, m;
	
	//üHÌîñðüÍ
	while(cin >>n >>m){
		if(n == 0 && m == 0) break;
		
		//dx[][][]Ìú»(SÄ-1É·é)
		for(int i = 0; i < MAX_N; i++){
			for(int j = 0; j < MAX_N; j++){
				dx[i][j][0] = -1;
				dx[i][j][1] = -1;
			}
		}
		
		for(int i = 0; i < n; i++){
			
			//a = wa, b = wb, cost = wa`wbÌàz, time = wa`wbÌÔ
			int a, b, cost, time;
			
			cin >>a >>b >>cost >>time;
			dx[a][b][0] = cost;
			dx[a][b][1] = time;
		}
		
		//k = â¢í¹Ì
		int k;
		
		cin >>k;
		for(int i = 0; i < k; i++){
			
			//p = o­w, q = w, r = oÍ·élÌíÞ
			int p, q, r;
			cin >>p >>q >>r;
			
			cout <<tell(p, q, n, m, r) <<endl;
		}
	}
	return 0;
}

int tell(int p, int q, int n, int m, int r){
	
	//checkendnum[wÌÔ] = Å¬àzÜ½ÍÅZÔ(¢mèÌêÍMAX_INT)
	int checkendnum[MAX_N];
	
	//checkendi[wÌÔ] = Å¬àzÜ½ÍÅZÔªmèµ½©Ç¤©(false:¢mè, true:mè)
	bool checkendi[MAX_N] = {false};
	
	//checkendnum[1 ` MAX_N]ðMAX_INTÅú»
	for(int i = 0; i < MAX_N; i++){
		checkendnum[i] = MAX_INT;
	}
	
	//p = ¡²×Ä¢ém[h
	int node = p;
	
	//o­wÍà¤0ÅmèµÄ¢é
	checkendnum[node] = 0;
	checkendi[node] = true;
	
	//EoðÍmè³ê½m[h = q
	while(1){
		
		//SÄÌüHÌîñð²×é
		for(int i = 0; i < n; i++){
			
			//p©çiÉs­àzÜ½ÍÔÌîñª èA»êªmèµÄ¢È¯êÎ
			if(dx[node][i][r] != -1){
				if(checkendi[i] == false){
					
					//¡ÜÅiÌm[hÉüÁÄ¢½óâÌîñÆAVµ­XV³êéûÌA¬³¢ûðóâÆ·é
					checkendnum[i] = min(checkendnum[i], (dx[node][i][r] + checkendnum[node]));
				}
			
			//àµ­ÍAp©çiÉs­àzÜ½ÍÔÌîñª èA»êªmèµÄ¢È¯êÎ
			} else if(dx[i][node][r] != -1){
				if(checkendi[i] == false){
					
					//¡ÜÅiÌm[hÉüÁÄ¢½îñÆAVµ­XV³êéûÌA¬³¢ûðóâÆ·é
					checkendnum[i] = min(checkendnum[i], (dx[i][node][r] + checkendnum[node]));
				}
			}
		}
		
		//minnum = mèµÄ¢È¢wÌÅAÅ¬ÌàzÜ½ÍÅZÌÔ, mini = minnumðÂwÌÔ
		int minnum = MAX_INT;
		int mini = -1;
		
		//SÄÌwð²×é
		for(int i = 1; i <= m; i++){
			
			//àzÜ½ÍÔªmèµÄ¨ç¸Aminnumæè¬³¢àzÜ½ÍÔðÂÈç
			if(checkendi[i] == false && checkendnum[i] < minnum){
				minnum = checkendnum[i];
				mini = i;
			}
		}
		
		//miniÌwÌàzÜ½ÍÔðmè
		checkendi[mini] = true;
		
		//Ì²×ém[hðminiÉ·é
		node = mini;
		
		//qÌÅ¬àzÜ½ÍÅZÔªmèµ½ÈçI¹(I¹ð)
		if(checkendi[q] == true){
			break;
		}
	}
	return checkendnum[q];
}

C++: #include <iostream>
#include <algorithm>
#define INF 1000000
using namespace std;

main() {
  while(1) {
    int n, m;
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    int data[2][m][m];
    fill(&data[0][0][0], &data[1][m-1][m-1]+1, INF);
    for(int i = 0; i < n; i++) {
      int a, b, c, time;
      cin >> a >> b >> c >> time;
      a--;
      b--;
      data[0][a][b] = data[0][b][a] = c;
      data[1][a][b] = data[1][b][a] = time;
    }

    int k;
    cin >> k;
    for(int t = 0; t < k; t++) {
      int p, q, r;
      cin >> p >> q >> r;
      p--;
      q--;
      
      bool visited[m];
      fill(&visited[0], &visited[m], false);
      int cost[m];
      fill(cost, cost+m, INF);
      cost[p] = 0;
      while(1) {
	int min = INF;
	int index;
	for(int i = 0; i < m; i++) {
	  if(!visited[i] && cost[i] < min) {
	    min = cost[i];
	    index = i;
	  }
	}
	if(min == INF) break;
	visited[index] = true;
	
	for(int i = 0; i < m; i++) {
	  if(visited[i]) continue;
	  int newValue = cost[index] + data[r][index][i];
	  if(newValue < cost[i]) {
	    cost[i] = newValue;
	  }
	}
      }
      cout << cost[q] << endl;
    }
  }
}

C++: #include <iostream>
using namespace std;
int time[110][110];
int cost[110][110];
int main()
{
    int n,m;
    while(cin>>n>>m){
        if(!n)return 0;
        for(int i=0;i<m;++i){
            for(int j=0;j<m;++j){
                if(i==j){
                    time[i][j]=0;
                    cost[i][j]=0;
                }else{
                    time[i][j]=100000;
                    cost[i][j]=100000;
                }
            }
        }
        for(int i=0;i<n;++i){
            int a,b,c,t;
            cin>>a>>b>>c>>t;
            a--;b--;
            cost[a][b]=c;
            cost[b][a]=c;
            time[a][b]=t;
            time[b][a]=t;
        }
        for(int k=0;k<m;++k){
            for(int i=0;i<m;++i){
                for(int j=0;j<m;++j){
                    cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
                    time[i][j]=min(time[i][j],time[i][k]+time[k][j]);
                }
            }
        }
        int k;
        cin>>k;
        for(int i=0;i<k;++i){
            int p,q,r;
            cin>>p>>q>>r;
            p--;q--;
            if(!r)
                cout<<cost[p][q]<<endl;
            else
                cout<<time[p][q]<<endl;
        }
    }
}