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 <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define IREP(it,array) for(vector<int>::iterator it=array.begin(); it!=array.end(); ++it)
#define SREP(it,array) for(vector<string>::iterator it=array.begin(); it!=array.end(); ++it)

#define MP       make_pair
#define PB       push_back
#define ALL(x)   (x).begin(),(x).end()

const int INF = 1<<29;
const double EPS = 1e-9;
double zero(double d){
  return d < EPS ? 0.0 : d;
}

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


int main()
{
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  int n,m;
  cin >> n >> m;

  vector< vector<int> > cost(m+1, vector<int>(m+1,INF)),
    time(m+1, vector<int>(m+1,INF));

  REP(i,cost.size()){
    cost[i][i] = 0;
    time[i][i] = 0;
  }

  REP(i,n){
    int a,b,c,t;
    cin >> a >> b >> c >> t;
    cost[a][b] = c;
    cost[b][a] = c;
    time[a][b] = t;
    time[b][a] = t;
  }

  int k;
  cin >> k;
  REP(_tmp,k){
    int p,q,r;
    cin >> p >> q >> r;

    vector< vector<int> > data(m+1, 0);
    data = r ? time : cost;

    deque<bool> reached(m+1,0);
    vector<int> dis(m+1,INF);
    dis[p] = 0;

    while(true){
      int minn = -1;
      REP(i,reached.size()){
	if( ! reached[i] ){
	  if( minn==-1 || dis[minn] > dis[i] ){
	    minn = i;
	  }
	}
      }
      if( minn == q )
	break;

      reached[minn] = true;
      REP(i,data[minn].size()+1){
	dis[i] = min( dis[minn] + data[minn][i], dis[i] );
      }
    }

    cout << dis[q] << endl;
  }

  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[400][400];
int B[400][400];
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;	
	}
	REP(i,m){
		REP(j,m){
			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]);		
			}
		}	
	}
	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 <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int,int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
const int inf = 1e9+7;
const int INF = 1e18+7;
//26

int a[200][200], b[200][200];
signed main(){
	while(1){
		int n, m;
		scanf("%lld%lld", &m, &n);
		if(n == 0 and m == 0)break;
		rep(i,200)rep(j,200)a[i][j] = b[i][j] = inf;
		rep(i,200)a[i][i] = b[i][i] = 0;
		rep(i,m){
			int x, y, c, t;
			scanf("%lld%lld%lld%lld", &x, &y, &c, &t);x--;y--;
			a[x][y] = a[y][x] = min(a[x][y], c);
			b[x][y] = b[y][x] = min(b[x][y], t);
		}
		rep(i,n)rep(j,n)rep(k,n){
			a[j][k] = a[k][j]= min(a[j][k], a[j][i]+a[i][k]);
			b[j][k] = b[k][j]= min(b[j][k], b[j][i]+b[i][k]);
		}
		int k;
		scanf("%lld", &k);
		rep(i,k){
			int x, y, f;
			scanf("%lld%lld%lld", &x, &y, &f);x--;y--;
			//printf("*%lld %lld %lld*\n", x, y, a[x][y]);
			if(f == 0)printf("%lld\n", a[x][y]);
			else printf("%lld\n", b[x][y]);
		}
		/*rep(i,n){
			rep(j,n)printf("%lld ", a[i][j]);
			printf("\n");
		}*/
	}
	return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
void f(int c[][101],int t[][101],int a[],int p,int e,int n,int b,int v){
  int i;  
  if(p==e){
    if(a[p]==-1||a[p]>v)
      a[p]=v;
  }else if(a[p]==-1||a[p]>v){
    a[p]=v;
    for(i=1;i<n+1;i++){
      if(i!=p){
	if(b){
	  if(t[p][i]!=-1)
	    f(c,t,a,i,e,n,b,v+t[p][i]);
	}else{
	  if(c[p][i]!=-1)
	    f(c,t,a,i,e,n,b,v+c[p][i]);
	}
      }
    }
  }
  return;
}
int main(){
  int i;
  int c[101][101],t[101][101],a[101],m,n,o;
  int *s;
  while(cin>>m>>n&&m+n){
    memset(c,-1,sizeof(c));
    memset(t,-1,sizeof(t));
    for(i=0;i<m;i++){
      int p,q,r,s;
      cin>>p>>q>>r>>s;
      if(c[p][q]==-1||c[p][q]>r){
	c[p][q]=r;
	c[q][p]=r;
      }
      if(t[p][q]==-1||t[p][q]>s){
	t[p][q]=s;
	t[q][p]=s;
      }
    }
    cin>>o;
    s=new int[o];
    for(i=0;i<o;i++){
      int p,q,r;
      cin>>p>>q>>r;
      memset(a,-1,sizeof(a));
      f(c,t,a,p,q,n,r,0);
      s[i]=a[q];
      cout<<s[i]<<endl;
    }
    delete[]s;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#define rep(i,j) FOR(i,0,j)
#define FOR(i,j,k) for(int i=j;i<k;i++)
using namespace std;
int d[2][100][100];
int main(){
	int n,m;
	while(cin>>n>>m,n,m){
		rep(i,101)rep(j,101)rep(k,2)d[k][i][j]=99999999;
		int a,b,c,t;
		rep(i,n){
			cin>>a>>b>>c>>t;
			a--;b--;
			d[0][a][b]=d[0][b][a]=c;
			d[1][a][b]=d[1][b][a]=t;
		}
		rep(k,m)rep(i,m)rep(j,m)rep(l,2)d[l][i][j]=min(d[l][i][j],d[l][i][k]+d[l][k][j]);
		int k,p,q,r;
		cin>>k;
		rep(i,k){
			cin>>p>>q>>r;
			p--;q--;
			cout<<d[r][p][q]<<endl;
		}
	}
	return 0;
}