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<queue>
#include<vector>
#include<limits>

using namespace std;

struct RequestObj {
	int begin, end, type;
};

int n, m;//???,???????????°

const int INF = 100000000;

int main() {
	while (cin >> n >> m, n * m) {

		int k;//????????¨????????°
		int time_table[100][100];
		int cheap_table[100][100];
		RequestObj request[200];


		for (int i = 0; i < 100; i++)for (int j = 0; j < 100; j++)time_table[i][j] = INF, cheap_table[i][j] = INF;

		for (int i = 0; i < n; i++) {
			int a, b;
			cin >> a >> b;
			cin >> cheap_table[a - 1][b - 1] >> time_table[a - 1][b - 1];
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> request[i].begin >> request[i].end >> request[i].type;
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				for (int k = 0; k < m; k++) {
					if (cheap_table[j][i] + cheap_table[i][k] < cheap_table[j][k]) {
						cheap_table[j][k] = cheap_table[j][i] + cheap_table[i][k];
					}
				}
			}
		}

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				for (int k = 0; k < m; k++) {
					if (time_table[j][i] + time_table[i][k] < time_table[j][k]) {
						time_table[j][k] = time_table[j][i] + time_table[i][k];
					}
				}
			}
		}
		for (int i = 0; i < k; i++) {
			if (request[i].type == 0) {
				cout << cheap_table[request[i].begin - 1][request[i].end - 1] << endl;
			}
			else {
				cout << time_table[request[i].begin - 1][request[i].end - 1] << endl;
			}
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;

int cost[101][101],t[101][101];
bool tf,cf;

int wf(int p,int q,int r,int m){
	if(r){
		if(tf){
				for(int i=1 ; i <= m ; ++i){
					for(int j=1 ; j <= m ; ++j){
			for(int k=1 ; k <= m ; ++k){
						if(t[i][j] > t[i][k] + t[k][j]) t[i][j] = t[i][k] + t[k][j];
					}
				}
			}
			tf = false;
			return t[p][q];
		}else return t[p][q];
	}else if(cf){
		for(int k=1 ; k <= m ; ++k){
			for(int i=1 ; i <= m ; ++i){
				for(int j=1 ; j <= m ; ++j){
						if(cost[i][j] > cost[i][k] + cost[k][j]) cost[i][j] = cost[i][k] + cost[k][j];
				}
			}
		}
		cf = false;
		return cost[p][q];
	}
	else return cost[p][q];
}

int main(){
	int a,b,c,d,n,m,k,p,q,r;
	
	while(cin >> n >> m && n && m){
		for(int i=1 ; i <= m ; ++i){
			for(int j=1 ; j <= m ; ++j){
				cost[i][j] = 10000;
				t[i][j] = 10000;
			}
		}
		cf = true;
		tf = true;
		for(int i=0 ; i < n ; ++i){
			cin >> a >> b >> c >> d;
			cost[a][b] = c;
			cost[b][a] = c;
			t[a][b] = d;
			t[b][a] = d;
		}
		cin >> k;
		for(int i=0 ; i < k ; ++i){
			cin >> p >> q >> r;
			cout << wf(p,q,r,m) << endl;
		}
	}
	return 0;
}

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

#define MAX_M 110

struct edge{int to,cost;};
typedef pair<int,int> P;

vector<edge> c[2][MAX_M];

int dijkstra(int s,int e,bool r){
	int d[MAX_M];
	bool use[MAX_M];
	for(int i=0;i<MAX_M;i++){
		d[i]=INT_MAX;
		use[i]=0;
	}
	priority_queue<P,vector<P>,greater< P > > que;
	
	d[s]=0;
	
	que.push(P(0,s));
	int ans;
	while(!que.empty()){
		P p=que.top();que.pop();
		//cout<<"pop "<<p.second<<" "<<p.first<<endl;
		int v=p.second;
		if(v==e){
			ans=p.first;
			break;
		}
		
		if(use[v]) continue;
		use[v]=1;
		//cout<<"use "<<p.second<<" "<<p.first<<endl;
		for(int i=0;i<c[r][v].size();i++){
			edge e=c[r][v][i];
			//if(!use[e.to]){
				d[e.to]=d[v]+e.cost;
				que.push(P(d[e.to],e.to));
			//}
		}
	}
	
	return d[e];
}

int main(){
	int n,m;
	while(cin>>n>>m,n!=0||m!=0){
		for(int r=0;r<2;r++){
			for(int i=0;i<MAX_M;i++){
				c[r][i].clear();
			}
		}
		for(int i=0;i<n;i++){
			int a,b,cos,tim;
			cin>>a>>b>>cos>>tim;
			edge co,t;
			co.to=b;co.cost=cos;
			t.to=b;t.cost=tim;
			c[0][a].push_back(co);
			c[1][a].push_back(t);
			co.to=a;t.to=a;
			c[0][b].push_back(co);
			c[1][b].push_back(t);
		}
		int k;
		cin>>k;
		for(int i=0;i<k;i++){
			int s,e;
			bool r;
			cin>>s>>e>>r;
			int ans=dijkstra(s,e,r);
			cout<<ans<<endl;
		}
	}
}

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

#define REP(i,p,n) for(int i=p;i<n;++i)
#define rep(i,n) REP(i,0,n)

typedef pair<int,int> pii;
const int INF=1e9, MAXM=128;
int C[2][MAXM][MAXM], dist[MAXM], visited[MAXM], N, M;

int dk(int p,int q,int r)
{
        dist[p]=0;

        while(true)
        {
                int from=-1,c=INF;
                rep(i,M) if(!visited[i]&&dist[i]<c){ c=dist[i]; from=i; }
                if(from==q) return dist[q];
                visited[from]=true;

                rep(to,M) if(dist[from]+C[r][from][to]<dist[to]){ dist[to]=dist[from]+C[r][from][to]; }
        }
        return 0;
}

int main()
{
        int a,b,c,d;
        cin>>N>>M;
        rep(i,M) rep(j,N) C[0][i][j]=C[1][i][j]=(i==j?0:INF);
        rep(i,N){ cin>>a>>b>>c>>d; --a; --b; C[0][a][b]=C[0][b][a]=c; C[1][a][b]=C[1][b][a]=d; }

        cin>>d;
        rep(i,d){ fill(dist,dist+M,INF); fill(visited,visited+M,false); cin>>a>>b>>c; cout<<dk(a-1,b-1,c)<<endl; }
        return 0;
}