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 <iostream>
#include <algorithm>
#include <climits>

#define INF INT_MAX / 2
#define REP(i, m, n) for(int i = m; i < static_cast<int>(n); ++i)
int main()
{
    int line_num, station_num;
    
    while (std::cin >> line_num >> station_num) {
        
        if (line_num == 0 && station_num == 0) break;

        int map[2][101][101];
        
        REP(i, 0, 2) REP(j, 0, 101) REP(k, 0, 101) map[i][j][k] = INF;
        
        REP(i, 0, line_num) {
            int a, b, cost, time;
            std::cin >> a >> b >> cost >> time;
            map[0][a][b] = map[0][b][a] = cost;
            map[1][a][b] = map[1][b][a] = time;
        }
        
        REP(l, 0, 2) {
            REP(i, 1, station_num + 1) {
                REP(j, 1, station_num + 1) {
                    REP(k, 1, station_num + 1) {
                        map[l][i][j] = std::min(map[l][i][j], map[l][i][k] +
                                                map[l][k][j]);
                    }
                }
            }
        }

        int input_num;

        std::cin >> input_num;

        REP(i, 0, input_num) {
            int p, q, r;

            std::cin >> p >> q >> r;
            std::cout << map[r][p][q] << std::endl;
        }
    
    }
    return 0;
}

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

using namespace std;

struct Edge{
    int from,to,cost;
    Edge(int from,int to,int cost)
        : from(from),to(to),cost(cost) {};
};

const int INF = 1 << 30;
// s:始点,dist:距離,prev:最短経路木
bool bellman(const vector<vector<Edge> >& graph,int s,vector<int> &dist,vector<int> &prev){
    int n = graph.size();
    for(int i=0;i<n;i++) dist[i] = INF;
    dist[s] = 0;
    for(int i=0;i<n;i++) prev[i] = -1;

    bool neg_cycle = false;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            for(int k=0;k<graph[j].size();k++){
                Edge e = graph[j][k];
                if(dist[e.to] > dist[e.from] + e.cost){
                    dist[e.to] = dist[e.from] + e.cost;
                    prev[e.to] = e.from;
                    if(i == n-1){
                        dist[e.to] = -INF;
                        neg_cycle = true;
                    }
                }
            }
        }
    }
    return !neg_cycle;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(true){
        int n,m;
        cin >> n >> m;
        if(n == 0 and m == 0) break;
        vector<vector<Edge> > C(m);
        vector<vector<Edge> > T(m);

        for(int i=0;i<n;i++){
            int a,b,cost,time;
            cin >> a >> b >> cost >> time;
            a--;b--;
            C[a].push_back(Edge(a,b,cost));
            C[b].push_back(Edge(b,a,cost));
            T[a].push_back(Edge(a,b,time));
            T[b].push_back(Edge(b,a,time));
        }
        int k;
        cin >> k;
        vector<int> dist(m);
        vector<int> prev(m);
        for(int i=0;i<k;i++){
            int p,q,r;
            cin >> p >> q >> r;
            p--,q--;
            if(r == 0){
                bellman(C,p,dist,prev);
            }else{
                bellman(T,p,dist,prev);
            }
            cout << dist[q] << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <limits.h>
#include <queue>

#define INF INT_MAX - 1

using namespace std;


struct edge {int to,cost;};
typedef pair<int,int> P;

vector<edge> ek[6000];
vector<edge> em[6000];
priority_queue<P,vector<P>,greater<P> >que;

int d[256];
int q,r;

void dik()
{
  while(!que.empty())
    {
      P p = que.top();
      que.pop();
      // cout<<p.first<<"  "<<p.second<<endl;
      int v=p.second;
      int h=p.first;
      if(v==q && d[v]!=INF)
	break;
      for(int i=0;i<ek[v].size();i++)
	{
	  edge e;
	  if(r==0)
	    e=em[v][i];
	  else
	    e=ek[v][i];
	  if(d[e.to] > d[v] + e.cost)
	    {
	      d[e.to]=d[v]+e.cost;
	      que.push(P(d[e.to],e.to));
	    }
	  
	}
    }
  
}

int main()
{
  int n,m;
  for(;cin>>n>>m,n;)
    {
      //マップ初期化
      for(int i=0;i<6000;i++)
	{
	  ek[i].clear();
	  em[i].clear();
	}
      
      for(int i=0;i<n;i++)
	{
	//エッジ情報
	  int a,b,cost,time;
	  cin>>a>>b>>cost>>time;
	  edge xx;
	  xx.to=b;
	  xx.cost=cost;
	  em[a].push_back(xx);
	  xx.cost=time;
	  ek[a].push_back(xx);

	  xx.to=a;
	  xx.cost=cost;
	  em[b].push_back(xx);
	  xx.cost=time;
	  ek[b].push_back(xx);
	  
	}
      int c;
      cin>>c;
      for(;c--;)
	{
	  fill(d,d+102,INF);
	  //問い合わせ
	  int p;
	  cin>>p>>q>>r;
	  d[p]=0;
	  que.push(P(0,p));
	  dik();
	  cout<<d[q]<<endl;
	}
    }
}