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<cstdio>

using namespace std;

int i, j, k, p, q, r, n, m, a, b, c, cost[3001], t, dc[101][101], dt[101][101];

int dic(const int d[][101]) {
  cost[p] = 0;
  int min, f[3001] = {};
  for(i = 0; i < m; i++) {
    f[p] = 1;
    for(j = 1; j <= m; j++) {
      if(f[j]) continue;
      if(cost[j] > cost[p] + d[p][j]) cost[j]  = cost[p] + d[p][j];
    }
    min = 200000000;
    for(j = 1; j <= m; j++ ) {
      if(f[j]) continue;
      if(min > cost[j]) min = cost[p = j];
    }
  }
  return cost[q];
}


int main() {
  for(i = 0; i < 100; i++)
    for(j = 0; j < 100; j++) {
      dc[i][j] = 100000000;
      dt[i][j] = 100000000;
    }
  
  cin >> n >> m;
  
  while(n--) {
    cin >> a >> b >> c >> t;
    dc[a][b] = dc[b][a] = c;
    dt[a][b] = dt[b][a] = t;
  }
  
  cin >> k;
  
  while(cin >> p  >> q, p != 0 || q != 0 ) {
    cin >> r;
    for(i = 0; i <= 3000; i++) cost[i] = 100000000;
    if(r) cout << dic(dt) << endl;
    else cout << dic(dc) << endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <cmath>
#include <cstring>
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define pb push_back
using namespace std;

const int INF=10000000;
int V;
int d[2][111][111];

void wf(){
    FOR(k,0,V+1)FOR(i,0,V+1)FOR(j,0,V+1){
        d[0][i][j]=min(d[0][i][j],d[0][i][k]+d[0][k][j]);
        d[1][i][j]=min(d[1][i][j],d[1][i][k]+d[1][k][j]);
    }
}

int main(){
    int n;
    while(1){
        cin>>n>>V;
        if(!(n||V)) break;
        FOR(i,0,V+1)FOR(j,0,V+1){
            d[0][i][j]=d[1][j][i]=INF;
        }
        FOR(i,0,n){
            int a,b,m,t;cin>>a>>b>>m>>t;
            d[0][a][b]=d[0][b][a]=t;
            d[1][a][b]=d[1][b][a]=m;
        }
        wf();
        int k;cin>>k;
        FOR(i,0,k){
            int p,q,r;cin>>p>>q>>r;
            cout<<d[r][p][q]<<endl;
        }
    }
}

C++: #include <iostream>
#include <algorithm>
#include <array>
#include <stack>

using namespace std;

int const Inf = 10000000;
int const N = 100;

typedef array<int, 2> line;

int main()
{
    int n, m;
    while( cin >> n >> m, n||m ) {
        line table[N][N] = {};

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

            int const from = a-1, to = b-1;
            line const in = {{cost, time}};
            table[from][to] = ( table[to][from] = in );
        }

        int k;
        cin >> k;
        for( int i=0; i<k; ++i ) {
            int p, q, r;
            cin >> p >> q >> r;
            int const from = p-1, to = q-1;

            bool v[N] = {};
            int d[N];
            fill( d, d+N, Inf );


            stack<int> w;
            w.push( from );
            d[from] = 0;

            while( !w.empty() ) {
                int const cur = w.top();
                w.pop();

                //if ( v[cur] )
                //  continue;
                //v[cur] = true;

                for( int ii=0; ii<m; ++ii ) {
                    if ( table[cur][ii][0] == 0 )
                        continue;

                    if ( d[cur] + table[cur][ii][r] < d[ii] ) {
                        d[ii] = d[cur] + table[cur][ii][r];
                        w.push( ii );
                    }

                    //cout << cur+1 << " -> " << ii+1 << " c " << d[ii] << endl;
                }
            }

            cout << d[to] << endl;
        }
    }
}

C++: #include<iostream>
#include<cstdio>

using namespace std;

int i, j, k, p, q, r, n, m, a, b, c, cost[101], t, dc[101][101], dt[101][101];

int dic(const int d[][101]) {
  cost[p] = 0;
  int min, f[101] = {};
  for(i = 0; i < m; i++) {
    f[p] = 1;
    for(j = 1; j <= m; j++) {
      if(f[j]) continue;
      if(cost[j] > cost[p] + d[p][j]) cost[j]  = cost[p] + d[p][j];
    }
    min = 200000000;
    for(j = 1; j <= m; j++ ) {
      if(f[j]) continue;
      if(min > cost[j]) min = cost[p = j];
    }
  }
  return cost[q];
}


int main() {
  for(i = 0; i < 100; i++)
    for(j = 0; j < 100; j++) {
      dc[i][j] = 100000000;
      dt[i][j] = 100000000;
    }
  
  while(cin >> n >> m, n != 0 || m != 0) {
  
    while(n--) {
      cin >> a >> b >> c >> t;
      dc[a][b] = dc[b][a] = c;
      dt[a][b] = dt[b][a] = t;
    }
  
    cin >> k;
    
    while(k--) {
      cin >> p >> q >> r;
      for(i = 0; i <= 100; i++) cost[i] = 100000000;
      if(r) cout << dic(dt) << endl;
      else cout << dic(dc) << endl;
    }
  
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;

int n, m;
int k;
int cost_table[100][100];
int time_table[100][100];

int main()
{
	memset(cost_table, 0x0f, sizeof cost_table);
	memset(time_table, 0x0f, sizeof time_table);

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

		cost_table[a - 1][b - 1] = cost;
		cost_table[b - 1][a - 1] = cost;

		time_table[a - 1][b - 1] = time;
		time_table[b - 1][a - 1] = time;
	}

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

	cin >> k;
	for (int i = 0; i < k; ++i)
	{
		int p, q, r;
		cin >> q >> p >> r;
		if (r == 0)
		{
			cout << cost_table[q - 1][p - 1] << endl;
		}
		else
		{
			cout << time_table[q - 1][p - 1] << endl;
		}
	}
}