Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"fmt"
)

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
				} else {
					c_dist[i][j] = c_matrix[i][j]
				}
				if c_matrix[i][j] == 0{
					c_prev[i][j] = -1
				} else {
					c_prev[i][j] = i
				}
				if t_matrix[i][j] == 0{
					t_dist[i][j] = INF
				} else {
					t_dist[i][j] = t_matrix[i][j]
				}
				if t_matrix[i][j] == 0{
					t_prev[i][j] = -1
				} else {
					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]
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[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>

using namespace std;

int main() {
	int l, n, m, s[100][100][2];
	while (cin >> n >> m, n || m) {
		for (int y = 0; y < 100; ++y)
			for (int x = 0; x < 100; ++x) {
				if (x == y) s[y][x][0] = 0, s[y][x][1] = 0;
				else s[y][x][0] = 100000, s[y][x][1] = 100000;
			}

		for (int i = 0; i < n; ++i) {
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			--a, --b;
			s[a][b][0] = cost, s[b][a][0] = cost;
			s[a][b][1] = time, s[b][a][1] = time;
		}

		for (int t = 0; t < 2; ++ t)
			for (int i = 0; i < m; ++i)
				for (int j = 0; j < m; j++)
					for (int k = 0; k < m; k++)
						if (s[j][k][t] > s[j][i][t]+s[i][k][t])
							s[j][k][t] = s[j][i][t]+s[i][k][t], s[k][j][t] = s[j][k][t];

		cin >> l;
		for (int i = 0; i < l; ++ i) {
			int p, q, r;
			cin >> p >> q >> r;
			--p, --q;
			cout << s[p][q][r] << endl;
		}
	}
}

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] = cost[0][b][a] = c;
			cost[1][a][b] = 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();
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<math.h>
using namespace std;
  
int min(int x, int y){
	if(x<y){
		return x;
	}else{
		return y;
	}
}

int co(int x,int y,int m,int c[][101]){
	int i,j,k,d[101],s[101];
	for(i=0;i<=100;i++){
		d[i]=2000;
		s[i]=0;
	}
	for(i=1;i<=m;i++){
		s[i]=1;
	}
	d[x]=0;
	for(i=1;i<=m;i++){
		k=0;
		for(j=1;j<=m;j++){
			if(s[j]==1){
				if(k==0){
					k=j;
				}else if(d[j]<d[k]){
					k=j;
				}
			}
		}
		if(k!=0){
			for(j=1;j<=m;j++){
				if(c[k][j]!=0){
					d[j]=min(d[j],d[k]+c[k][j]);
				}
			}
		}
		if(k==0){break;}
		s[k]=0;
	}
	return d[y];
}
int ti(int x,int y,int n,int t[][101]){
	int i,j,k,d[101],s[101];
	for(i=0;i<=100;i++){
		d[i]=2000;
		s[i]=0;
	}
	for(i=1;i<=n;i++){
		s[i]=1;
	}
	d[x]=0;
	for(i=1;i<=n;i++){
		k=0;
		for(j=1;j<=n;j++){
			if(s[j]==1){
				if(k==0){
					k=j;
				}else if(d[j]<d[k]){
					k=j;
				}
			}
		}
		for(j=1;j<=n;j++){
			if(t[k][j]!=0){
				d[j]=min(d[j],d[k]+t[k][j]);
			}
		}
		s[k]=0;
	}
	return d[y];
}

int main(){
	while(1){
		int i,j,a,b,k,n,m,p,q,r,c[101][101],t[101][101];
		for(i=0;i<=100;i++){
			for(j=0;j<=100;j++){
				c[i][j]=0;
				t[i][j]=0;
			}
		}
		cin>>n>>m;
		if(n==0&&m==0){break;}
		for(i=0;i<n;i++){
			cin>>a>>b;
			cin>>c[a][b]>>t[a][b];
			c[b][a]=c[a][b];
			t[b][a]=t[a][b];
		}
		cin>>k;
		for(i=0;i<k;i++){
			cin>>p>>q>>r;
			if(r==0){
				cout<<co(p,q,m,c)<<endl;
			}else if(r==1){
				cout<<ti(p,q,m,t)<<endl;
			}
		}
	}
    return 0;
}

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

inline int max(int a, int b) { if (a>b) return a; else return b; }
inline int min(int a, int b) { if (a<b) return a; else return b; }

int min_route(int matrix[101][101], int n, int a, int b)
{
  int cost[101];
  bool done[101];
  int min_s = a;
  const int MAX = 999999999;

  for (int i=1; i<=n; i++)
  {
    cost[i] = MAX;
  }
  cost[a] = 0;
  while (!done[b])
  {
    done[min_s] = true;
    for (int i=1; i<=n; i++)
    {
      if ((matrix[min_s][i] > 0) && (cost[i] > cost[min_s]+matrix[min_s][i]))
      {
        cost[i] = cost[min_s]+matrix[min_s][i];
      }
    }
    int minin = MAX;
    for (int i=1; i<=n; i++)
    {
      if ((!done[i]) && (minin > cost[i]))
      {
        min_s = i;
        minin = cost[i];
      }
    }
  }
  return cost[b];
}


int main()
{
  int time[101][101];
  int cost[101][101];
  int n, m;
  int a, b, c, t;

  cin >> n >> m;

  for (int i=1; i<=m; i++)
    for (int j=1; j<=m; j++) { time[i][j] = 0; cost[i][j] = 0; }

  for (int i=0; i<n; i++)
  {
    cin >> a >> b >> c >> t;
    time[a][b] = t;
    time[b][a] = t;
    cost[a][b] = c;
    cost[b][a] = c;
  }

  int k;
  int p, q, r;
  cin >> k;
  for (int i=1; i<=k; i++)
  {
    cin >> p >> q >> r;
    if (r==0)
    {
      cout << min_route(cost, m, p, q) << endl;
    }
    else
    {
      cout << min_route(time, m, p, q) << endl;
    }
  }
  return 0;
}