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>
using namespace std;

const int INF = 100000000;

int main(int argc, char const *argv[]){
    int n, m, k;
    int cost[100][100];
    int time[100][100];

    while (cin>>n>>m, n||m){
        for (int i = 0; i < 100; i++){
            for (int j = 0; j < 100; j++){
                if (i == j) {
                    cost[i][j] = 0;
                    time[i][j] = 0;
                }
                else{
                    cost[i][j] = INF;
                    time[i][j] = INF;
                }
            }
        }

        for (int i = 0; i < n; i++){
            int a, b, c, t;
            cin>>a>>b>>c>>t;
            a--; b--;

            cost[a][b] = c;
            cost[b][a] = c;
            time[a][b] = t;
            time[b][a] = t;
        }

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

        cin>>k;
        for (int i = 0; i < k; i++)
        {
            int p, q, r;
            cin>>p>>q>>r;
            p--; q--;

            if (r){
                cout<<time[p][q]<<endl;
            }
            else{
                cout<<cost[p][q]<<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;                     // 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;                  // r=0:????°??????? 1:????????????
  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;
}

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

using namespace std;

int a,b,c,d,T,n;
int s=0,min=INT_MAX,m,tul[102][102],yv[102][102]={0},tsag[102][102]={0};

int go(int x){
	if(x==b){
		if(s<min) min=s;
		return 0;
	}
	for(int i=1 ; i<=m ; i++ ){
		if(tul[x][i]!=0 && yv[x][i]==0){
			yv[x][i]=1;
			yv[i][x]=1;
			s+=tul[x][i];
			go(i);
			s-=tul[x][i];
			yv[x][i]=0;
			yv[i][x]=0;
		}
	}
}
int go1(int x){
	if(x==b){
		if(s<min) min=s;
		return 0;
	}
	for(int i=1 ; i<=m ; i++ ){
		if(tsag[x][i]!=0 && yv[x][i]==0){
			yv[x][i]=1;
			yv[i][x]=1;
			s+=tsag[x][i];
			go1(i);
			s-=tsag[x][i];
			yv[x][i]=0;
			yv[i][x]=0;
		}
	}
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i=0 ; i<n ; i++ ){
		scanf("%d %d %d %d",&a,&b,&c,&d);
		tul[a][b]=c;
		tul[b][a]=c;
		tsag[a][b]=d;
		tsag[b][a]=d;
	}
	scanf("%d",&T);
	while(T--){
		scanf("%d %d %d",&a,&b,&c);
		s=0;
		min=INT_MAX;
		d=1;
		if(c==0) go(a);
		else go1(a);
		printf("%d\n",min);
	}
}

C++: #include<cstdio>
#include<iostream>
#include<queue>

#define NMAX 3000
#define MMAX 100
#define INF (1000*MMAX+100)*10
using namespace std;

int main(void){

	int n,m;
	int a[NMAX],b[NMAX],cost[2][NMAX];
	int k;
	int p,q,r,flag;
	int node[MMAX];

	while(1){
		cin>>n>>m;
		if(n==0||m==0) break;
		for(int i=0;i<n;i++){
			cin>>a[i]>>b[i]>>cost[0][i]>>cost[1][i];
		}
		cin>>k;
		for(int i=0;i<k;i++){
			cin>>p>>q>>r;

			for(int j=0;j<n;j++) node[j]=INF;
			node[p]=0;
			flag=1;
			while(flag){
				flag=0;
				for(int j=0;j<n;j++){
					if(node[a[j]]!=INF){
						if(node[b[j]]>node[a[j]]+cost[r][j]){
							node[b[j]]=node[a[j]]+cost[r][j];
							flag=1;
						}
					}
					if(node[b[j]]!=INF){
						if(node[a[j]]>node[b[j]]+cost[r][j]){
							node[a[j]]=node[b[j]]+cost[r][j];
							flag=1;
						}
					}
				}
			}
			
			cout<<node[q]<<endl;
		}
	}

	return 0;
}