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<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<utility>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pi acos(-1)
#define all(v) v.begin(),v.end()
using namespace std;

const double eps = 1e-8;
const double INF = 1e5;

typedef pair<int,int> P;
typedef double Real;
typedef complex<Real> Point;
typedef vector<Point> G;

double cross(const Point& a,const Point& b){//calculate ad-bc(vector product)
	return imag(conj(a)*b);
}

struct L:public vector<Point>{//line data
	L(const Point &a,const Point &b){
		push_back(a);
		push_back(b);
	}
};

int main(){
	int n,m;
	while(1){
		cin>>n>>m;
		if(!n && !m)break;
		int cost[m+1][m+1],time[m+1][m+1];
		rep(i,m)rep(j,n)cost[i][j]=time[i][j]=INF;
		int a,b,c,t;
		rep(i,n){
			cin>>a>>b;
			cin>>cost[a][b]>>time[a][b];
			cost[b][a]=cost[a][b];
			time[b][a]=time[a][b];
		}
		rep(k,m+1){
			rep(i,m+1){
				rep(j,m+1){
					cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
					time[i][j]=min(time[i][j],time[i][k]+time[k][j]);
				}
			}
		}
		int k,p,q,r;
		cin>>k;
		rep(i,k){
			cin>>p>>q>>r;
			if(r==0){
				cout<<cost[p][q]<<endl;
			}else{
				cout<<time[p][q]<<endl;
			}
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;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,n)r(j,n){
      if(i==j)a2[i][j]=a1[i][j]=0;
      else a2[i][j]=a1[i][j]=INF;
    }
    while(m--){
      cin>>q>>w>>e>>r;q--;w--;
      a1[q][w]=e;
      a2[q][w]=r;
    }
    r(k,n)r(i,n)r(j,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--){
      cin>>q>>w>>r;q--;w--;
      if(r)cout<<a2[q][w]<<endl;
      else cout<<a1[q][w]<<endl;
    }
  }
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int x[1000][1000][2], n, m, a, b, c, d, q, e;

int path(int n, int s, int g) {
	int p[1000];
	memset(p, 127, sizeof(p));
	p[s] = 0;
	e = min(n, 20);
	for (int i = 0; i < e; i++){
		for (int j = 1; j <= n; j++) {
			for (int k = 1; k <= n; k++) {
				if (x[j][k][1] < 10000000) {
					p[k] = min(p[k], p[j] + x[j][k][1]);
				}
			}
		}
	}
	return p[g];
}

int score(int n, int s, int g) {
	int p[1000];
	memset(p, 127, sizeof(p));
	p[s] = 0;
	e = min(n, 20);
	for (int i = 0; i < e; i++) {
		for (int j = 1; j <= n; j++) {
			for (int k = 1; k <= n; k++) {
				if (x[j][k][0] < 10000000) {
					p[k] = min(p[k], p[j] + x[j][k][0]);
				}
			}
		}
	}
	return p[g];
}

int main() {
	while (true) {
		memset(x, 0, sizeof(x));
		cin >> m >> n;
		if (m == 0 && n == 0) { break; }
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> c >> d;
			x[a][b][0] = c;
			x[b][a][0] = c;
			x[a][b][1] = d;
			x[b][a][1] = d;
		}
		cin >> q;
		for (int i = 0; i < q; i++) {
			cin >> a >> b >> c;
			if (c == 0) { cout << score(n, a, b) << endl; }
			else { cout << path(n, a, b) << endl; }
		}
	}
}