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>
using namespace std;
static const int INFTY = (1 << 21);
int main(){
	cout << INFTY;
	int t[105][105];int c[105][105];
	int n,m,a,b,cost,time,kk,p,q,r;
	while(cin >> n >> m,n,m){
		for(int i=1;i<105;i++){
			for(int j=1;j<105;j++){
				t[i][j] = c[i][j] = INFTY;
			}
		} 
		for(int i = 0;i < n;i++){
			cin >> a >> b >> cost >> time;
			t[a][b] =t[b][a] = time;
			c[a][b] =c[b][a] = cost;
		}
		for(int k=1;k<=m;k++){
			for(int i=1;i<=m;i++){
				if(t[i][k] == INFTY) continue;
				for(int j=1;j<=m;j++){
					if(t[i][k] == INFTY) continue;
					t[i][j] = min(t[i][j],t[i][k]+t[k][j]);
					c[i][j] = min(c[i][j],c[i][k]+c[k][j]);
				}
			}
		}
		cin >>  kk;
		for(int i=0;i<kk;i++){
			cin >> p >> q >> r;
			if(r == 0) cout << c[p][q] << endl;
			else cout << t[p][q] << endl;
		}
	}
	return 0;
}

C++: 
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <tuple>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<vector<PII> > VVPII;
typedef priority_queue<PII, vector<PII>, greater<PII> > PQPII;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int DX[] = {0, 1, 0, -1};
const int DY[] = {-1, 0, 1, 0};

//other
//--------------------------------------------
#define RANGE(a, c, b) ((a <= b) && (b < c))

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

int dijkstra(int from, int to, VVPII & nodes, int station_count){
	VI dist(station_count, 10e7);
	dist[from] = 0;
	PQPII q;
	q.emplace(0, from);
	while(!q.empty()){
		PII task = q.top();
		int cost = task.first;
		int station = task.second;
		q.pop();
		if(dist[station] < cost) continue;
		for(PII node : nodes[station]){
			int to_cost = node.first;
			int to_station = node.second;
			if(to_cost + dist[station] < dist[to_station]){
				dist[to_station] = to_cost + dist[station];
				q.emplace(dist[to_station], to_station);
			}
		}
	}
	return dist[to];
}

int main(int argc, char const *argv[])
{
	for(int n, m;cin  >> n >> m, n | m;){
		VVPII fs(m + 1), ts(m + 1);
		REP(i, n){
			int from, to, f, t;
			cin >> from >> to >> f >> t;
			fs[from].PB(MP(f, to));
			ts[from].PB(MP(t, to));
			fs[to].PB(MP(f, from));
			ts[to].PB(MP(t, from));
		}
		int request;
		cin >> request;
		REP(i, request){
			int from, to, type;
			cin >> from >> to >> type;
			int ans = dijkstra(from, to, (type ? ts : fs), m + 1);
			cout << ans << endl;
		}
	}
	
	return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i,j,k;
  int c[100][100],t[100][100],m,n,o;
  while(cin>>n>>m&&n+m){
    for(i=0;i<m;i++){
      for(j=0;j<m;j++){
	c[i][j]=10000;
	t[i][j]=10000;
      }
    }
    for(i=0;i<n;i++){
      int p,q,r,s;
      cin>>p>>q>>r>>s;
      p--;
      q--;
      c[p][q]=min(c[p][q],r);
      c[q][p]=c[p][q];
      t[p][q]=min(t[p][q],s);
      t[q][p]=t[p][q];
    }
    for(k=0;k<m;k++){
      for(i=0;i<m;i++){
	for(j=0;j<m;j++){
	  c[i][j]=min(c[i][j],c[i][k]+c[k][j]);
	  t[i][j]=min(t[i][j],t[i][k]+t[k][j]);
	}
      }
    }
    cin>>o;
    for(i=0;i<o;i++){
      int p,q,r;
      cin>>p>>q>>r;
      p--;
      q--;
      if(r)
	cout<<t[p][q]<<endl;
      else
	cout<<c[p][q]<<endl;
    }
  }
  return 0;
}