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]
					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 <map>

using namespace std;

typedef pair<int, int> pii;

int c[101][101] = { { 0 } };
int t[101][101] = { { 0 } };
int n, m;

int calculate(int mat[101][101], int from, int to){
	map<int, int> locations;
	for(int i = 1; i <= m; i++){
		if(mat[from][i] != 0){
			locations.insert(pii(mat[from][i], i));
		}
	}
	while(!locations.empty()){
		map<int, int>::iterator it = locations.begin();
		int d = (*it).first, f = (*it).second;
		if(f == to){ break; }
		for(int i = 1; i <= m; i++){
			if(mat[f][i] != 0 && (d + mat[f][i] < mat[from][i] || mat[from][i] == 0)){
				int dist = d + mat[f][i];
				mat[from][i] = mat[i][from] = dist;
				locations.insert(pii(dist, i));
			}
		}
		locations.erase(it);
	}
	return mat[from][to];
}

int main(){
	while(true){
		for(int i = 0; i < 101; i++){
			for(int j = 0; j < 101; j++){
				c[i][j] = t[i][j] = 0;
			}
		}
		scanf("%d%d", &n, &m);
		if(n == 0 || m == 0){ break; }
		for(int i = 0; i < n; i++){
			int a, b, cost, time;
			scanf("%d%d%d%d", &a, &b, &cost, &time);
			c[a][b] = c[b][a] = cost;
			t[a][b] = t[b][a] = time;
		}
		int k;
		scanf("%d", &k);
		while(k--){
			int p, q, r;
			scanf("%d%d%d", &p, &q, &r);
			printf("%d\n", calculate((r == 0) ? c : t, p, q));
		}
	}
	return 0;
}

C++: #include <stdio.h>
#define INF 100000000

int main(void){
	int n,m;
	
	while(scanf("%d%d",&m,&n),n,m){
		int i,j;
		int a,b,cost,time,c;
		int DPc[128][128];
		int DPt[128][128];
		int k,ans[128],count=0;
		
		for(i=0;i<128;i++){
			for(j=0;j<128;j++){
				if(i==j){
					DPc[i][j]=0;
					DPt[i][j]=0;
				}
				else{
					DPc[i][j]=INF;
					DPt[i][j]=INF;
				}
			}
		}
		
		for(i=0;i<m;i++){
			scanf("%d%d%d%d",&a,&b,&cost,&time);
			DPc[a][b]=cost;
			DPc[b][a]=cost;
			DPt[a][b]=time;
			DPt[b][a]=time;
		}
		for(i=0;i<n;i++){
			for(j=0;j<=n;j++){
				for(k=0;k<=n;k++){
					if(DPt[i][k]+DPt[k][j]<DPt[i][j]){
						DPt[i][j]=DPt[i][k]+DPt[k][j];
					}
					if(DPc[i][k]+DPc[k][j]<DPc[i][j]){
						DPc[i][j]=DPc[i][k]+DPc[k][j];
					}
				}
			}
		}
		
		scanf("%d", &m);
		for(i=0;i<m;i++){
			scanf("%d%d%d",&a,&b,&c);
			if(c==1){
				ans[count]=DPt[a][b];
			}
			else {
				ans[count]=DPc[a][b];
			}
			count++;
		}
		for(i=0;i<m;i++){
			printf("%d\n",ans[i]);
		}
	}
	return 0;
}

C++: #include<iostream>
#define INF 2 << 20
using namespace std;
int main(){
  int n,m;
  cin >> n >> m;
  int cost[m+1][m+1];
  int time[m+1][m+1];

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

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

  for (int i = 1; i <= m; i++)      // ?????±????????????
    for (int j = 1; j <= m; j++)    // ????§???????
      for (int k = 1; k <= m; k++){  // ??????
        cost[j][k] = min(cost[j][k], cost[j][i] + cost[i][k]);
        time[j][k] = min(time[j][k], time[j][i] + time[i][k]);
      }


  int k;
  cin >> k;
  int p,q,r;
  while(cin>>p>>q){
    if(p==0&&q==0) break;
    cin >> r;
    switch(r){
    case 0: cout << cost[p][q] << endl; break;
    case 1: cout << time[p][q] << endl; break;
    default: break;
    }
  }
}

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

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

using namespace std;

const int INF=1<<29;

template<class T>
class binary_heap{
	const static int N_MAX=3000;
	int n;
	T a[N_MAX+1];
public:
	binary_heap():n(0){}
	const T &find_min()const{ return a[1]; }
	void insert(const T &x){
		int u=++n;
		a[u]=x;
		while(u!=1 && a[u/2]>a[u]) swap(a[u/2],a[u]), u/=2;
	}
	void delete_min(){
		a[1]=a[n--];
		for(int u=1;;){
			int v=u;
			if(2*u+0<=n && a[v]>a[2*u+0]) v=2*u+0;
			if(2*u+1<=n && a[v]>a[2*u+1]) v=2*u+1;
			if(v==u) break;
			swap(a[v],a[u]);
			u=v;
		}
	}
	bool empty()const{ return n==0; }
	int size()const{ return n; }
};

struct edge{ int v,cost1,cost2; };

int main(){
	for(int m,n;scanf("%d%d",&m,&n),n;){
		vector<edge> G[100];
		rep(i,m){
			int u,v,cost1,cost2; scanf("%d%d%d%d",&u,&v,&cost1,&cost2); u--; v--;
			G[u].push_back((edge){v,cost1,cost2});
			G[v].push_back((edge){u,cost1,cost2});
		}

		int q; scanf("%d",&q);
		while(q--){
			int s,t,type; scanf("%d%d%d",&s,&t,&type); s--; t--;

			int d[100];
			rep(u,n) d[u]=(u==s?0:INF);
			static binary_heap< pair<int,int> > H; H.insert(make_pair(0,s));
			while(!H.empty()){
				int d_now=H.find_min().first,u=H.find_min().second;
				H.delete_min();

				if(d[u]<d_now) continue;

				rep(i,G[u].size()){
					const edge &e=G[u][i];
					int d2=d_now+(type==0?e.cost1:e.cost2);
					if(d[e.v]>d2){
						d[e.v]=d2;
						H.insert(make_pair(d2,e.v));
					}
				}
			}

			printf("%d\n",d[t]);
		}
	}

	return 0;
}

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