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

using namespace std;

typedef vector< int > VI;
typedef vector< VI > VVI;

int main(){
	while(true){
		int n, m;

		cin >> n >> m;

		if(n == 0 && m == 0){
			break;
		}

		VVI map_cost(m + 1, VI(m + 1, INT_MAX / 4));
		VVI map_time(m + 1, VI(m + 1, INT_MAX / 4));

		for(int i = 0; i < m; i++){
			map_cost[i][i] = 0;
			map_time[i][i] = 0;
		}

		for(int i = 0; i < n; i++){
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;

			a--;
			b--;

			map_cost[a][b] = cost;
			map_time[a][b] = time;
		}

		for(int k = 0; k < m; k++){
			for(int i = 0; i < m; i++){
				for(int j = 0; j < m; j++){
					map_cost[i][j] = min(map_cost[i][j], map_cost[i][k] + map_cost[k][j]);
					map_time[i][j] = min(map_time[i][j], map_time[i][k] + map_time[k][j]);
				}
			}
		}

		int k;
		cin >> k;

		VI p(k, INT_MAX / 4);
		VI q(k, INT_MAX / 4);
		VI r(k, INT_MAX / 4);

		for(int i = 0; i < k; i++){

			cin >> p[i] >> q[i] >> r[i];

			p[i]--;
			q[i]--;
		}

		for(int i = 0; i < k; i++){
			if(r[i] == 0){
				cout << map_cost[p[i]][q[i]] << endl;
			}
			else if(r[i] == 1){
				cout << map_time[p[i]][q[i]] << endl;
			}
		}
	}
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999
#define MAX_V 100

struct edge{int to,cost;};
vector<edge> G[2][MAX_V];
int d[2][MAX_V];

int dijkstra(int s,int V,int ver){
    priority_queue< pii,vector<pii>,greater<pii> > que;
    rep(i,MAX_V)d[ver][i]=INF;
    d[ver][s]=0;
    que.push(pii(0,s));
    
    while(que.size()){
        pii p=que.top();
        que.pop();
        
        int v=p.second;
        if(d[ver][v]<p.first)continue;
        
        rep(i,G[ver][v].size()){
            edge e=G[ver][v][i];
            if(d[ver][e.to]>d[ver][v]+e.cost){
                d[ver][e.to]=d[ver][v]+e.cost;
                que.push(pii(d[ver][e.to],e.to));
            }
        }
    }
    return d[ver][V];
}




int main(){
    int n,m;
    cin>>n>>m;
    rep(i,n){
        int a,b,cost,time;
        cin>>a>>b>>cost>>time;
        a--,b--;
        G[0][a].pb(edge{b,cost});
        G[0][b].pb(edge{a,cost});
        G[1][a].pb(edge{b,time});
        G[1][b].pb(edge{a,time});
    }
    int k;
    cin>>k;
    rep(i,k){
        int p,q,r;
        cin>>p>>q>>r;
        p--,q--;
        cout<<dijkstra(p,q,r)<<endl;
    }
}

C++: #include<iostream>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=1;i<=n;i++)
using namespace std;
typedef pair<int, int> P;

int c[100][100]={};
int t[100][100]={};

int main()
{
  int n,m,a,b;
  int i,j,k;
  int p,q,r;

  while(1){
    cin>>n>>m;
    if(n==0 && m==0)break;

    rep(i,m)rep(j,m)c[i][j]=t[i][j]=INF;//INFテ」ツ?ァテ・ツ按敕ヲツ慊淌・ツ個?
    rep(i,m)c[i][i]=t[i][i]=0;//テ・ツ青古ゥツァツ?」ツ?ッ0

    rep(i,n){
      cin>>a>>b;
      cin>>c[a][b]>>t[a][b];
      c[b][a]=c[a][b];
      t[b][a]=t[a][b];
    }
    
    rep(k,m)rep(i,m)rep(j,m){
      c[i][j]=min(c[i][j],c[i][k]+c[k][j]);
      t[i][j]=min(t[i][j],t[i][k]+t[k][j]);
    }

    cin>>k;
    rep(i,k){
      cin>>p>>q>>r;
      if(r)
	cout<<t[p][q]<<endl;
      else
	cout<<c[p][q]<<endl;
    }
  }
}

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

const int INF = 1000000000;

int main()
{
  int count=0;
	while(count==50){
		int n, m;
		cin >> n >> m;

		if(n==0 && m==0)
			break;

		 int C[100][100];
		 int T[100][100];
	   for(int i=0; i<m; i++){
		   for(int j=0; j<m; j++){
			   if(i==j){
	         C[i][j] = T[i][j] = 0;
		     }else{
			     C[i][j] = T[i][j] = INF;
		     }
		   }
     }

	   for(int i=0; i<n; i++){
		   int a, b, c, t;
		   cin >> a >> b >> c >> t;

	     C[a-1][b-1] = C[b-1][a-1] = c;
		   T[a-1][b-1] = T[b-1][a-1] = t;
    }

    int k;
    cin>>k;
    for(int i=0;i<k;i++){
      int p, q, r;
      int a=0;
      cin>>p>>q>>r;
      int d[m][m];
      if(r==0){
        for(int i=0;i<m;i++){
          for(int j=0;j<m;j++){
            d[i][j]=C[i][j];
          }
        }
        for(int k=0;k<m;k++){
          for(int i=0;i<m;i++){
            for(int j=0;j<m;j++){
              if(d[i][j]>d[i][k]+d[k][j]){
                d[i][j]=d[i][k]+d[k][j];
              }
            }
          }
        }
        a=d[p-1][q-1];
        cout<<a<<endl;
      }
      if(r==1){
        for(int i=0;i<m;i++){
          for(int j=0;j<m;j++){
            d[i][j]=T[i][j];
          }
        }
        for(int k=0;k<m;k++){
          for(int i=0;i<m;i++){
            for(int j=0;j<m;j++){
              if(d[i][j]>d[i][k]+d[k][j]){
                d[i][j]=d[i][k]+d[k][j];
              }
            }
          }
        }
        a=d[p-1][q-1];
        cout<<a<<endl;
      }
    }
    count++;
	}

	return 0;
}