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 <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <iterator>
#define INF 100000100
using namespace std;


int minimize(int start, int goal, map<int,int>* mp,int n);


int main(void){
  int n,m;
  while(true){
    cin>>n>>m;
    if(n==0 and m==0){break;}
    map<int,int> costs[n];
    map<int,int> times[n];
    int a,b,c,t;
    for(int i=0;i<n;i++){
      cin>>a>>b>>c>>t;
      a--;
      b--;
      costs[a][b]=c;
      costs[b][a]=c;
      times[a][b]=t;
      times[b][a]=t;
    }
    int k;
    cin>>k;
    int p,q,r;
    for(int i=0;i<k;i++){
      cin>>p>>q>>r;
      p--;
      q--;
      if(r==0){
        cout<<minimize(p,q,costs,n)<<endl;
      }
      else{
        cout<<minimize(p,q,times,n)<<endl;
      }
    }
  }
  return 0;
}

int minimize(int start, int goal, map<int,int>* mp,int n){
  int d[n];
  for(int i=0;i<n;i++){
    d[i]=INF;
  }
  d[start]=0;
  vector<int> next,_next;
  next.push_back(start);
  while(next.size()>0){
    _next.clear();
    for(auto iter=next.begin();iter!=next.end();iter++){
      int s=*iter;
      for(auto iter2=mp[s].begin();iter2!=mp[s].end();iter2++){
        int key=iter2->first;
        int value=iter2->second;
        if(d[key]>d[s]+value){
          d[key]=d[s]+value;
          _next.push_back(key);
        }
      }
    }
    next.clear();
    copy(_next.begin(),_next.end(),back_inserter(next));
  }
  return d[goal];
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
#define INF 1<<29
using namespace std;
main(){
  int m,n;
  while(cin>>m>>n,n){
    int a1[n][n],a2[n][n],q,w,e,r;
    r(i,0,n)r(j,0,n){
      if(i==j)a2[i][j]=a1[i][j]=0;
      else a2[i][j]=a1[i][j]=INF;
    }
    while(m--){
      scanf("%d%d%d%d",&q,&w,&e,&r);q--;w--;
      a1[q][w]=a1[w][q]=e;
      a2[q][w]=a2[w][q]=r;
    }
    r(k,0,n)r(i,0,n)r(j,i+1,n){
      a1[i][j]=min(a1[i][j],a1[i][k]+a1[k][j]);
      a2[i][j]=min(a2[i][j],a2[i][k]+a2[k][j]);
    }
    cin>>e;
    while(e--){
      scanf("%d%d%d",&q,&w,&r);q--;w--;
      if(r)cout<<a2[q][w]<<endl;
      else cout<<a1[q][w]<<endl;
    }
  }
}

C++: #include<iostream>
#include<algorithm>
#define MAX 1000000
using namespace std;
int main(){
	int n, m, a, b, cost, ti, k, p, q, r, point = 0;
	int cost_[101][101], time_[101][101], k_[101];
	for (int i = 0; i < 101; i++){
		for (int l = 0; l < 101; l++){
			cost_[i][l] = MAX;
			ti_[i][l] = MAX;
		}
	}
	cin >> n >> m;
	while (n != 0 && m != 0){
		for (int i = 0; i < n; i++){
			cin >> a >> b >> cost >> ti;
			cost_[a][b] = cost;
			ti_[a][b] = time;
		}
		cin >> k;
		for (int i = 0; i < k; i++){
			cin >> p >> q >> r;
			k_[point] = p;
			k_[point + 1] = q;
			k_[point + 2] = r;
			point += 3;
		}point = 0;
		for (int i = 1; i <= m; i++){
			for (int j = 1; j <= m; j++){
				for (int o = 1; o <= m; o++){
					if (cost_[j][i] != MAX && cost_[i][o] != MAX &&
						ti_[j][i] != MAX && ti_[i][o] != MAX){
						cost_[j][o] = min(cost_[j][o], cost_[j][i] + cost_[i][o]);
						ti_[j][o] = min(ti_[j][o], ti_[j][i] + ti_[i][o]);
					}
				}
			}
		}
		for (int i = 0; i < k; i++){
			if (k_[point + 2] == 0){
				cout << cost_[k_[point]][k_[point + 1]] << endl;
			}
			else cout << time_[k_[point]][k_[point + 1]] << endl;
			point += 3;
		}
		cin >> n >> m;
	}
	return 0;
}

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

const int inf = 1<<20;
int Cost[100][100];
int Time[100][100];

int main()
{
	int n,m,k,a,b,c,t;
	while(cin >> n >> m, (n||m))
	{
		for(int i=0; i<m; i++)
		for(int j=0; j<m; j++)
		{
			Cost[i][j] = Time[i][j] = (i==j?0:inf);
		}

		while(n--)
		{
			cin >> a >> b >> c >> t;
			a--; b--;
			Cost[a][b] = Cost[b][a] = c;
			Time[a][b] = Time[b][a] = t;
		}
	
		for(int l=0; l<m; l++)
		for(int i=0; i<m; i++)
		for(int j=0; j<m; j++)
		{
			if(Time[i][j]>Time[i][l]+Time[l][j]) Time[i][j] = Time[i][l]+Time[l][j];
			if(Cost[i][j]>Cost[i][l]+Cost[l][j]) Cost[i][j] = Cost[i][l]+Cost[l][j];
		}

		cin >> k;
		while(k--)
		{
			cin >> a >> b >> t;
			a--; b--;
			if(t==0){ cout << Cost[a][b] << endl; }
			if(t==1){ cout << Time[a][b] << endl; }
		}
	}
}