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<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

const int MAXN = 3000, MAXM = 100, C = 0, T = 1;
const int INF = 1 << 28;
int cost[2][MAXM + 10][MAXM + 10];

int main()
{
    int n, m;
    while(cin >> n >> m && n)
    {
        int a, b, cc, ct;
        REP(MAXM)rep(j,MAXM)rep(e,2)cost[e][i][j] = INF;
        REP(n)
        {
            cin >> a >> b >> cc >> ct; a--; b--;
            cost[C][a][b] = cost[C][b][a] = min(cc, cost[C][a][b]);
            cost[T][a][b] = cost[T][b][a] = min(ct, cost[T][a][b]);
        }

        int k, p, q, r;
        cin >> k;
        int d[MAXM] = {}, used[MAXM] = {};
        rep(s,k)
        {
            cin >> p >>q >> r; p--; q--;
            memset(used,0, sizeof(used));
            REP(m) d[i] = INF;
            d[p] = 0;
            while(true)
            {
                int v = -1;
                rep(i, m)if(!used[i] && (v == -1 || d[i] < d[v])) v = i;
                if(v == -1) break;
                used[v] = true;
                rep(i,m) d[i] = min(d[i], d[v] + cost[r][v][i]);
            }
            cout << d[q] << endl;
        }
    }
    return 0;
}

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

#define NONE INT_MAX>>1

vector< vector<int> > WarshallFloyd(vector< vector<int> >A)
{
  for(int k=0;k<A.size();k++)for(int i=0;i<A.size();i++)for(int j=0;j<A.size();j++)
    A[i][j]=min(A[i][j],A[i][k]+A[k][j]);
  return A;
}

int main()
{
  int n,m,a,b,cost,time,k,p,q,r;
  vector< vector<int> >M,N;
  while(cin>>n>>m,n|m){
    M.resize(m);
    N.resize(m);
    for(int i=0;i<m;i++){
      M[i].resize(m,NONE);
      N[i].resize(m,NONE);
    }
    for(int i=0;i<n;i++){
      cin>>a>>b>>cost>>time;
      a--;b--;
      M[a][b]=M[b][a]=cost;
      N[a][b]=N[b][a]=time;
    }
    M=WarshallFloyd(M);
    N=WarshallFloyd(N);
    cin>>k;
    for(int i=0;i<k;i++){
      cin>>p>>q>>r;
      p--;q--;
      if(r==0)cout<<M[p][q]<<endl;
      else cout<<N[p][q]<<endl;
    }
  }
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
#define MAX 10000000
#define MAX_ST 101 //MAX_STATION
#define LP(i,n) for(int i=0;i<n;i++)
class Node{
public:
	int at;
	int cost;
	Node(int a,int c){
		at=a;cost=c;
	}
	bool operator < (const Node &o) const{
		return cost>o.cost;
	}
};
int feeMap[MAX_ST][MAX_ST];
int timeMap[MAX_ST][MAX_ST];
int minMap[MAX_ST];
bool visited[MAX_ST];
typedef priority_queue<Node> PQ;

int dijkstra(int st,int ed,int n,int judge){
	PQ pq;
	pq.push(Node(st,0));
	while(!pq.empty()){
		Node cur=pq.top();pq.pop();
		if(cur.at==ed)return cur.cost;
//		cout<<"now:"<<cur.at<<endl;
		for(int i=1;i<=n;i++){
			if((judge&&timeMap[cur.at][i]==MAX)||(!judge&&feeMap[cur.at][i]==MAX)||cur.at==i)continue;
//			cout<<"    next:"<<i<<endl;
			int cost=judge?timeMap[cur.at][i]:feeMap[cur.at][i];
			if(minMap[i]<cur.cost+cost)continue;
			else minMap[i]=cur.cost+cost;
			pq.push(Node(i,cur.cost+cost));
		}
	}
	cout<<"not found"<<endl;
	return MAX;
}

void init(){
	LP(i,MAX_ST)LP(j,MAX_ST){feeMap[i][j]=timeMap[i][j]=minMap[i]=MAX;visited[i]=false;}
}
void printMap(int n){
	LP(i,n){LP(j,n)printf("%8d",feeMap[i][j]!=MAX?feeMap[i][j]:-1);cout<<endl;}
	cout<<"---------------------------------------------------------"<<endl;
	LP(i,n){LP(j,n)printf("%8d",timeMap[i][j]!=MAX?timeMap[i][j]:-1);cout<<endl;}
}
int main(){
	int n,m;
	while(cin>>n>>m&&(n||m)){
		init();
		int _n=n;
		//ツ静シツ路ツ湘ョツ陛ア
		for(int a,b,cost,time;n--&&cin>>a>>b>>cost>>time;){
			if(feeMap[a][b]>cost)feeMap[a][b]=feeMap[b][a]=cost;
			if(timeMap[a][b]>time)timeMap[a][b]=timeMap[b][a]=time;
		}
		//printMap(_n);
		int d;
		cin>>d;
		//ツ姪「ツつ「ツ債づュツつケ
		for(int p,q,r;d--&&cin>>p>>q>>r;){
			cout<<dijkstra(p,q,_n,r)<<endl;
		}
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#include<iostream>
#include<queue>
#define rep(i,a,b) for(i=a;i<b;i++)
#define F1 first.first
#define F2 first.second
#define F3 second
#define INF 2100000000
using namespace std;
typedef pair<pair<int,int>,int> P1;
typedef pair<int,int> P2;
int main(void)
{
	int i,j,k,n,m,x,s,e;
	int a,b,cos,tim,st,en,way;
	int leng[1001],kami[1002];
	P1 glid1[6000],glid2[6000];
	P2 tori;
	while(cin>>n>>m,n&&m) {
		rep(i,0,n) {
			cin>>a>>b>>cos>>tim;
			glid1[i*2].F1=a,glid1[i*2].F2=b,glid1[i*2].F3=cos;
			glid2[i*2].F1=a,glid2[i*2].F2=b,glid2[i*2].F3=tim;
			glid1[i*2+1].F1=b,glid1[i*2+1].F2=a,glid1[i*2+1].F3=cos;
			glid2[i*2+1].F1=b,glid2[i*2+1].F2=a,glid2[i*2+1].F3=tim;
		}
		sort(glid1,glid1+m*2);sort(glid2,glid2+m*2);
		kami[1]=0;
		rep(i,1,m*2) {
			if(glid1[i].F1!=glid1[i-1].F1) {
				x=glid1[i].F1;
				kami[x]=i;
			}
		}
		kami[n+1]=m*2;
		priority_queue<P2,vector<P2>,greater<P2> > que;
		cin>>k;
		rep(i,0,k) {
			cin>>st>>en>>way;
			for(j=1;j<=n;j++) leng[j]=INF;
			while(!que.empty()) que.pop();
			que.push(P2(0,st));
			while(!que.empty()) {
				tori=que.top(),que.pop();
				a=tori.first,b=tori.second;
				if(a<leng[b]) {
					leng[b]=a;
					if(b==en) break;
					s=kami[b];e=kami[b+1];
					if(way==0)rep(j,s,e) que.push(P2(a+glid1[j].F3,glid1[j].F2));
					else rep(j,s,e) que.push(P2(a+glid2[j].F3,glid2[j].F2));
				}
			}
			cout<<leng[en]<<endl;
		}
	}
	
}