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<cstdio>
#include<algorithm>
#include<functional>
#include<queue>
#define F1 first.first
#define F2 first.second
#define F3 second.first
#define F4 second.second
#define MM 2000000000
using namespace std;
typedef pair<int,int> P1;
typedef pair<P1,P1> P2;
P2 p[6000];
int st[102];
int leng[101],flg[101];
int main(void)
{
	int n,m,a,b,co,ti,i,j,pp,q,r,x,p1,p2,le,xx,n2,k;
	P1 ppp;
	while(1)	{
		scanf("%d %d",&n,&m);
		if(n==0 && m==0)	break;
		priority_queue<P1,vector<P1>,greater<P1> >qu;	
		for(i=0;i<n;i++)	{
			scanf("%d %d %d %d",&a,&b,&co,&ti);
			p[i].F1=a;	p[i].F2=b;	p[i].F3=co;	p[i].F4=ti;
			p[i+n].F1=b; p[i+n].F2=a; p[i+n].F3=co; p[i+n].F4=ti;
		}
		n2=n*2;
		sort(p,p+n2);
//		for(i=0;i<n2;i++)	printf("i=%d %d %d %d %d\n",i,p[i].F1,p[i].F2,p[i].F3,p[i].F4);
		for(i=1;i<=m;i++)	st[i]=-1;
		for(i=0;i<n2;i++)	{
			if(st[p[i].F1]==-1)	 st[p[i].F1]=i;
		}
		st[m+1]=n2;
		scanf("%d",&k);
		for(i=0;i<k;i++)	{
			scanf("%d %d %d",&p1,&q,&r);
			for(j=1;j<=m;j++)	{
				leng[j]=MM;	flg[j]=0;
			}
			leng[p1]=0;	qu.push(P1(leng[p1],p1));
			while(1)	{
				while(1)	{
					ppp=qu.top();	qu.pop();
					x=ppp.second;	xx=ppp.first;
					if(flg[x]==0)	break;
				}
				flg[x]=1;
				if(x==q)	break;
				for(j=st[x];j<st[x+1];j++)	{
					if(flg[p[j].F2]==0)	{
						if(r==0)	co=p[j].F3;
						else co=p[j].F4;
						qu.push(P1(xx+co,p[j].F2));
					}
				}
			}
			printf("%d\n",xx);
			while(qu.size())	qu.pop();
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define vi vector<int>
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const ll int INF = 1e18;
ll int ti[101][101];//time
ll int co[101][101];//cost
int n,m;
int main(){
	while(1){
		cin >> n >> m; 
		if(n == 0 && m == 0) break;
		REP(i,m){
			REP(j,m){
				if(i == j){
					ti[i][i] = co[i][i] = 0;
				}else{
					ti[i][j] = co[i][j] = INF;
				}
			}
		}
		REP(i,n){
			ll int a,b,cost,tim;
			cin >> a >> b >> cost >> tim;
			a--; b--;
			co[a][b] = cost;
			co[b][a] = cost;
			ti[a][b] = tim;
			ti[b][a] = tim;
		}
		//Warshall???Floyd Algorithm
		REP(i,m){
			REP(j,m){
				REP(k,m){
					ti[i][j] = min(ti[i][j], ti[i][k] + ti[k][j]);
					co[i][j] = min(co[i][j], co[i][k] + co[k][j]);
				}
			}
		}
		int k;
		cin >> k;
		REP(i,k){
			int a,b,r;
			cin >> a >> b >> r;
			a--; b--;
			if(r == 0) cout << co[a][b] << endl;
			else cout << ti[a][b] << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
using namespace std;

static const int MAX_ST = 100;
static const int INFTY  = ( 1 << 20 );

pair< int, int > AdjMt[MAX_ST][MAX_ST];	// first : cost, second : time

int Min( const int& left, const int& right ) { return left > right ? right : left; }

int main()
{

	ios::sync_with_stdio( false );	// for speeding up

	int  info_n, sta_n, query_n, depplc, dest, cost, time;
	bool query_meth;

	while( cin >> info_n >> sta_n, info_n || sta_n ) {

		for( int i = 0; i < MAX_ST; i++ ) {
			for( int j = 0; j < MAX_ST; j++ ) AdjMt[i][j].first = AdjMt[i][j].second = INFTY;
		}

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

			cin >> depplc >> dest >> cost >> time;
			AdjMt[--depplc][--dest].first = cost;
			AdjMt[depplc][dest].second    = time;

		}

		// Warshall-Floyd
		for( int k = 0; k < sta_n; k++ ) {
			for( int i = 0; i < sta_n; i++ ) {
				if( AdjMt[i][k].first == INFTY ) continue;
				for( int j = 0; j < sta_n; j++ ) {

					AdjMt[i][j].first  = Min( AdjMt[i][j].first , AdjMt[i][k].first  + AdjMt[k][j].first );
					AdjMt[i][j].second = Min( AdjMt[i][j].second, AdjMt[i][k].second + AdjMt[k][j].second );

				}
			}
		}

		cin >> query_n;
		for( int i = 0; i < query_n; i++ ) {

			cin >> depplc >> dest >> query_meth;
			depplc--; dest--;
			cout << ( !query_meth ? AdjMt[depplc][dest].first : AdjMt[depplc][dest].second ) << endl;

		}

	}

	return 0;

}

C++: #include<iostream>
#include<queue>
#include<algorithm>
#include<utility>
#include<vector>
#include<climits>
using namespace std;

#define MAX_M 110

struct edge{int to,cost;};
typedef pair<int,int> P;

vector<edge> c[2][MAX_M];

int dijkstra(int s,int e,bool r){
	int d[MAX_M];
	for(int i=0;i<MAX_M;i++){
		d[i]=INT_MAX;
	}
	priority_queue<P,vector<P>,greater< P > > que;
	
	d[s]=0;
	
	que.push(P(0,s));
	
	while(!que.empty()){
		P p=que.top();que.pop();
		int v=p.second;
		if(d[v]<p.first) continue;
		for(int i=0;i<c[r][v].size();i++){
			edge e=c[r][v][i];
			if(d[e.to]>d[v]+e.cost){
				d[e.to]=d[v]+e.cost;
				que.push(P(d[e.to],e.to));
			}
		}
	}
	
	return d[e];
}

int main(){
	int n,m;
	while(cin>>n>>m,n!=0||m!=0){
		for(int r=0;r<2;r++){
			for(int i=0;i<MAX_M;i++){
				c[r][i].clear;
			}
		}
		for(int i=0;i<n;i++){
			int a,b,cos,tim;
			cin>>a>>b>>cos>>tim;
			edge co,t;
			co.to=b;co.cost=cos;
			t.to=b;t.cost=tim;
			c[0][a].push_back(co);
			c[1][a].push_back(t);
		}
		cin>>n;
		for(int i=0;i<n;i++){
			int s,e;
			bool r;
			cin>>s>>e>>r;
			int ans=dijkstra(s,e,r);
			cout<<ans<<endl;
		}
	}
}