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 <vector>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 24;
int c[200][200];
int t[200][200];
int cost[200][200];
int tim[200][200];

int main(){
  int n, m, K;
  int p, q, r;
  while(cin >> n >> m && (n || m)){
    fill(&cost[0][0], &cost[199][199] + 1, INF);
    fill(&tim[0][0], &tim[199][199] + 1, INF);
    fill(&c[0][0], &c[199][199] + 1, INF);
    fill(&t[0][0], &t[199][199] + 1, INF);
    for(int i = 1; i <= m; i++){
      cost[i][i] = tim[i][i] = 0;
      c[i][i] = t[i][i] = 0;
    }
    for(int i = 0; i < n; i++){
      int a, b;
      cin >> a >> b;
      cin >> c[a][b] >> t[a][b];
      c[b][a] = c[a][b];
      t[b][a] = t[a][b];
    }
    for(int k = 1; k <= m; k++){
      for(int i = 1; i <= m; i++){
	for(int j = 1; j <= m; j++){
	  if(cost[i][j] > cost[i][k] + c[k][j]){
	    cost[i][j] = cost[i][k] + c[k][j];
	  }
	  if(tim[i][j] > tim[i][k] + t[k][j]){
	    tim[i][j] = tim[i][k] + t[k][j];
	  }
	}
      }
    }
    cin >> K;    
    for(int i = 0; i < K; i++){
      cin >> p >> q >> r;
      if(!r){
	cout << cost[p][q] << endl;
      }else{
	cout << tim[p][q] << endl;
      }
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <queue>

#define INF 100000000

using namespace std;

int dijk(vector<vector<pair<int,int> > > &graph, int start, int goal){
        int total;
        priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;
        que.push( make_pair(0,start) );
        vector<int> cost(graph.size(), INF);
        while( !que.empty() ){
                pair<int,int> tmp = que.top(); 
                que.pop();
                if( cost[ tmp.second ] < INF ) continue;

        //      cout << tmp.second << "  " << tmp.first << "  " << endl;
                cost[tmp.second] = tmp.first;

                if( tmp.second == goal ) break;
                for(int i=0; i < graph[tmp.second].size(); i++){
                        if( cost[ graph[tmp.second][i].first ] == INF )
                                que.push( make_pair( tmp.first + graph[tmp.second][i].second, graph[tmp.second][i].first ) );
                }
        }
        return cost[goal];
}


int main(){
        int n, m, a, b, cost, time;

        while( cin >> n >> m , n && m ){
                vector<vector<pair<int, int> > > graph(m);
                vector<vector<pair<int, int> > > gtime(m);

                for(int i=0; i<n; i++){
                        cin >> a >> b >> cost >> time;
                        --a; --b;
                        graph[a].push_back( make_pair(b, cost) );
                        graph[b].push_back( make_pair(a, cost) );
                        gtime[a].push_back( make_pair(b, time) );
                        gtime[b].push_back( make_pair(a, time) );
                }

                int k;

                cin >> k;
                for(int i=0; i<k; i++){
                        int p, q, r;
                        cin >> p >> q >> r;
                        p--; q--;
                        if( r ){
                                cout << dijk( gtime, p, q ) << endl;
                        }else{
                                cout << dijk( graph, p, q ) << endl;
                        }
                }
        }

        return 0;
}

C++: //2013/11/15(Fri)

#include <iostream>
using namespace std;
int main()
{
	int n,m;
	int a,b,cost[100][100],time[100][100];
	int k;
	int p,q,r;
	int INF = 1000001;

	while(true){
		cin >> n >> m;
		if(n==0 && m==0)
			break;

		for(int i=0; i<m; i++)
			for(int j=0; j<m; j++){
				if(i==j){
					cost[i][j] = 0;
					time[i][j] = 0;
				}		
				else{
					cost[i][j] = INF;
					time[i][j] = INF;
				}
			}

		for(int i=0; i<n; i++){
			cin >> a >> b ;
			cin >> cost[a-1][b-1] >> time[a-1][b-1];
			cost[b-1][a-1] = cost[a-1][b-1];
			time[b-1][a-1] = time[a-1][b-1];
		}  

		for(int h=0; h<m; h++)
			for(int i=0; i<m; i++)
				for(int j=0; j<m; j++){
					if(cost[i][j] > cost[i][h] + cost[h][j])
						cost[i][j] = cost[i][h] + cost[h][j];
					if(time[i][j] > time[i][h] + time[h][j])
						time[i][j] = time[i][h] + time[h][j];
				}

		cin >> k;
		for(int i=0; i<k; i++){
			cin >> p >> q >> r;

			if(r == 0)
				cout << cost[p-1][q-1] << endl;
			else if(r == 1)
				cout << time[p-1][q-1] << endl;
		}
	}
	return 0;
}