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++: #include<iostream>
#include<vector>
using namespace std;
class NextNodesData
{
public:
	int nextNodesName;
	int nextNodesMany;
	int nextNodesTime;
};
class Nodes
{
public:
	vector<NextNodesData> nextNodes;
	int name;
	int minValue;
	bool thisNodesIsFixed;
};

int main()
{
	int N, M;
	cin >> N >> M;
	vector<Nodes> nodesList;
	for(int i = 0; i < M; i++)
	{
		Nodes nodes;
		nodes.name = i+1;
		nodesList.push_back(nodes);
	}
	for(int i = 0; i < N; i++)
	{
		int Start;
		int End;
		int Many;
		int Time;
		cin >> Start >> End >> Many >> Time;

		NextNodesData nodes;
		nodes.nextNodesName = End;
		nodes.nextNodesMany = Many;
		nodes.nextNodesTime = Time;
		nodesList[Start - 1].nextNodes.push_back(nodes);
	}

	int K;
	cin >> K;
	for(int i = 0; i < K; i++)
	{
		cout << 0 << endl;
	}
	/*for(int i = 0; i < K; i++)
	{
		for(int j = 0; j < nodesList.size(); j++)
		{
			nodesList[j].minValue = -1;
			nodesList[j].thisNodesIsFixed = false;
		}
		int Start;
		int End;
		int mode;
		vector<int> candidateNodes;
		
		cin >> Start >> End;
		cin >> mode;

		nodesList[Start - 1].minValue = 0;
		nodesList[Start - 1].thisNodesIsFixed = true;
		candidateNodes.push_back(Start);
		int minValue = 1001;
		while(1)
		{
			minValue = 1001;
			int nextNodesName = -1;
			//????¢???????????????????????????????????¬?????¢???????????????????????????¢??????
			for(int j = 0; j < candidateNodes.size(); j++)
			{
				for(int k = 0; k < nodesList[candidateNodes[j] - 1].nextNodes.size(); k++)
				{
					if(nodesList[nodesList[candidateNodes[j] - 1].nextNodes[k].nextNodesName - 1].thisNodesIsFixed == true)
					{
						continue;
					}

					int value;
					value = 0;
					if(mode == 0)
					{
						value = nodesList[candidateNodes[j]-1].minValue;
						value += nodesList[candidateNodes[j]-1].nextNodes[k].nextNodesMany;
					}
					else
					{
						value = nodesList[candidateNodes[j] - 1].minValue;
						value += nodesList[candidateNodes[j] - 1].nextNodes[k].nextNodesTime;
					}
					if(value < minValue)
					{
						minValue = value;
						nextNodesName = nodesList[candidateNodes[j] - 1].nextNodes[k].nextNodesName;
					}

				}
			}
			//???????????§??§????¬?????¢??????????????????????????±????

			nodesList[nextNodesName - 1].minValue = minValue;
			candidateNodes.push_back(nextNodesName);
			nodesList[nextNodesName - 1].thisNodesIsFixed = true;
			if(nextNodesName == End)
			{
				break;
			}
		}
		cout << nodesList[End - 1].minValue << endl;


	}  */


}

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

int cost[101][101];
int gtime[101][101];

struct tSta{
	int val, sta;
	tSta( int s ){
		val = 0;
		sta = s;
	}
};

bool operator<( const tSta& l, const tSta& r ){
	return l.val > r.val;
}

int main(){
	priority_queue<tSta> que;
	int n, m, k;
	while( cin >> n >> m, n||m ){
		memset( cost, 0, sizeof(cost) );
		memset( gtime, 0, sizeof(gtime) );
		for( int i=0; i<n; i++ ){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[a][b] = cost[b][a] = c;
			gtime[a][b] = gtime[b][a] = t;
		}
		cin >> k;
		for( int i=0; i<k; i++ ){
			int p, q, r;
			cin >> p >> q >> r;
			while( !que.empty() )	que.pop();
			que.push( tSta(p) );
			while( !que.empty() ){
				tSta ts = que.top();
				if( ts.sta == q )	break;
				que.pop();
				if( r==0 ){	// cost
					for( int j=1; j<=m; j++ ){
						if( cost[ts.sta][j] > 0 ){
							tSta next(j);
							next.val = ts.val + cost[ts.sta][j];
							que.push( next );
						}
					}
				}else if( r==1 ){	// time
					for( int j=1; j<=m; j++ ){
						if( gtime[ts.sta][j] > 0 ){
							tSta next(j);
							next.sta = j;
							next.val = ts.val + gtime[ts.sta][j];
							que.push( next );
						}
					}
				}
			}
			cout << que.top().val << endl;
		}
	}
	return 0;
}

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

class State{
public:
  int v, c;
  State(int _v, int _c)
    :v(_v), c(_c) {}

  bool operator < (const State& st) const{
    return this->c > st.c;
  }
};


int main(){
  int n, m, k;
  const int INF = 1 << 30;

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

    vector<vector<vector<int> > > score(2, vector<vector<int> >(m, vector<int>(m, 0)));
    int a, b, cost, time;

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


    int p, q, r;

    cin >> k;
    while(k--){
      cin >> p >> q >> r; 
      vector<int> d(m, INF);
      --q;
      --p;
      priority_queue<State> req;
      req.push(State(p, 0));
      d[p] = 0; 
	while(! req.empty()){
	  State st = req.top();
	  req.pop();

	  if (st.v == q) {
	    cout << st.c << endl;
	    break;
	  }

	  if (d[st.v] < st.c)
	    continue;

	  for(int i=0; i<m; i++){
	    if(score[r][st.v][i] != 0){
	      if(st.c + score[r][st.v][i] < d[i]){
		d[i] = st.c + score[r][st.v][i];
		req.push(State(i, d[i]));
	      } 
	    }
	  }
	}

    }
  }
    return 0;
}