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<bits/stdc++.h>
using namespace std;
#define INF 1e7
struct edge{int to,cost,time;};
vector<edge> G[101];
int d[101];
bool visited[101];
int V;
void dijkstra(int p,int r){
  fill(d,d+101,INF);
  d[p]=0;
  memset(visited,false,sizeof(visited));
  while(1){
    int id=-1;
    for(int i=0;i<V;++i){
      if(!visited[i])
        if(id==-1 || d[id]>d[i])
          id=i;
    }
    if(id==-1) break;
    visited[id]=true;
    for(int i=0;i<G[id].size();++i){
      edge e=G[id][i];
      if(r) d[e.to]=min(d[e.to],d[id]+e.time);
      else d[e.to]=min(d[e.to],d[id]+e.cost);
    }
  }
}
int main(){
  int n;
  while(cin>>n>>V,n){
    for(int i=0;i<101;++i) G[i].clear();
    for(int i=0;i<n;++i){
      int a,b,c,t; cin>>a>>b>>c>>t;
      G[a].push_back((edge){b,c,t});
      G[b].push_back((edge){a,c,t});
    }
    int k; cin>>k;
    for(int i=0;i<k;++i){
      int p,q,r; cin>>p>>q>>r;
      dijkstra(p,r);
      cout<<d[q]<<endl;
    }
  }
  return 0;
}

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

#define MIN(x,y) ((x<y)? x:y)
#define INF (100000000)

using namespace std;

int date[2][105][105];
int answer[105];//最短距離
int subdate[105];//0...最短が決定,定数...次行ける,INF...次行けない
int n,m;
int a,b,cost,time;
int k;
int p,q,r;

int main()
{
	while(true)
	{
		scanf("%d%d",&n,&m);
		for(int j=0;j<=m;j++)for(k=0;k<=m;k++)date[0][j][k]=date[1][j][k]=INF;
		if(n==0&&m==0)break;
		for(int i=0;i<n;i++)
		{
			scanf("%d%d%d%d",&a,&b,&cost,&time);
			date[0][a][b]=date[0][b][a]=cost;
			date[1][a][b]=date[1][b][a]=time;
		}
		scanf("%d",&k);
		for(int i=0;i<k;i++)
		{
			scanf("%d%d%d",&p,&q,&r);
			fill(subdate,subdate+105,INF);
			answer[104]=p;
			answer[p]=0;
			subdate[p]=0;
			while(true)
			{
				answer[101]=-1;
				//最後に更新した点から行けるところを更新
				//そして、最短経路が決まっているところから
				//後ひと駅で行ける場所の中で最短を最短経路として決定する
				answer[103]=INF;
				for(int j=1;j<=m;j++)
				{
					subdate[j]=MIN(subdate[j],answer[answer[104]]+date[r][answer[104]][j]);
				}
				for(int j=1;j<=m;j++)
				{
					if(subdate[j]!=0&&answer[103]>subdate[j])
					{
						answer[103]=subdate[j];
						answer[104]=j;
					}
				}
				if(answer[103]==INF)break;
				answer[answer[104]]=answer[103];
				subdate[answer[104]]=0;
			}
			printf("%d\n",answer[q]);
		}
	}
	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]={0},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){
		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; 
			if(c==0) go(a);
			else go1(a);
			printf("%d\n",min);
		}
		int tul[102][102]={0},yv[102][102]={0},tsag[102][102]={0};
	}
}

C++: #include<iostream>
using namespace std;
#include<algorithm>
#include<string>
#define REP(i,a) for(int  i=0;i<a;i++)
int A[400][400];
int B[400][400];
int main(){
	int a,b=0,c=0,d,n,m;	
	cin>>n>>m;
	while(n!=0||m!=0){
	REP(i,m){
		REP(j,m){
			A[i][j]=1001;
			B[i][j]=1001;
		}
	}
	REP(i,n){
	cin>>a>>b>>c>>d;
	A[a-1][b-1]=c;
	B[a-1][b-1]=d;	
	A[b-1][a-1]=c;
	B[b-1][a-1]=d;
	}
	REP(i,m){
		REP(j,m){
	 		REP(k,m){
		A[i][j]=min(A[i][j],A[i][k]+A[k][j]);
		B[i][j]=min(B[i][j],B[i][k]+B[k][j]);		
			}
		}	
	}
	cin>>n;
	REP(i,n){
	cin>>a>>b>>c;
	if(c==0) cout<<A[a-1][b-1]<<endl;
	else cout<<B[a-1][b-1]<<endl;
	}
	cin>>n>>m;
	}
	return 0;
}