Go: // Warshall–Floyd Algorithm

package main

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

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
					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 <cstdio>
#include <algorithm>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
#define INF (1<<29)
using namespace std;
int C[2][100][100];
int main()
{
	while (true) {
		int n, m; scanf("%d %d", &n, &m);
		if (n == 0 && m == 0) break;
		rep(k, 2) rep(i, 100) {
			rep(j, 100) { C[k][i][j] = INF; }
			C[k][i][i] = 0;
		}
		rep(i, n) {
			int a, b, c, t; scanf("%d%d%d%d", &a, &b, &c, &t);
			a--, b--;
			C[0][a][b] = C[0][b][a] = c;
			C[1][a][b] = C[1][b][a] = t;
		}
		rep(k, m) rep(i, m) rep(j, m) rep(l, 2) {
			C[l][i][j] = min(C[l][i][k] + C[l][k][j], C[l][i][j]);
		}
		int k; scanf("%d", &k);
		rep(i, k) {
			int p, q, r; scanf("%d%d%d", &p, &q, &r);
			printf("%d\n", C[r][p-1][q-1]);
		}
	}
	return 0;
}

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

array<array<int, 110>, 110> costs;
array<array<int, 110>, 110> times;

int main() {
    int n, m;
    while (cin >> n >> m, n || m) {
        for (auto& ar : costs) ar.fill(1000000);
        for (auto& ar : times) ar.fill(1000000);
        for (int i = 0; i < n; i++) {
            int a, b, c, t;
            cin >> a >> b >> c >> t;
            a--; b--;
            costs[a][b] = costs[b][a] = c;
            times[a][b] = times[b][a] = t;
        }
        for (int i = 0; i < n; i++) {
            costs[i][i] = 0;
            times[i][i] = 0;
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    costs[j][k] = min(costs[j][k], costs[j][i] + costs[i][k]);
                    times[j][k] = min(times[j][k], times[j][i] + times[i][k]);
                }
            }
        }
        int n;
        for (cin >> n; n; n--) {
            int p, q, r;
            cin >> p >> q >> r;
            p--; q--;
            if (r == 0) {
                cout << costs[p][q] << endl;
            }
            else {
                cout << times[p][q] << endl;
            }
        }
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <limits.h>
using namespace std;


				
int main()
{
	int n,m,a,b,co,ti,p,r;
	int money[100][100];
	int time[100][100];

	while(cin >> n >> m && n && m)
	{
		/*[m-1][m-1]ツづ慊づづ個配ツ療アツづーツ渉可甘コツ可サ*/
		for(int i=0;i<100;i++)
			for(int j=0;j<100;j++)
				time[i][j]=money[i][j]= (i-j) ? (1<<22) : 0;

		for(int i=0;i<n;i++)
		{
			cin >> a >> b >> co >> ti;
			money[a-1][b-1]=money[b-1][a-1]=co;
			time[a-1][b-1]=time[b-1][a-1]=ti;
		}
		/*ツδ渉ーツシツδδδ仰フツδ債イツドツ法 for time*/
		for(int k=0;k<m;k++)
			for(int i=0;i<m;i++)
				for(int j=0;j<m;j++)
					time[i][j]=time[j][i]=min(time[i][j],time[i][k]+time[k][j]);
					
		/*ツδ渉ーツシツδδδ仰フツδ債イツドツ法 for cost*/	
		for(int k=0;k<m;k++)
			for(int i=0;i<m;i++)
				for(int j=0;j<m;j++)
					money[i][j]=money[j][i]=min(money[i][j],money[i][k]+money[k][j]);
		cin >> p;
		
		/*
		for(int i=0;i<m;i++)
		{
			
			for(int j=0;j<m;j++)printf("%10d",money[i][j]);
			cout << endl;
		}
		*/
		
		for(int i=0;i<p;i++)
		{
			cin >> a >> b >> r;
			cout <<(r?time[a-1][b-1]:money[a-1][b-1])<< endl;
		}
	}
}

C++: #include<stdio.h>
int mt[100][100]={{0}};
int mm[100][100]={{0}};
int ct[101][101];
int cm[101][101];
int Q[10000];
int main(){
  int n,m,q,r,i,j,k,l,s,g,h,c;
  int a,b,cost,time;
  while(1){
    scanf("%d %d",&n,&m);
    if(n==0&&m==0)break;
    for(i=0;i<n;i++){
      scanf("%d %d %d %d",&a,&b,&cost,&time);
      mt[--a][--b]=time;
      mt[b][a]=time;
      mm[a][b]=cost;
      mm[b][a]=cost;
    }
    for(i=0;i<m;i++){
      for(j=0;j<m;j++){
	cm[i][j]=1000000000;
	ct[i][j]=1000000000;
      }
    }
    for(s=0;s<m;s++){
      for(g=0;g<m;g++){
	//scanf("%d",&k);
	//for(i=0;i<k;i++){
	//scanf("%d %d %d",&s,&g,&h);
	Q[0]=s;
	q=0;
	r=1;
	//s--;
	//g--;
	c=0;
	int checkm[100]={0};
	for(j=0;j<m;j++){
	  if(mm[g][j]!=0)c++;
	  checkm[j]=1;
	}
	checkm[0]=0;
	cm[s][s]=0;
	ct[s][s]=0;
	//if(h==0){
	while(q!=r){
	  for(j=0;j<m;j++){//printf("%d,%d,%d\n",Q[q],j,mm[Q[q]][j]);
	    if(mm[Q[q]][j]!=0){
	      if(checkm[j]==1){
		Q[r]=j;
		//check[j]=0;
		//printf("%d %d %d %d %d\n",Q[q],j,cm[j],cm[Q[q]],mm[Q[q]][j]);
		if(cm[s][j]>cm[s][Q[q]]+mm[Q[q]][j])cm[s][j]=cm[s][Q[q]]+mm[Q[q]][j];
		r++;
	      }
	    }
	  }
	  if(Q[q]==g)c--;
	  checkm[Q[q]]=0;
	  q++;
	  if(c==0)break;
	}
	//printf("%d\n",cm[g]);
	//}
	//else{
	Q[0]=s;
	q=0;
	r=1;
	c=0;
	int checkt[100]={0};
	for(j=0;j<m;j++){
	  if(mm[g][j]!=0)c++;
	  checkt[j]=1;
	}
	checkt[0]=0;
	cm[s][s]=0;
	ct[s][s]=0;
	while(q!=r){
	  for(j=0;j<m;j++){
	    if(mt[Q[q]][j]!=0){
	      if(checkt[j]==1){
		Q[r]=j;
		//check[j]=0;
		if(ct[s][j]>ct[s][Q[q]]+mt[Q[q]][j])ct[s][j]=ct[s][Q[q]]+mt[Q[q]][j];
		r++;
	      }
	    }
	  }
	  if(Q[q]==g)c--;
	  checkt[Q[q]]=0;
	  q++;
	  if(c==0)break;
	}
	//printf("%d\n",ct[g]);
	//}
      }
    }
    scanf("%d",&k);
    for(i=0;i<k;i++){
      scanf("%d %d %d",&s,&g,&h);
      if(h==0)printf("%d\n",cm[s-1][g-1]);
      else    printf("%d\n",ct[s-1][g-1]);
    }
  }

  return 0;
}