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<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(){
	while(1){
	int s=0,min=INT_MAX,m,tul[102][102],yv[102][102]={0},tsag[102][102]={0};
	scanf("%d %d",&n,&m);
	if(n==0 || m==0) break;
	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<bits/stdc++.h>
using namespace std;
int N,M;

int main()
{
	while(true){
		scanf("%d%d",&N,&M);
		if(N==0&&M==0)return 0;
		int cost[200][200];
		int time[200][200];
		int minc[200][200];
		int mint[200][200];
		for(int i=0;i<M;i++){
			for(int j=0;j<M;j++){
				cost[i][j]=1<<25;
				time[i][j]=1<<25;
				minc[i][j]=1<<25;
				mint[i][j]=1<<25;
				if(i==j){
					cost[i][j]=0;
					time[i][j]=0;
					minc[i][j]=0;
					mint[i][j]=0;
				}
			}
		}
		int A,B,co,ti;
		for(int i=0;i<N;i++){
			scanf("%d%d%d%d",&A,&B,&co,&ti);A--;B--;
			cost[A][B]=co;cost[B][A]=co;
			time[A][B]=ti;time[B][A]=ti;
		}
		for(int i=0;i<M;i++){
			for(int j=0;j<M;j++){
				for(int k=0;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;
		scanf("%d",&K);
		for(int i=0;i<K;i++){
			int P,Q,R;
			scanf("%d%d%d",&P,&Q,&R);P--;Q--;
			if(R==0)printf("%d\n",cost[P][Q]);
			else printf("%d\n",time[P][Q]);
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

typedef vector< int > VI;
typedef vector< VI > VVI;

int main(){
	while(true){
		int n, m;

		cin >> n >> m;

		if(n == 0 && m == 0){
			break;
		}

		VVI map_cost(m, VI(m, INT_MAX / 4));
		VVI map_time(m, VI(m, INT_MAX / 4));

		for(int i = 0; i < m; i++){
			map_cost[i][i] = 0;
			map_time[i][i] = 0;
		}

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

			a--;
			b--;

			map_cost[a][b] = cost;
			map_time[a][b] = time;
			map_cost[b][a] = cost;
			map_time[b][a] = time;
		}

		for(int k = 0; k < m; k++){
			for(int i = 0; i < m; i++){
				for(int j = 0; j < m; j++){
					map_cost[i][j] = min(map_cost[i][j], map_cost[i][k] + map_cost[k][j]);
					map_time[i][j] = min(map_time[i][j], map_time[i][k] + map_time[k][j]);
				}
			}
		}

		int k;
		cin >> k;

		int p, q, r;

		for(int i = 0; i < k; i++){

			cin >> p >> q >> r;

			p--;
			q--;

			if(r == 0){
				cout << map_cost[p][q] << endl;
			}
			else if(r == 1){
				cout << map_time[p][q] << endl;
			}
		}
	}
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<utility>

using namespace std;

int n,m;

long long gc[110][110];
long long gt[110][110];

void warshallfloyd()
{
	for (int k = 0; k < m; k++)
	{
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < m; j++)
			{
				gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]);
				gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]);
			}
		}
	}
}

int main(void)
{
	while (cin >> n >> m, n || m)
	{

		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				gc[i][j] = 9999999;
				gt[i][j] = 9999999;
			}
		}

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

			cin >> a >> b >> c >> t;

			a--; b--;

			gc[a][b] = gc[b][a] = c;
			gt[a][b] = gt[b][a] = t;
		}

		warshallfloyd();

		int k;

		cin >> k;

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

			cin >> p >> q >> r;

			p--; q--;

			if (r == 0)
			{
				cout << gc[p][q] << endl;
			}
			else
			{
				cout << gt[p][q] << endl;
			}
		}
	}

	

	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

const int N = 300;
const int inf = (1 << 21);
int cost[2][N][N];

void calc(bool a,int n){
  rep(k,n)
    rep(i,n)
    rep(j,n)
      cost[a][i][j]=min(cost[a][i][j],cost[a][i][k]+cost[a][k][j]);
}

main(){
  int n,m;
  while(cin>>m>>n && n){
    rep(i,n){
      rep(j,n)cost[0][i][j]=cost[1][i][j]=inf;
      cost[0][i][i]=cost[1][i][i]=0;
    }
    
    rep(i,m){
      int f,t,mo,ti;
      cin>>f>>t>>mo>>ti;
      f--;t--;
      cost[0][f][t]=cost[0][t][f]=mo;
      cost[1][f][t]=cost[1][t][f]=ti;
    }

    calc(0,n);
    calc(1,n);

    int q;
    cin>>q;
    rep(i,q){
      int f,t,r;
      cin>>f>>t>>r;
      f--;t--;
      cout << cost[r][f][t] << endl;

    }

  }
  return 0;
}