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<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#include<iostream>
#include<queue>
#define rep(i,a,b) for(i=a;i<b;i++)
#define F1 first.first
#define F2 first.second
#define F3 second
#define INF 2100000000
#define long long int int
using namespace std;
typedef pair<pair<int,int>,int> P1;
typedef pair<int,int> P2;
int main(void)
{
	int i,j,k,n,m,x,s,e;
	int a,b,cos,tim,st,en,way;
	int leng[1001],kami[1002];
	P1 glid1[6000],glid2[6000];
	P2 tori;
	while(cin>>n>>m,n&&m) {
		rep(i,0,n) {
			cin>>a>>b>>cos>>tim;
			glid1[i*2].F1=a,glid1[i*2].F2=b,glid1[i*2].F3=cos;
			glid2[i*2].F1=a,glid2[i*2].F2=b,glid2[i*2].F3=tim;
			glid1[i*2+1].F1=b,glid1[i*2+1].F2=a,glid1[i*2+1].F3=cos;
			glid2[i*2+1].F1=b,glid2[i*2+1].F2=a,glid2[i*2+1].F3=tim;
		}
		sort(glid1,glid1+m*2);sort(glid2,glid2+m*2);
		kami[1]=0;
		rep(i,1,m*2) {
			if(glid1[i].F1!=glid1[i-1].F1) {
				x=glid1[i].F1;
				kami[x]=i;
			}
		}
		kami[n+1]=m*2;
		priority_queue<P2,vector<P2>,greater<P2> > que;
		cin>>k;
		rep(i,0,k) {
			cin>>st>>en>>way;
			for(j=1;j<=n;j++) leng[j]=INF;
			while(!que.empty()) que.pop();
			que.push(P2(0,st));
			while(!que.empty()) {
				tori=que.top(),que.pop();
				a=tori.first,b=tori.second;
				if(a<leng[b]) {
					leng[b]=a;
					if(b==en) break;
					s=kami[b],e=kami[b+1];
					if(way==0)rep(j,s,e) que.push(P2(a+glid1[j].F3,glid1[j].F2));
					else rep(j,s,e) que.push(P2(a+glid2[j].F3,glid2[j].F2));
				}
			}
			cout<<leng[en]<<endl;
		}
	}
	return 0;
}

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

int main(void)
{
	int n, m, a, b, c, t;
	int p, q, r;
	int cost[100][100], time[100][100];
	
	while (scanf("%d %d", &m, &n), n){
		for (int i = 0; i < 100; i++){
			for (int j = 0; j < 100; j++){
				if (i == j) cost[i][j] = time[i][j] = 0;
				else cost[i][j] = time[i][j] = 10000000;
			}
		}
		
		for (int i = 0; i < m; i++){
			cin >> a >> b >> c >> t;
			cost[a - 1][b - 1] = cost[b - 1][a - 1] = c;
			time[a - 1][b - 1] = time[b - 1][a - 1] = t;
		}
		
		for (int k = 0; k < n; k++){
			for (int i = 0; i < n; i++){
				for (int j = 0; j < n; j++){
					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;
		cin >> k;
		for (int i = 0; i < k; i++){
			cin >> p >> q >> r;
			if (r == 0) cout << cost[p - 1][q - 1] << endl;
			else cout << time[p - 1][q - 1] << endl;
		}
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = 1e9;

void warshall(vector<vector<int> > &adj){
	int n = adj[0].size();
	for(int k=0; k<n; k++){
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				adj[i][j] = min(adj[i][j], adj[i][k]+adj[k][j]);
			}
		}
	}
}

int main(){
	while(1){
		int n,m;
		cin >> n >> m;
		if(n==0) break;
		
		vector<vector<int> > adjc(m+1, vector<int>(m+1, inf));
		vector<vector<int> > adjt(m+1, vector<int>(m+1, inf));
		for(int i=0; i<n; i++){
			int a,b,c,t;
			cin >> a >> b >> c >> t;
			adjc[a][b] = adjc[b][a] = c;
			adjt[a][b] = adjt[b][a] = t;
		}
		warshall(adjc);
		warshall(adjt);
		
		int k;
		cin >> k;
		for(int i=0; i<k; i++){
			int p,q,r;
			cin >> p >> q >> r;
			if(r==0){
				cout << adjc[p][q] << endl;
			}else{
				cout << adjt[p][q] << endl;
			}
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)

template<typename T>
void remove(vector<T>& vector, unsigned int index){
vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int cost[2][120][120];
int n, m;


int main(void){
	while(cin >> n >> m, n|m){
		fill_n(**cost, 2*120*120, 10000000);
		for(int i = 0; i < n; i++){
			int a, b,c,t;
			cin >> a >> b >> c >> t;
			cost[0][a][b] = c;
			cost[0][b][a] = c;
			cost[1][a][b] = t;
			cost[1][b][a] = t;
		}
		for(int k=0;k<n;++k)for(int i=0;i<n;++i)for(int j=0;j<n;++j){
			cost[0][i][j] = min(cost[0][i][j], cost[0][i][k] + cost[0][k][j]);
			cost[1][i][j] = min(cost[1][i][j], cost[1][i][k] + cost[1][k][j]);
		}
		int k;
		cin >> k;
		rep(i, k){
			int a, b, c;
			cin >> a >> b >> c;
			cout << cost[c][a][b] << endl;
		}
	}

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}