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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
int dp[2][100][100];
void mainmain(){
	int n,m;
	cin>>n>>m;
	rep(i,2){
		rep(j,100){
			rep(k,100){
				dp[i][j][k]=INF;
			}
		}
	}
	rep(i,n){
		int a,b,c,d;
		cin>>a>>b>>c>>d;
		a--;
		b--;
		dp[0][a][b]=c;
		dp[0][b][a]=c;
		dp[1][a][b]=d;
		dp[1][b][a]=d;
	}
	rep(k,m){
		rep(i,m){
			rep(j,m){
				rep(l,2){
					dp[l][i][j]=min(dp[l][i][j],dp[l][i][k]+dp[l][k][j]);
				}
			}
		}
	}
	int k;
	cin>>k;
	rep(i,k){
		int p,q,r;
		cin>>p>>q>>r;
		cout<<dp[r][p-1][q-1]<<endl;
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

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



int d[100];
bool used[100];

int n,m,k;
int INF=200000000;



int main(){

	int i,j;
	int a,b,c,e;

	int cost[100][100];
	int time[100][100];


	scanf("%d %d",&n,&m);

	fill((int*)cost,(int*)(cost+m),INF);
	fill((int*)time,(int*)(time+m),INF);

	/*
	fill(d,d+m,INF);
	memset(used,0,sizeof(used));
	*/

	for(i=0;i<n;i++){
		scanf("%d %d %d %d",&a,&b,&c,&e);
		a--;
		b--;
		cost[a][b]=cost[b][a]=c;
		time[a][b]=time[b][a]=e;
	}

	scanf("%d",&k);

	for(i=0;i<k;i++){
		scanf("%d %d %d",&a,&b,&c);
		a--;
		b--;
		
		fill(d,d+m,INF);
		fill(used,used+m,false);

		d[a]=0;
		
		
		while(true){
			int v=-1;
			
			//printf("-\n");

			for(j=0;j<m;j++){
				if(!used[j] && (v==-1 || d[j]<d[v])) v=j;
			}	
			
			if(v==-1)break;
			used[v]=true;

			if(c) for(j=0;j<m;j++) d[j]=min(d[j],d[v]+time[v][j]);
			else  for(j=0;j<m;j++) d[j]=min(d[j],d[v]+cost[v][j]);
		}
		printf("%d",d[b]);
		
		

	}
	scanf("%d %d",&a,&b);


	return 0;
}



	//scanf("%s",&i);

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int > P;
const int INF = 1 << 30;
int Dijkstra(int s,int g,int r);

int m,n;

int min_cost[2][111];

typedef struct{
  vector<int>edge_to;
  vector<int>edge_cost[2];
}Node_data;

Node_data Graph[111];

priority_queue<P ,vector< P >, greater< P > >que;

int main(){
  int a,b,c,d,k;
  int s,g,r;
  while(1){
    cin >> m >> n;
    if(m == 0 && n == 0)break;
    for(int i = 0;i < m;i++){
      scanf("%d %d %d %d",&a,&b,&c,&d);
      Graph[a - 1].edge_to.push_back(b - 1);
      Graph[a - 1].edge_cost[0].push_back(c);
      Graph[a - 1].edge_cost[1].push_back(d);
      Graph[b - 1].edge_to.push_back(a - 1);
      Graph[b - 1].edge_cost[0].push_back(c);
      Graph[b - 1].edge_cost[1].push_back(d);
    }
    cin >> k;
    for(int i = 0;i < k;i++){

      cin >> s >> g >> r;

      for(int j = 0;j < n;j++){
	min_cost[r][j] = INF;
      }
      cout << Dijkstra(s - 1,g - 1,r) << endl;
    }
  }
  return(0);
}

int Dijkstra(int s,int g,int r){
  int node,cost;
  int newnode,newcost;
  min_cost[r][s] = 0;
  P a;
  a.first = 0;
  a.second = s;
  que.push(a);
  while(!que.empty()){
    node = que.top().second;
    cost = que.top().first;
    que.pop();
    if(node == g)return cost;
    if(cost > min_cost[r][node])continue;
    for(int i = 0;i < Graph[node].edge_to.size();i++){
      newnode = Graph[node].edge_to[i];
      newcost = min_cost[r][node] + Graph[node].edge_cost[r][i];
      if(min_cost[r][newnode] > newcost){
	min_cost[r][newnode] = newcost;
	a.first = newcost;
	a.second = newnode;
	que.push(a);
      }
    }
  }
  return -1;
}

C++: #include <iostream>
#include <math.h>
#include <vector>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)

int main(void){
	int n, m;
	while(cin >> n >> m, n||m){
		int cost[120][120];
		int timm[120][120];
		fill_n(*cost, 120*120, 10000000);
		fill_n(*timm, 120*120, 10000000);
		for(int i = 0; i < n; i++){
			int a, b,c,t;
			cin >> a >> b >> c >> t;
			cost[a][b] = c;
			cost[b][a] = c;
			timm[a][b] = t;
			timm[b][a] = t;
		}
		for(int k=0;k<n;++k)for(int i=0;i<n;++i)for(int j=0;j<n;++j){
			cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
			timm[i][j] = min(timm[i][j], timm[i][k] + timm[k][j]);
		}
		int k;
		cin >> k;
		rep(i, k){
			int a, b, c;
			cin >> a >> b >> c;
			if(!c)
				cout << cost[a][b] << endl;
			else
				cout << timm[a][b] << endl;
		}
	}

	return 0;
}