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<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<cstring>
using namespace std;
int mini(int a,int b){
	if(a<b){
		return a;
	}else{
		return b;
	}
}

int cost(int p,int q,int n,int co[][101]){
	int i,s[101],a,j,d[101],b;
	for(i=0;i<=n;i++){s[i]=0;d[i]=2000;}
	a=p;s[a]=1;d[a]=0;
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++){
			d[j]= mini(d[j],d[a]+co[a][j]);
		}
		s[a]=1;b=2000;
		for(j=1;j<=n;j++){
			if(s[j]==0&&d[j]<=b){
				a=j;b=d[j];
			}
		}
		s[a]=1;
	}
	return d[q];
}

int main(){
	while(1){
		int i,a=0,b=0,p,q,r,j,k,n,m,co[101][101],ti[101][101],s[101],ans[200];
		for(i=0;i<101;i++){
			s[i]=0;
			for(j=0;j<101;j++){
				if(i==j){
					co[i][j]=0;
					ti[i][j]=0;
				}else{
					co[i][j]=2000;
					ti[i][j]=2000;
				}
			}
		}
		cin>>n>>m;
		if(n==0&&m==0)break;
		for(i=0;i<n;i++){
			cin>>a>>b;
			cin>>co[a][b]>>ti[a][b];
			co[b][a]=co[a][b];
			ti[b][a]=ti[a][b];
		}
		cin>>k;
		for(i=0;i<k;i++){
			cin>>p>>q>>r;
			if(r==0){
				ans[i]=cost(p,q,m,co);
			}else{
				ans[i]=cost(p,q,m,ti);
			}
		}
		for(i=0;i<k;i++){
			cout<<ans[i]<<endl;
		}
	}
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <string>
#include <queue>

const int INF = 1<<28;
using namespace std;
string IntToString(int);
int StringToInt(string);
struct edje{
public:
  int from;
  int to;
  int cost;
  edje(int f,int t,int c){
    from = f;
    to = t;
    cost = c;
  }
};

void bellmanFord(vector<int> &map,vector<edje> &cost){
  while(1){
    bool update = false;
    for(int j = 0;j < cost.size();j++){
      if(map[cost[j].to] > map[cost[j].from] + cost[j].cost){
	map[cost[j].to] = min(map[cost[j].to],map[cost[j].from] + cost[j].cost);
	update = true;
      }
    }
    if(!update)break;
  }
}
int main(){
  while(1){
    int n,m;
    cin >> n >> m;
    if(n == 0 && m == 0)break;
    vector<edje> cost;
    vector<edje> time;
    
    for(int i = 0;i < n;i++){
      int a,b,c,t;
      cin >> a >> b >> c >> t;
      edje cs(a-1,b-1,c);
      cost.push_back(cs);
      cs= edje(b-1,a-1,c);
      cost.push_back(cs);
      edje tm(a-1,b-1,t);
      time.push_back(tm);
      tm = edje(b-1,a-1,t);
      time.push_back(tm);
    }
    int k;
    cin >> k;
    for(int i = 0;i < k;i++){
      int s,g,q;
      cin >> s >> g >> q;
      vector<edje> tmp;
      if(q == 0) tmp = vector<edje>(cost);
      else if(q == 1) tmp = vector<edje>(time);
      vector<int> map(m,INF);
      map[s-1] = 0;
      bellmanFord(map,tmp);
      cout << map[g-1] << endl;
      

    }
  }
  return 0;   
}
string IntToString(int number){
  stringstream ss;
  ss << number;
  return ss.str();
}
int StringToInt(string number){
  stringstream ss;
  ss << number;
  int numInt;
  ss >> numInt;
  return numInt;
  
}

C++: #include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
const int INF = INT_MAX/3;
const int MAXN = 200;
const int TIME = 1,COST = 0;
int graph[2][MAXN][MAXN];
int n,m,a,b,c,t;
void solve();
void init(){
  for(int i = 0;i < m;i++)
    for(int j = 0;j < m;j++)
      graph[0][i][j] =graph[0][j][i] = graph[1][j][i] = graph[1][i][j] = INF;
}
int main(){
  cin >> n >> m;
  init();
  for(int i = 0;i < n;i++){
    cin >> a >> b >> c >> t;
    graph[TIME][a-1][b-1] = graph[TIME][b-1][a-1] = t;
    graph[COST][a-1][b-1] = graph[COST][b-1][a-1] = c;
  }
  solve();
  int X;
  cin >> X;
  for(int i = 0;i < X;i++){
    cin >> a >> b >> c;
    cout << graph[c][a-1][b-1] << endl;
  }
  return 0;
}

void solve(){
  for(int k = 0;k < m;k++){
    for(int i = 0;i < m;i++){
      for(int j = 0;j < m;j++){
	graph[0][i][j] = min(graph[0][i][j],graph[0][i][k] + graph[0][k][j]);
	graph[1][i][j] = min(graph[1][i][j],graph[1][i][k] + graph[1][k][j]);
      }
    }
  }
}

C++: #include <bits/stdc++.h>
#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define RREP(i,a,b) for(int i=(a);i>=(b);i--)
#define pq priority_queue
#define P pair<int,int>
#define P2 pair<int,P>
#define P3 pair<int,P2>
typedef long long ll; typedef long double ld;
using namespace std;
const int INF=1e9, MOD=1e9+7, around[]={0,1,1,-1,-1,0,-1,1,0,0};
const ll LINF=1e18;
const ld PI=abs(acos(-1));
int n,m,k,a,b,c,d,li1[110][110],li2[110][110];

int main(){
	while(cin >> n >> m,n||m){
		REP(i,0,m) REP(j,0,m) if(i!=j) li1[i][j]=INF,li2[i][j]=INF;
		REP(i,0,n) cin >> a >> b >> c >> d,li1[a-1][b-1]=c,li1[b-1][a-1]=c,li2[a-1][b-1]=d,li2[b-1][a-1]=d;
		REP(k,0,m) REP(j,0,m) REP(i,0,m) li1[i][j]=min(li1[i][j], li1[i][k]+li1[k][j]),li2[i][j]=min(li2[i][j], li2[i][k]+li2[k][j]);
		cin >> k;
		REP(i,0,k){
			cin >> a >> b >> c;
			if(c) cout << li2[a-1][b-1] << endl;
			else cout << li1[a-1][b-1] << endl;
		}
	}
	return 0;
}