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]
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[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<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<utility>

using namespace std;

int n,m;

long long gc[110][110];
long long gt[110][110];

void warshallfloyd()
{
	for (int k = 0; k < m; k++)
	{
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < m; j++)
			{
				gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]);
				gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]);
			}
		}
	}
}

int main(void)
{
	while (cin >> n >> m, n || m)
	{

		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < m; j++)
			{
				gc[i][j] = 9999999;
				gt[i][j] = 9999999;
			}
		}

		for (int i = 0; i < n; i++)
		{
			int a, b, c, t;

			cin >> a >> b >> c >> t;

			a--; b--;

			gc[a][b] = gc[b][a] = c;
			gt[a][b] = gt[b][a] = t;
		}

		warshallfloyd();

		int k;

		cin >> k;

		for (int i = 0; i < k;i++)
		{
			int p, q, r;

			cin >> p >> q >> r;

			p--; q--;

			if (r == 0)
			{
				cout << gc[p][q] << endl;
			}
			else
			{
				cout << gt[p][q] << endl;
			}
		}
	}

	

	return 0;
}

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

const int INF = 1000000000;

int main()
{
	while(true){
		int n, m;
		cin >> n >> m;

		if(n==0 && m==0)
			break;

		int C[100][100];
		int T[100][100];
		for(int i=0; i<m; i++){
			for(int j=0; j<m; j++){
				if(i==j){
					C[i][j] = T[i][j] = 0;
				}
				else{
					C[i][j] = T[i][j] = INF;
				}
			}
		}

		for(int i=0; i<n; i++){
			int a, b, c, t;
			cin >> a >> b >> c >> t;

			C[a-1][b-1] = C[b-1][a-1] = c;
			T[a-1][b-1] = T[b-1][a-1] = t;
		}

		int N,p,q,r;
		int d[100][100];
		cin >> N;
		for(int I=0;I<N;I++){
			cin >> p >> q >> r;
			if(r){
				for(int i=0;i<m;i++){
					for(int j=0;j<m;j++){
						d[i][j]=T[i][j];
					}
				}
			}else{
				for(int i=0;i<m;i++){
					for(int j=0;j<m;j++){
						d[i][j]=C[i][j];
					}
				}
			}
			for(int k=0;k<m;k++){
				for(int i=0;i<m;i++){
					for(int j=0;j<m;j++){
						if(d[i][j]>d[i][k]+d[k][j]){
							d[i][j]=d[i][k]+d[k][j];
						}
					}
				}
			} 
			cout << d[p-1][q-1] << endl;
		}
	}

	return 0;
}

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

typedef pair<int,int> P;

const int MAX_V = 101;
const int INF = 1000000009;
struct edge { int to, cost, time; };
vector<edge> G[MAX_V];
int dist[MAX_V];

int dijkstra(int s, int t, int type) {
	fill(dist, dist+MAX_V, INF);
	dist[s] = 0;
	priority_queue<P, vector<P>, greater<P> > que;
	que.push(P(0, s));
	while(!que.empty()) {
		P n = que.top(); que.pop();
		int v = n.second;
		if(v == t) return dist[v];

		for(int i=0; i<G[v].size(); i++) {
			edge& e = G[v][i];
			if(type == 0) {
				if(dist[e.to] > dist[v] + e.cost) {
					dist[e.to] = dist[v] + e.cost;
					que.push(P(dist[e.to], e.to));
				}
			} else {
				if(dist[e.to] > dist[v] + e.time) {
					dist[e.to] = dist[v] + e.time;
					que.push(P(dist[e.to], e.to));
				}
			}
		}
	}
	return INF;
}

void add_edge(int from, int to, int cost, int time) {
	edge e1 = { to, cost, time };
	edge e2 = { from, cost, time };
	G[from].push_back(e1);
	G[to].push_back(e2);
}

int M, N, K;
int a, b, c, t;
int p, q, r;

int main() {
	while(scanf("%d%d", &N, &M), N|M) {
		for(int i=0; i<MAX_V; i++) G[i].clear();

		for(int i=0; i<N; i++) {
			scanf("%d%d%d%d", &a, &b, &c, &t);
			add_edge(a, b, c, t);
		}

		scanf("%d", &K);
		for(int i=0; i<K; i++) {
			scanf("%d%d%d", &p, &q, &r);
			int d = dijkstra(p, q, r);
			printf("%d\n", d);
		}
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main() {
	int rp;
	rp = 0;
	int ans[111][333];
	int f[1111], aa, bb, cc;
	int n,a,b,c,d, time[111][111],m, cost[111][111];
	while (1) {
		cin >> n >> m;
		if (n == 0 && m == 0) {
			goto a;
		}
		rp = rp + 1;
		for (int i = 1; i <= m; i++) {
			for (int z = 1; z <= m; z++) {
				if (z == i) {
					time[z][i] = 0;
					time[i][z] = 0;
					cost[i][z] = 0;
					cost[z][i] = 0;
				}
				else {
					time[z][i] = 9999;
					time[i][z] =9999;
					cost[i][z] = 9999;
					cost[z][i] = 9999;
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			cin >> a >> b >> c >> d;
			cost[a][b] = c;
			cost[b][a] = c;
			time[a][b] = d;
			time[b][a] = d;
		}
		for (int i = 1; i <= m; i++) {
			for (int z = 1; z <= m; z++) {
				for (int s = 1; s <= m; s++) {
					cost[z][s] = min(cost[z][s], cost[z][i] + cost[i][s]);
					time[z][s] = min(time[z][s], time[z][i] + time[i][s]);
				}
			}
		}
		cin >> f[rp];
		for (int i = 1; i <= f[rp]; i++) {
			cin >> aa >> bb >> cc;
			if (cc == 1) {
				ans[rp][i] = time[aa][bb];
			}
			else {
				ans[rp][i] = cost[aa][bb];
			}
		}
	}
a:;
	for (int z = 1; z <= rp; z++) {
		for (int i = 1; i <= f[z]; i++) {
			cout << ans[z][i] << endl;
		}
	}
}