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
			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<algorithm>
using namespace std;
int main(){
  int n,m;
  while(cin >>n>>m,n||m){
    int ans[100][100][2];
    for(int i=0; i<100; i++)
      for(int j=0; j<100; j++)
	for(int k=0; k<2; k++)
	  ans[i][j][k] = 1e+9;
    for(int i=0,a,b,cost,time; i<n; i++){
      cin >>a>>b>>cost>>time;
      a--;b--;
      ans[a][b][0] = ans[b][a][0] = cost;
      ans[a][b][1] = ans[b][a][1] = time;
    }
    for(int k=0; k<2; k++){
      for(int l=0; l<m; l++){
	for(int i=0; i<m; i++){
	  for(int j=0; j<m; j++){
	    ans[i][j][k] = min(ans[i][j][k],ans[i][l][k]+ans[l][j][k]);
	  }
	}
      }
    }
    int t=0;
    cin >>t;
    while(t--){
      int p,q,r;
      cin >>p>>q>>r;
      cout <<ans[p-1][q-1][r]<<endl;
    }
  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<set>

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)

int main(){
	while(1){
		int m,n;
		cin>>m>>n;
		if(m==0)break;
		
		int dp[2][111][111];
		
		rep(p,2){
			rep(i,111){
				rep(j,111){
					dpc[p][i][j]=dpt[p][i][j]=1000000000;
				}
				dpc[p][i][i]=dpt[p][i][i]=0;
			}
		}
		
		rep(i,m){
			int a,b,c,t;
			cin>>a>>b>>c>>t;
			
			dp[0][a][b]=c;
			dp[1][a][b]=t;
		}
		
		rep(p,2){
			rep(k,n){
				rep(i,n){
					rep(j,n){
						dp[p][i][j] = min(dp[p][i][j],dp[p][i][k]+dp[p][k][j]);
					}
				}
			}
		}
		
		int T;
		cin>>T;
		rep(i,T){
			int a,b,c;
			cin>>a>>b>>c;
			printf("%d\n",dp[c][a][b]);
		}
	}
}

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

using namespace std;

#define chmin(a,b) a=min(a,b)
#define rep(i,x) for(int i=0;i<(x);++i)

const int inf = 1e9;

int N, M;
int mc[100][100], mt[100][100];

int main()
{
	while (scanf("%d %d", &N, &M), N || M) {
		fill(mc, mc[M], inf);
		fill(mt, mt[M], inf);

		rep(i, M) mc[i][i] = mt[i][i] = 0;

		rep(i, N) {
			int a, b, cost, time; scanf("%d %d %d %d", &a, &b, &cost, &time); a--; b--;
			mc[a][b] = mc[b][a] = cost;
			mt[a][b] = mt[b][a] = time;
		}

		rep(k, M) rep(i, M) rep(j, M) {
			chmin(mc[i][j], mc[i][k] + mc[j][k]);
			chmin(mt[i][j], mt[i][k] + mt[j][k]);
		}

		int K; scanf("%d", &K);
		rep(i, K) {
			int p, q, r; scanf("%d %d %d", &p, &q, &r); p--; q--;
			printf("%d\n", r == 0 ? mc[p][q] : mt[p][q]);
		}
	}
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

int main()
{
	int n, m;
	while (scanf("%d %d", &n, &m), n | m)
	{
		int d[2][101][101];
		for (int i = 0; i <= m; ++i)
			for (int j = 0; j <= m; ++j)
				d[0][i][j] = d[1][i][j] = 100000000;
		for (int i = 0; i <= m; ++i)
			d[0][i][i] = d[1][i][i] = 0;

		for (int i = 0; i < n; ++i)
		{
			int a, b, cost, time;
			scanf("%d %d %d %d", &a, &b, &cost, &time);
			d[0][a][b] = d[0][b][a] = cost;
			d[1][a][b] = d[1][b][a] = time;
		}
		for (int k = 0; k <= m; ++k)
			for (int i = 0; i <= m; ++i)
				for (int j = 0; j <= m; ++j)
					for (int l = 0; l < 2; ++l)
						d[l][i][j] = min(d[l][i][j], d[l][i][k] + d[l][k][j]);

		int queries;
		scanf("%d", &queries);
		for (int i = 0; i < queries; ++i)
		{
			int p, q, r;
			scanf("%d %d %d", &p, &q, &r);
			printf("%d\n", d[r][p][q]);
		}
	}

	return 0;
}

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

#define INF 100000000

int main(){
  int N,M;
  cin >> N >> M;
  int glaf[N+1][N+1];
  for(int i=0;i<=N;i++){
    for(int j=0;j<=N;j++){
      glaf[i][j] = INF;
    }
    glaf[i][i] = 0;
  }

  int s,t,d;
  for(int i=0;i<M;i++){
    cin >> s >> t >> d;
    glaf[s][t] = min(d,glaf[s][t]);
  }

  for(int k=1;k<=N;k++)
    for(int i=1;i<=N;i++)
      for(int j=1;j<=N;j++)
        glaf[i][j] = min(glaf[i][j],glaf[i][k]+glaf[k][j]);

  int ans = 0;
  for(int i=1;i<=N;i++){
    int g = (i==N) ? 1 : i+1;
    if(glaf[i][g]==INF){
      cout << -1 << endl;
      return 0;
    }
    ans += glaf[i][g];
  }
  // for(int i=1;i<=N;i++){
  //   for(int j=1;j<=N;j++)
  //     printf("%3d ",glaf[i][j]);
  //   cout << endl;
  // }
  cout << ans << endl;

  return 0;
}

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;
}