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]
					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 <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>
#include <cassert>
using namespace std;

//common
typedef int  i32;
typedef long long i64,ll;
#define BR "\n"

#define ALL(c) (c).begin(),(c).end()
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))

typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;
typedef vector<string> vs; typedef long double ld;

//config
#define MODE_DEBUG
//#define INF 1<<30
//#define EPS 1e-8
//const ll MOD =100000007;

//debug
//#ifdef MODE_DEBUG
#define LINE()    cerr<< " (L" << __LINE__ << ")"
#define LINELN()    LINE()<<endl

#define  DUMP(x)  cerr << #x << " = " << (x) <<endl
#define DEBUG(x) DUMP(x) << " (L" << __LINE__ << ")" << " " << __FILE__<<endl
#define CHECK(exp,act)  if(exp!=act){DUMP(exp);DEBUG(act);}
#define STOP(e)  CHECK(e,true);if(!(e)) exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp,act)
#define STOP(e)
#endif

template<class T> inline string toString(const vector<T>& x) {
	stringstream ss;
	REP(i,x.size()){
		if(i!=0)ss<<" ";
		ss<< x[i];
	}
	return ss.str();
}
template<class T> inline string toString(const vector<vector<T>>& map) {
	stringstream ss;
	REP(i,map.size()){
		if(i!=0)ss<<BR;
		ss<< toString(map[i]);
	}
	return ss.str();
}
template<class K,class V>  string toString(map<K,V>& x) {
	string res;stringstream ss;
	for(auto& p:x)ss<< p.first<<":" << p.second<<" ";
	return ss.str();
}

template<typename T,typename V> inline T mod(T v,V MOD){
	return (v%MOD+MOD)%MOD;
}

typedef vector<vector<int>> Mat;
void warshall_floyd(Mat& d){
    const int V=d.size();
    for(int k=0;k<V;k++)
        for(int i=0;i<V;i++)for(int j=0;j<V;j++)
            if(d[i][j]>d[i][k] + d[k][j]){
                d[i][j]=d[i][k] + d[k][j];
                //prev[i][j]=k;
            }
}

const int INF =1<<28;
int main(){

	while(true){
		int N,M;cin >> N >> M;
		if(N==0 && M==0)return 0;
		Mat ds(M+1,vector<int>(M+1,INF));
		Mat cs(M+1,vector<int>(M+1,INF));
		REP(i,M+1)ds[i][i]=cs[i][i]=0;

		REP(i,N){
			int a,b,c,t; cin >> a >> b >> c>>t;
			ds[a][b]=min(ds[a][b],t);ds[b][a]=min(ds[b][a],t);

			cs[a][b]=min(cs[a][b],c);cs[b][a]=min(cs[b][a],c);
		}
		warshall_floyd(ds);DUMP(toString(ds));
		warshall_floyd(cs);DUMP(toString(cs));
		int k;cin >> k;
		REP(i,k){
			int p,q,r;cin >> p >> q >>r;
			if(r==0)cout<<cs[p][q]<<endl;
			else cout<<ds[p][q]<<endl; 
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#define MAX_M 100
#define INF (1<<21)
using namespace std;

int dp_cost[MAX_M][MAX_M];
int dp_time[MAX_M][MAX_M];
int m;

void warshall_froyd(){
  for(int k=0;k<m;k++){
    for(int i=0;i<m;i++){
      for(int j=0;j<m;j++){
        dp_cost[i][j] = min(dp_cost[i][j], dp_cost[i][k] + dp_cost[k][j]);
        dp_time[i][j] = min(dp_time[i][j], dp_time[i][k] + dp_time[k][j]);
      }
    }
  }
}

int main(){
  int n;
  cin >> n >> m;
  for(int i=0;i<m;i++){
    for(int j=0;j<m;j++){
      dp_time[i][j] = (i==j ? 0 : INF);
      dp_cost[i][j] = (i==j ? 0 : INF);
    }
  }

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

  int k,p,q,r;
  cin >> k;
  for(int i=0;i<k;i++){
    cin >> p >> q >> r;
    p--; q--;
    cout << (r == 0 ? dp_cost[p][q] : dp_time[p][q]) << endl;
  }
  cin >> p >> q;
  return 0;
}

C++: #include<iostream>
using namespace std;
#include<algorithm>
#include<string>
#define REP(i,a) for(int  i=0;i<a;i++)
int A[101][101];
int B[101][101];
int main(){
	int a,b=0,c=0,d,n,m;	
	cin>>n>>m;
	while(n!=0||m!=0){
	REP(i,m){
		REP(j,m){
			A[i][j]=1001;
			B[i][j]=1001;
		}
	}
	REP(i,n){
	cin>>a>>b>>c>>d;
	A[a-1][b-1]=c;
	B[a-1][b-1]=d;	
	A[b-1][a-1]=c;
	B[b-1][a-1]=d;
	}
	REP(k,m){
		REP(i,m){
		if(i==k) continue;
	 		REP(k,m){
		A[i][j]=min(A[i][j],A[i][k]+A[k][j]);
		B[i][j]=min(B[i][j],B[i][k]+B[k][j]);
		A[j][i]=A[i][j];
		B[j][i]=B[i][j];
			}
		}	
	}
	cin>>n;
	REP(i,n){
	cin>>a>>b>>c;
	if(c==0) cout<<A[a-1][b-1]<<endl;
	else cout<<B[a-1][b-1]<<endl;
	}
	cin>>n>>m;
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;

const int mx = 1<<28;

int cost[101][101];
int tIme[101][101];


int main(){
	int n , m;
	while(cin>>n>>m , n||m){
		for(int i=0;i<m;++i){
			for(int j=0;j<m;++j){
				cost[i][j]=mx;
				tIme[i][j]=mx;
			}
		}
		for(int i=0,a,b,c,t; i<n ; ++i){
			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 i=0 ; i<m ; ++i ){
			for(int j=0 ; j<m ; ++j){
				for(int k=0 ; k<m ; ++k){
					cost[j][k]=min(cost[j][k] , cost[j][i]+cost[i][k]);
					tIme[j][k]=min(tIme[j][k] , tIme[j][i]+tIme[i][k]);
				}
			}
		}
		int k;
		cin >> k;
		for(int i=0 , p , q , r ; i<k ; ++i){
			cin >> p >> q >> r;
			p--; q--;
			cout << (r ? tIme[p][q] : cost[p][q] ) << endl;
		}
	}
	return 0; 
}