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
			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;
int main(){
	int d[2][101][101];
	int n,m;
	while(cin>>n>>m,n,m){
	for(int i=1;i<=m;i++)for(int j=i;j<=m;j++)for(int k=0;k<2;k++)d[k][i][j]=999999999;
	for(int i=0;i<n;i++){
		int a,b,c,t;
		cin>>a>>b>>c>>t;
		d[0][a][b]=d[0][b][a]=c;
		d[1][a][b]=d[1][b][a]=t;
	}
	for(int k=1;k<=m;k++){
		for(int i=1;i<=m;i++){
			for(int j=1;j<=m;j++){
				d[0][i][j]=min(d[0][i][j],d[0][i][k]+d[0][k][j]);
				d[1][i][j]=min(d[1][i][j],d[1][i][k]+d[1][k][j]);
			}
		}
	}
	int kl;
	cin>>kl;
	for(int i=0;i<kl;i++){
		int p,q,r;
		cin>>p>>q>>r;
		cout<<d[r][p][q]<<endl;
	}
	}
}

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

using namespace std;

int main() {
	int a, b, time, cost, querys;
	int i, j, k, p, q, r, n, m;
	int times[100][100], costs[100][100];

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

		for (i = 0; i < m; i++) {
			for (j = 0; j < m; j++) {
				times[i][j] = (i == j) ? 0 : 1e6;
				costs[j][i] = (i == j) ? 0 : 1e6;
			}
		}

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

		for (k = 0; k < m; k++) {
			for (i = 0; i < m; i++) {
				for (j = 0; j < m; j++) {
					times[i][j] = min(times[i][j], times[i][k] + times[k][j]);
					costs[i][j] = min(costs[i][j], costs[i][k] + costs[k][j]);
				}
			}
		}

		cin >> querys;
		for (i = 0; i < querys; i++) {
			cin >> p >> q >> r;
			cout << (r ? times[p - 1][q - 1] : costs[p - 1][q - 1]) << endl;
		}
	}
	return 0;
}

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

int V, E, cost[100][100][2], d[100], done[100];

int dj(int s, int k)
{
  fill(d, d + 100, INF);
  fill(done, done + 100, 0);
  d[s] = 0;

  while(1)
  {
    int v = -1;
    for(int i = 0; i < V; i++)
      if(!done[i] && (v == -1 || d[i] < d[v]))
        v = i;
    if(v == -1)
      break;
    done[v] = 1;
    for(int i = 0; i < V; i++)
      d[i] = min(d[i], d[v] + cost[v][i][k]);
  }
}

int main()
{
loop:
  fill((int*)cost, (int*)cost + 20000, INF);
  cin >> E >> V;
  if(!E)
    return 0;

  for(int i = 0; i < E; i++)
  {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--; b--;
    cost[a][b][0] = cost[b][a][0] = c;
    cost[a][b][1] = cost[b][a][1] = d;
  }
  int k;
  cin >> k;
  for(int i = 0; i < k; i++)
  {
    int p, q, r;
    cin >> p >> q >> r;
    p--; q--;
    dj(p, r);
    cout << d[q] << endl;
  }
  goto loop;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstring>
#include <tuple>
#include <utility>
#include <list>
#include <cmath>
#include <unordered_map>

using namespace std;

typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;

const int INF = 1 << 29;
const LL MOD = 100000;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define all(c) (c).begin(), (c).end()

struct edge{ int to, cost; edge(int t, int c) :to(t), cost(c){} };

int main()
{
	//while (cin >> n)
	stringstream ss;
	//ss << "result" << i + 1 << ".txt";
	//FILE* fp_out = freopen(ss.str().c_str(), "w", stdout);

	int n, m;
	while (cin >> n >> m && n)
	{
		vector<vector<int>> times(m, vector<int>(m, INF));
		vector<vector<int>> costs(m, vector<int>(m, INF));
		rep(i, n)
		{
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			a--; b--;
			costs[a][b] = costs[b][a] = c;
			times[a][b] = times[b][a] = t;
		}

		rep(k, m) rep(i, m) rep(j, m)
		{
			costs[i][j] = min(costs[i][j], costs[i][k] + costs[k][j]);
			times[i][j] = min(times[i][j], times[i][k] + times[k][j]);
		}

		int k; cin >> k;
		rep(i, k)
		{
			int p, q, r; cin >> p >> q >> r;
			p--; q--;
			cout << (r ? costs[p][q] : times[p][q]) << endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>
#define INF 1000000
using namespace std;

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

vector<line> L;
int costv[100];
int timev[100];

void mincost(int from,int to) {
  for(int i=0;i<100;i++) {
    costv[i] = INF;
  }
  costv[from] = 0;
  while(1) {
    bool update = false;
    for(unsigned int i=0;i<L.size();i++) {
      line l = L[i];
      if(costv[l.from] != INF && costv[l.to] > costv[l.from] + l.cost) {
	costv[l.to] = costv[l.from] + l.cost;
	update = true;
      }
    }
    if(!update) break;
  }
  cout << costv[to] << endl;
}

void mintime(int from,int to) {
  for(int i=0;i<100;i++) {
    timev[i] = INF;
  }
  timev[from] = 0;
  while(1) {
    bool update = false;
    for(unsigned int i=0;i<L.size();i++) {
      line l = L[i];
      if(timev[l.from] != INF && timev[l.to] > timev[l.from] + l.time) {
	timev[l.to] = timev[l.from] + l.time;
	update = true;
      }
    }
    if(!update) break;
  }
  cout << timev[to] << endl;
}

int main() {
  int n,m,k;
  while(cin >> n >> m) {
    if(n==0&&m==0)break;
    for(int i=0;i<n;i++) { //input line
      line temp;
      int f,t,ti,co;
      cin >> f >> t >> co >> ti;
      temp.from = f;
      temp.to = t;
      temp.time = ti;
      temp.cost = co;
      L.push_back(temp);
      temp.from = t;
      temp.to = f;
      L.push_back(temp);
    }

    cin >> k;
    for(int i=0;i<k;i++) {
      int p,q,r;
      cin >> p >> q >> r;
      if(r == 0)
	mincost(p,q);
      else
	mintime(p,q);
    }
  }
}