Go: // Warshall–Floyd Algorithm

package main

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

var scanner = bufio.NewScanner(os.Stdin)

const INF = 2000000000

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
			c_matrix[b-1][a-1] = c
			t_matrix[a-1][b-1] = t
			t_matrix[b-1][a-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 "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back


int n,m;

int C[200][200];
int T[200][200];
int d[200];


int dijk(int s,int g,int r){
    bool used[200];
    rep(i,200)used[i]=false;
    rep(i,200)d[i]=999999999;
    
    d[s]=0;
    
    while(1){
        int v=-1;
        rep(u,m){
            if( !used[u]&&(v==-1||d[u]<d[v]) )v=u;
        }
        if(v==-1)break;
        
        used[v]=true;
        
        rep(u,m){
            if(r==0)
                d[u] = min(d[u],d[v]+C[v][u]);
            else
                d[u] = min(d[u],d[v]+T[v][u]);
        }
    }
    return d[g];
}

int main(){
    while(cin>>n>>m){
        if(n==0&&m==0)break;
        
        rep(i,200)rep(j,200){
            C[i][j]=999999999;
            T[i][j]=999999999;
        }
        rep(i,n){
            int a,b,c,t;
            cin>>a>>b>>c>>t;
            a--,b--;
            C[a][b]=c;
            C[b][a]=c;
            T[a][b]=t;
            T[b][a]=t;
        }
        
        int k;
        cin>>k;
        rep(i,k){
            int s,g,r;
            cin>>s>>g>>r;
            s--,g--;
            cout<<dijk(s,g,r)<<endl;
        }
    }
}

C++: #include<iostream>

using namespace std;

#define rep(i, n) for (int i = 0 ; i < int(n); ++i)

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    int cost[100][100];
    int time[100][100];
    rep (i, 100) rep (j, 100) cost[i][j] = time[i][j] = 1e9;
    rep (i, 100) cost[i][i] = time[i][i] = 0;
    rep (i, n) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      --a;
      --b;
      cost[a][b] = min(cost[a][b], c);
      time[a][b] = min(time[a][b], t);
      cost[b][a] = min(cost[b][a], c);
      time[b][a] = min(time[b][a], t);
    }
    rep (k, m) rep (i, m) rep (j, m) {
      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;
    rep (i, k) {
      int p, q, r;
      cin >> p >> q >> r;
      --p;
      --q;
      if (r == 0) cout << cost[p][q] << endl;
      else cout << time[p][q] << endl;
    }
  }
  return 0;
}

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

#define INF 1000000;

struct line{
	int to,cost,time;
};

int n,m;
int s[100];
std::vector<std::vector<line> >sta(100);

int calc(int i,int j,int r){
	for(int k=0;k<sta[i].size();k++){
		if(r==0){
			if(sta[i][k].cost+s[i]<s[sta[i][k].to]){
				s[sta[i][k].to]=s[i]+sta[i][k].cost;
				calc(sta[i][k].to,j,r);
			}
		}else if(r==1){
			if(sta[i][k].time+s[i]<s[sta[i][k].to]){
				s[sta[i][k].to]=s[i]+sta[i][k].time;
				calc(sta[i][k].to,j,r);
			}
		}
	}
	return s[j];
}

int main(){
	while(std::cin>>n>>m){
		if(n==0&&m==0)break;
		for(int i=0;i<n;i++){
			int a,b,c,d;
			line e;
			std::cin>>a>>b>>c>>d;
			e.to=b,e.cost=c,e.time=d;
			sta[a].push_back(e);
			e.to=a;
			sta[b].push_back(e);
		}
		int k;
		std::cin>>k;
		for(int i=0;i<k;i++){
			int p,q,r;
			std::cin>>p>>q>>r;
			for(int j=0;j<=m;j++)s[j]=INF;s[p]=0;
			std::cout<<calc(p,q,r)<<std::endl;
		}
	}
	return 0;
}