Go: // Warshall–Floyd Algorithm

package main

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

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
				} else {
					c_dist[i][j] = c_matrix[i][j]
				}
				if c_matrix[i][j] == 0{
					c_prev[i][j] = -1
				} else {
					c_prev[i][j] = i
				}
				if t_matrix[i][j] == 0{
					t_dist[i][j] = INF
				} else {
					t_dist[i][j] = t_matrix[i][j]
				}
				if t_matrix[i][j] == 0{
					t_prev[i][j] = -1
				} else {
					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 <vector>
#include <cstring>
#include <queue>

using namespace std;

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)

struct edge{
    int to,money,time;
};
struct state{
    int v,d;
    bool operator<(const state& rhs)const{
        return d>rhs.d;
    }
};

priority_queue<state> q;
vector<vector<edge>> G;
int d[101];

int main(){
    int n,m;
    while(cin>>n>>m,n|m){
        G.resize(m+1);
        rep(i,n){
            int f,t,m,ti; cin>>f>>t>>m>>ti;
            G[f].push_back({t,m,ti});
            G[t].push_back({f,m,ti});
        }
        int k; cin>>k;
        rep(i,k){
            int a,b,c; cin>>a>>b>>c;
            priority_queue<state>().swap(q);
            for(int& i:d) i=1<<30;
            d[a]=0;
            q.push({a,0});
            while(q.size()){
                state sta=q.top(); q.pop();
                if(d[sta.v]<sta.d) continue;
                for(edge& e:G[sta.v]){
                    int cost=(c==0 ? e.money : e.time);
                    if(d[e.to]>d[sta.v]+cost){
                        d[e.to]=d[sta.v]+cost;
                        q.push({e.to,d[e.to]});
                    }
                }
            }
            cout<<d[b]<<endl;
        }
    }
}

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

using namespace std;

class Edge{
public:
	int from,to,cost,time;
	void Swap(){
		int self = from;
		from = to;
		to = from;
	}
	int GetValue(int r){
		if(r == 0){
			return cost;
		}else{
			return time;
		}
	}
};

vector<Edge> edge;
int n,m;

const int INF = 100000000;

int solve(const int p,const int q,const int r);

int main(){
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++){
		Edge ed;
		scanf("%d %d %d %d",&ed.from,&ed.to,&ed.cost,&ed.time);
		edge.push_back(ed);
		ed.Swap();
		edge.push_back(ed);
	}
	int k;
	int p,q,r;
	scanf("%d",&k);
	for(int i=0;scanf("%d %d",&p,&q) && p && q && i < k;i++){
		scanf("%d",&r);
		printf("%d\n",solve(p,q,r));
	}
	return 0;
}

int solve(const int p,const int q,const int r){
	vector<int> d(m+1);
	for(int i=0;i<=m;i++){
		d[i] = INF;
	}
	d[p] = 0;
	
	bool flag;
	while(1){
		flag = false;
		vector<Edge>::iterator it;
		for(it = edge.begin();it != edge.end();it++){
			if(d[(*it).from] != INF && d[(*it).to] > d[(*it).from] + (*it).GetValue(r)){
				d[(*it).to] = d[(*it).from] + (*it).GetValue(r);
				flag = true;
			}
		}
		if(!flag){
			break;
		}
	}
	
	return d[q];
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

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

typedef pair<int, int> P;

const int MAX_V = 3010;
const int INF = 1 << 30;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, m;
	while(cin >> n >> m && n != 0 && m != 0){

		vector<Edge> vec[MAX_V];
		int d[MAX_V];

		for(int i = 0; i < n; ++i){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			--a, --b;
			vec[a].push_back(Edge{b, c, t});
			vec[b].push_back(Edge{a, c, t});
		}

		int k;
		cin >> k;

		while(k > 0){
			--k;
			priority_queue<P, vector<P>, greater<P> > que;
			fill(d, d + n, INF);

			int p, q, r;
			cin >> p >> q >> r;
			--p, --q;

			d[p] = 0;
			que.push(P(0, p));

			while(!que.empty()){
				P p0 = que.top();
				que.pop();
				int v = p0.second;
				if(d[v] < p0.first) continue;
				for(int i = 0; i < vec[v].size(); ++i){
					Edge e = vec[v][i];
					if(r == 0 && d[e.to] > d[v] + e.cost){
						d[e.to] = d[v] + e.cost;
						que.push(P(d[e.to], e.to));
					}
					if(r == 1 && d[e.to] > d[v] + e.time){
						d[e.to] = d[v] + e.time;
						que.push(P(d[e.to], e.to));
					}
				}
			}

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

	return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<math.h>
using namespace std;
  
int min(int x, int y){
	if(x<y){
		return x;
	}else{
		return y;
	}
}

int co(int x,int y,int m,int c[][101]){
	int i,j,k,d[101],s[101];
	for(i=0;i<=100;i++){
		d[i]=2000;
		s[i]=0;
	}
	for(i=1;i<=m;i++){
		s[i]=1;
	}
	d[x]=0;
	for(i=1;i<=m;i++){
		k=0;
		for(j=1;j<=m;j++){
			if(s[j]==1){
				if(k==0){
					k=j;
				}else if(d[j]<d[k]){
					k=j;
				}
			}
		}
		for(j=1;j<=m;j++){
			if(c[k][j]!=0){
				d[j]=min(d[j],d[k]+c[k][j]);
			}
		}
		s[k]=0;
	}
	return d[y];
}
int ti(int x,int y,int n,int t[][101]){
	int i,j,k,d[101],s[101];
	for(i=0;i<=100;i++){
		d[i]=2000;
		s[i]=0;
	}
	for(i=1;i<=n;i++){
		s[i]=1;
	}
	d[x]=0;
	for(i=1;i<=n;i++){
		k=0;
		for(j=1;j<=n;j++){
			if(s[j]==1){
				if(k==0){
					k=j;
				}else if(d[j]<d[k]){
					k=j;
				}
			}
		}
		for(j=1;j<=n;j++){
			if(t[k][j]!=0){
				d[j]=min(d[j],d[k]+t[k][j]);
			}
		}
		s[k]=0;
	}

	return d[y];
}

int main(){
	while(1){
		int i,j,a,b,k,n,m,p,q,r,c[101][101],t[101][101];
		for(i=0;i<=100;i++){
			for(j=0;j<=100;j++){
				c[i][j]=0;
				t[i][j]=0;
			}
		}
		cin>>n>>m;
		if(n==0&&m==0){break;}
		for(i=0;i<n;i++){
			cin>>a>>b;
			cin>>c[a][b]>>t[a][b];
			c[b][a]=c[a][b];
			t[b][a]=t[a][b];
		}
		cin>>k;
		for(i=0;i<k;i++){
			cin>>p>>q>>r;
			if(r==0){
				cout<<co(p,q,m,c)<<endl;
			}else if(r==1){
				cout<<ti(p,q,m,t)<<endl;
			}
		}
	}

    return 0;
}