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
			c_matrix[b-1][a-1] = c
			t_matrix[a-1][b-1] = t
			t_matrix[b-1][a-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<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T1, class T2> inline void minup(T1& m, T2 x){ if(m>x) m=static_cast<T2>(x); }
template<class T1, class T2> inline void maxup(T1& m, T2 x){ if(m<x) m=static_cast<T2>(x); }

#define INF 1000000000LL
#define MOD 1000000009
#define EPS 1E-9

const int MAX_V = 1000;
ll dp[2][MAX_V][MAX_V];
int E, V;
int a, b;
ll c, t;
int k;
int p, q, r;

int main()
{
  //  while((cin >> E >> V) && E && V){
  while(scanf(" %d %d", &E, &V) && E && V){
    rep(i, 101) rep(j, 101) dp[0][i][j] = dp[1][i][j] = INF;

    rep(i, E){
      //      cin >> a >> b >> c >> t;
      scanf(" %d %d %d %d", &a, &b, &c, &t);
      a-=1;  b-=1;
      dp[0][a][b] = dp[0][b][a] = c;
      dp[1][a][b] = dp[0][b][a] = t;
    }

    rep(b, 2) rep(k, V) rep(i, V) rep(j, V) minup(dp[b][i][j], dp[b][i][k] + dp[b][k][j]);

    //    cin >> k;
    scanf(" %d", &k);
    while(k--){
      //      cin >> p >> q >> r;
      scanf(" %d %d %d", &p, &q, &r);
      //      cout << dp[r][p-1][q-1] << endl;
      printf("%lld\n", dp[r][p-1][q-1]);
    }
  }

  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;
	bool fixed;
};

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 j = 0; j < N; j++)
	{
		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);

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

	int Query;
	cin >> Query;

	for(int k = 0; k < Query; k++) 
	{
		
		 //???????????????
		for(int l = 0; l < nodesList.size(); l++)
		{
			nodesList[l].fixed = false;
		}
		int Start;
		int End;
		int mode;
		
		cin >> Start >> End;
		cin >> mode;

		if(Start == End)
		{
			cout << 0 << endl;
			continue;
		}

		nodesList[Start - 1].fixed = true;
		nodesList[Start - 1].minValue = 0;
		int nextFixedNodesName = Start;

		

		vector<Nodes> candidate;
		while(1)
		{
			//??????????¢????????????°??????????´????????????????????????¨???????????????????????????
			for(int m = 0; m < nodesList[nextFixedNodesName - 1].nextNodes.size(); m++)
			{
				Nodes obj;
				obj.name = nodesList[nextFixedNodesName - 1].nextNodes[m].nextNodesName;
				//??¢??????????????????????????????????????????????????????
				if(nodesList[obj.name - 1].fixed == true)
				{
					continue;
				}
				if(mode == 0)
				{
					obj.minValue = nodesList[nextFixedNodesName - 1].minValue + nodesList[nextFixedNodesName - 1].nextNodes[m].nextNodesMany;
				}
				else
				{
					obj.minValue = nodesList[nextFixedNodesName - 1].minValue + nodesList[nextFixedNodesName - 1].nextNodes[m].nextNodesTime;
				}
				//?¬???????????????????????????????????????????
				candidate.push_back(obj);				   
			}

			int minValue = 1000001;
			int minName  = -1;
			//?¬????????????????????£???????????°???????????????¢???
			for(int n = 0; n < candidate.size(); n++)
			{
				if(candidate.size() == 0)
				{
					cout << 0 << endl;
				}
				if(candidate[n].minValue < minValue)
				{
					minValue = candidate[n].minValue;
					minName = candidate[n].name;
				}  
			}
			if(minName == -1)
			{
				cout << 0 << endl;
				break;
			}
			if(minName == End)
			{
				cout << minValue << endl;
				break;
			}
			nodesList[minName - 1].minValue = minValue;
			nodesList[minName - 1].fixed = true;
			nextFixedNodesName = minName;

			
			for(auto itr = candidate.begin(); itr != candidate.end();)
			{
				if(nodesList[itr->name - 1].fixed == true)
				{
					itr = candidate.erase(itr);
				}
				else
				{
					itr++;
				}
			}
		}	
		

	} 

	return 0;

}

C++: #include<stdio.h>
#define INF 100001
int e1[100][100],e2[100][100];
int d[100];
bool used[100];
int m,n;
int p,q,r;
int k;
int i,j;
int a,b,t,c;
int v;
int mc,mt;

int min(int a,int b){
	if(a<=b)return a;
	else return b;
}

void solve(int s,int mo){
	for(j=0;j<m;j++){
		used[j]=false;
		d[j]=INF;
	}
	d[s]=0;
	while(true){
		v=-1;
		for(int u=0;u<m;u++)if(!used[u]&&(v==-1||d[u]<d[v]))v=u;
		if(v==-1)break;
		used[v]=true;
		for(int u=0;u<m;u++){
			if(mo==0)d[u]=min(d[u],d[v]+e1[v][u]);
			else d[u]=min(d[u],d[v]+e2[v][u]);
		}
	}
}


int main(){
	while(1){
		scanf("%d %d",&n,&m);
		if(n==0&&m==0)break;
		mt=INF;
		mc=INF;
		for(i=0;i<m;i++)for(j=0;j<m;j++){
			e1[i][j]=INF;
			e2[i][j]=INF;
		}
		for(i=0;i<n;i++){
			scanf("%d %d %d %d",&a,&b,&c,&t);
			e1[a-1][b-1]=c;
			e1[b-1][a-1]=c;
			e2[a-1][b-1]=t;
			e2[b-1][a-1]=t;
		}
		scanf("%d",&k);
		for(i=0;i<k;i++){
			scanf("%d %d %d",&p,&q,&r);
			solve(p-1,r);
			if(r==0)mc=min(mc,d[q-1]);
			else mt=min(mt,d[q-1]);
		}
		printf("%d\n%d\n",mc,mt);
	}
}