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<algorithm>
#include<vector>
#include<queue>
#define mod 1000000007
#define lol(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> PP;
int main(){
	int n,m;
	while(cin>>m>>n&&n+m){
		priority_queue<P,vector<P>,greater<P> >Q;
		vector<PP> v[110];
		int dis[110];
		lol(i,m){
			int a,b,c,d;
			cin>>a>>b>>c>>d;
			v[a].push_back(make_pair(b,make_pair(c,d)));
			v[b].push_back(make_pair(a,make_pair(c,d)));
		}
		cin>>m;
		lol(u,m){
			lol(i,n)dis[i+1]=mod;
			int s,t,c;cin>>s>>t>>c;
			Q.push(make_pair(s,0));
			while(!Q.empty()){
				int a=Q.top().first,b=Q.top().second;
				Q.pop();
				if(dis[a]<mod)continue;
				dis[a]=b;
				lol(i,v[a].size()){
					int to=v[a][i].first,co=b+(c?v[a][i].second.second:v[a][i].second.first);
					if(dis[to]==mod)Q.push(make_pair(to,co));
				}
			}
			cout<<dis[t]<<endl;
		}
	}
	return 0;
}

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

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

int c[100][100];
int t[100][100];

int main() {
    while(true) {
        int n, m;
        cin >> n >> m;
        if(!n && !m) break;

        rep(i, m) {
            rep(j, m) {
                c[i][j] = t[i][j] = (i != j ? 1001 : 0);
            }
        }

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

        rep(i, m) {
            rep(j, m) {
                rep(k, m) {
                    c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
                    t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
                }
            }
        }

        int k;
        cin >> k;
        rep(i, k) {
            int p, q, r;
            cin >> p >> q >> r;
            p--; q--;
            cout << (r == 0 ? c[p][q] : t[p][q]) << endl;
        }
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
class NextNodesData
{
public:
	int nextNodesName;
	int nextNodesMany;
	int nextNodesTime;
};
class Nodes
{
public:
	vector<NextNodesData> nextNodes;
	int name;
	int minValue;
};

int main()
{
	int N, M;
	cin >> N >> M;
	vector<Nodes> nodesList;
	for(int i = 0; i < M; i++)
	{
		Nodes nodes;
		nodes.name = i+1;
		nodesList.push_back(nodes);
	}
	for(int i = 0; i < N; i++)
	{
		int Start;
		int End;
		int Many;
		int Time;
		cin >> Start >> End >> Many >> Time;

		NextNodesData nodes;
		nodes.nextNodesName = End;
		nodes.nextNodesMany = Many;
		nodes.nextNodesTime = Time;
		nodesList[Start - 1].nextNodes.push_back(nodes);
	}

	int K;
	cin >> K;
	for(int i = 0; i < K; i++)
	{
		for(int j = 0; j < nodesList.size(); j++)
		{
			nodesList[j].minValue = -1;
		}
		int Start;
		int End;
		int mode;
		vector<int> candidateNodes;
		
		cin >> Start >> End;
		cin >> mode;

		nodesList[Start - 1].minValue = 0;
		candidateNodes.push_back(Start);
		int minValue = 1001;
		while(1)
		{
			minValue = 1001;
			int nextNodesName = -1;
			//????¢???????????????????????????????????¬?????¢???????????????????????????¢??????
			for(int j = 0; j < candidateNodes.size(); j++)
			{
				for(int k = 0; k < nodesList[candidateNodes[j] - 1].nextNodes.size(); k++)
				{
					bool thisNodesIsNotFixed = false;

					for(int l = 0; l < candidateNodes.size(); l++)
					{
						if(candidateNodes[l] == nodesList[candidateNodes[j] - 1].nextNodes[k].nextNodesName)
						{
							thisNodesIsNotFixed = true;
						}
					}
					if(thisNodesIsNotFixed)
					{
						continue;
					}

					int value;
					value = 0;
					if(mode == 0)
					{
						value = nodesList[candidateNodes[j]-1].minValue;
						value += nodesList[candidateNodes[j]-1].nextNodes[k].nextNodesMany;
					}
					else
					{
						value = nodesList[candidateNodes[j] - 1].minValue;
						value += nodesList[candidateNodes[j] - 1].nextNodes[k].nextNodesTime;
					}
					if(value < minValue)
					{
						minValue = value;
						nextNodesName = nodesList[candidateNodes[j] - 1].nextNodes[k].nextNodesName;
					}

				}
			}
			//???????????§??§????¬?????¢??????????????????????????±????

			nodesList[nextNodesName - 1].minValue = minValue;
			candidateNodes.push_back(nextNodesName);

			if(nextNodesName == End)
			{
				break;
			}
		}
		cout << nodesList[End - 1].minValue << endl;


	}


}

C++: #include <iostream>
#include <algorithm>

using namespace std;
#define INF 1e9

void solve()
{
	int dp[2][101][101];
	int n, m;
	while (cin >> n >> m, n || m)
	{
		for (int i = 0; i < 101; ++i)
		{
			for (int j = 0; j < 101; ++j)
			{
				if (i == j)
				{
					dp[0][i][j] = dp[1][i][j] = 0;
				}
				else
				{
					dp[0][i][j] = dp[1][i][j] = INF;
				}
			}
		}
		int a, b, cost, time;
		for (int i = 0; i < n; ++i)
		{
			cin >> a >> b >> cost >> time;
			dp[0][a][b] = cost;
			dp[0][b][a] = cost;
			dp[1][a][b] = time;
			dp[1][b][a] = time;
		}
		for (int k = 1; k <= m; ++k)
		{
			for (int i = 1; i <= m; ++i)
			{
				for (int j = 1; j <= m; ++j)
				{
					dp[0][i][j] = min(dp[0][i][j], dp[0][i][k] + dp[0][k][j]);
					dp[1][i][j] = min(dp[1][i][j], dp[1][i][k] + dp[1][k][j]);
				}
			}
		}
		int inq;
		cin >> inq;
		for (int i = 0; i < inq; ++i)
		{
			int p, q, r;
			cin >> p >> q >> r;
			cout << dp[r][p][q] << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}