Go: // Warshall–Floyd Algorithm

package main

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

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
					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<bits/stdc++.h>
using namespace std;
typedef struct E{
  int to,cost,time;
}edge;
vector<vector<edge> >root(101);
int d[2][101];
void dijkstraco(int s){
  priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >que;
  d[0][s] = 0;
  que.push(pair<int,int>(0,s));
  while(!que.empty()){
    pair<int,int> p=que.top();que.pop();
    int v = p.second;
    if(d[0][v]<p.first)continue;
    for(int j=0;j<root[v].size();j++){
      edge t = root[v][j];
      if(d[0][t.to]>d[0][v]+t.cost){
	d[0][t.to]=d[0][v]+t.cost;
	que.push(pair<int,int>(d[0][t.to],t.to));
      }
    }
  }
}
void dijkstrati(int s){
  priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >que;
  d[1][s] = 0;
  que.push(pair<int,int>(0,s));
  while(!que.empty()){
    pair<int,int> p=que.top();que.pop();
    int v = p.second;
    if(d[1][v]<p.first)continue;
    for(int j=0;j<root[v].size();j++){
      edge t = root[v][j];
      if(d[1][t.to]>d[1][v]+t.time){
	d[1][t.to]=d[1][v]+t.time;
	que.push(pair<int,int>(d[1][t.to],t.to));
      }
    }
  }
}
int main(){
  int n,m;
  while(cin>>n>>m,n+m!=0){
    for(int i=0;i<n;i++){
      int f;cin>>f;
      edge e;cin>>e.to>>e.cost>>e.time;
      root[f].push_back(e);
    }
    int k;cin>>k;
    for(int i=0;i<k;i++){
      for(int j=0;j<2;j++){
	for(int a=0;a<101;a++)
	  d[j][a]=1000000000;
      }
      int p,q,r;cin>>p>>q>>r;
      if(r==0)
	dijkstraco(p);
      else
	dijkstrati(p);
      cout<<d[r][q]<<endl;

    }
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

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

int N,M,K;
const int MAXN=128;
const int INF=1e9;
int G1[MAXN][MAXN];
int G2[MAXN][MAXN];

int main()
{
	while(cin>>M>>N&&(M||N))
	{
		rep(i,N){ rep(j,N){ G1[i][j]=INF; G2[i][j]=INF; } }

		rep(i,M)
		{ 
			int a,b,c,d;
			cin>>a>>b>>c>>d; --a; --b;
			G1[a][b]=G1[b][a]=c;
			G2[a][b]=G2[b][a]=d;
		}

		rep(from,N) rep(to,N) rep(point,N)
		{
			G1[from][to] = min(G1[from][to],G1[from][point]+G1[point][to]);
			G2[from][to] = min(G2[from][to],G2[from][point]+G2[point][to]);
		}

		cin>>K;
		rep(i,K)
		{
			int a,b,c; cin>>a>>b>>c; --a; --b;
			cout<<(c?G1[a][b]:G2[a][b])<<endl;
		}
	}

	return 0;
}

C++: // 2012/11/09 Tazoe

#include <iostream>
#include <algorithm>
using namespace std;

const int INF = 1000000000;

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

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

		int C[100][100];
		int T[100][100];
		for(int i=0; i<m; i++){
			for(int j=0; j<m; j++){
				if(i==j){
					C[i][j] = T[i][j] = 0;
				}
				else{
					C[i][j] = T[i][j] = INF;
				}
			}
		}

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

			C[a-1][b-1] = C[b-1][a-1] = c;
			T[a-1][b-1] = T[b-1][a-1] = t;
		}

		for(int h=0; h<m; h++){
			for(int i=0; i<m; i++){
				for(int j=0; j<m; j++){
					C[i][j] = min(C[i][j], C[i][h]+C[h][j]);
					T[i][j] = min(T[i][j], T[i][h]+T[h][j]);
				}
			}
		}

		int k;
		cin >> k;

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

			if(r==0){
				cout << C[p-1][q-1] << endl;
			}
			else{
				cout << T[p-1][q-1] << endl;
			}
		}
	}

	return 0;
}

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

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

int main(void){

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

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

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