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 <queue>
using namespace std;

#define MMAX 101
const int INF=1<<10;

int gtime[MMAX+1][MMAX+1];
int gcost[MMAX+1][MMAX+1];

int n,m;
int p,q,r;

struct node{
	int st;//
	int cost;//
}typedef Node;

bool operator < (const Node n1,const Node n2){
	return n1.cost > n2.cost;
}
bool visited[MMAX];
int dijkstra(){
	fill(visited,visited+MMAX,false);
	priority_queue<Node> pq;
	Node start={p,0};
	pq.push(start);
	while(!pq.empty()){
		Node cur=pq.top();pq.pop();
		if(cur.st==q)return cur.cost;
		if(visited[cur.st])continue;
		visited[cur.st]=true;
		for(int i=1;i<=m;i++){
			if(cur.st==i||gtime[cur.st][i]==INF||gcost[cur.st][i]==INF)continue;
			if(visited[i])continue;
			if(r==1){
				Node add={i,cur.cost+gtime[cur.st][i]};
				pq.push(add);
			}
			else {//
				Node add={i,cur.cost+gcost[cur.st][i]};
				pq.push(add);
			}
		}
	}
}
void solve(){
	cout<<dijkstra()<<endl;
}
int main(){
	int a,b,cost,time,k;
	while(cin>>n>>m,(n||m)){
		for(int i=0;i<MMAX;i++){
			fill(gtime[i],gtime[i]+MMAX,INF);
			fill(gcost[i],gcost[i]+MMAX,INF);
		}
		for(int i=0;i<n;i++){
			cin>>a>>b>>cost>>time;
			if(gtime[a][b]>time)gtime[a][b]=gtime[b][a]=time;
			if(gcost[a][b]>cost)gcost[a][b]=gcost[b][a]=cost;
		}
		cin>>k;
		for(int i=0;i<k;i++){
			cin>>p>>q>>r;
			solve();
		}		
	}
	return 0;
}

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

const int INF=10000;
int main(){
    //cout<<"線路の数　駅の数"<<endl;
    int n,m,a,b,cost,time,k,p,q,r,w;
    cin>>n>>m;
    int c[m][m];
    int t[m][m];
    int dc[m][m];
    int dt[m][m];
    for(int i=0;i<m;i++){
        for(int j=0;j<m;j++){
            c[i][j]=t[i][j]=INF;
        }
    }
    for(int i=0; i<n; i++){
        //cout<<"第"<<i+1<<"の線路の情報 a b cost time"<<endl;
        cin>>a>>b>>cost>>time;
        c[a-1][b-1]=cost;
        c[b-1][a-1]=cost;
        t[a-1][b-1]=time;
        t[b-1][a-1]=time;
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<m;j++){
           dc[i][j]=c[i][j];
           dt[i][j]=t[i][j];
        }
    }

    for(int z=0;z<m;z++){
        for(int i=0;i<m;i++){
            for(int j=0;j<m;j++){
                if(dc[i][j]>dc[i][z]+dc[z][j]){
                    dc[i][j]=dc[i][z]+dc[z][j];
                }
                if(dt[i][j]>dt[i][z]+dt[z][j]){
                    dt[i][j]=dt[i][z]+dt[z][j];
                }
            }
        }
    }
    //cout<<"問い合わせの数"<<endl;
    cin>>k;
    int ans[200];
    for(int i=0;i<k;i++){
       // cout<<"第"<<i+1<<"の問い合わせ p q r"<<endl;
        cin>>p>>q>>r;
        if(r==0){
            ans[w]=dc[p-1][q-1];
            w++;
        }
        else if(r==1){
            ans[w]=dt[p-1][q-1];
            w++;
        }
    }
    cout<<"結果"<<endl;
    for(int i=0;i<k;i++){
        cout<<ans[i]<<endl;
    }
return 0;


}

C++: #include<bits/stdc++.h>
using namespace std;
#define M 100000007
typedef pair<int,int> P;
int main(void){
	int n,m,k;
	while(1){
		cin>>n>>m;
		if(n==0)break;
		vector<P> cost[m+1],time[m+1];
		for(int i=0;i<n;i++){
			int a,b,c,t;
			cin>>a>>b>>c>>t;
			cost[a].push_back(P(b,c)); cost[b].push_back(P(a,c));
			time[a].push_back(P(b,t)); time[b].push_back(P(a,t));
		}
		cin>>k;
		for(int i=0;i<k;i++){
			int s,g,f;
			int leng[m+1];
			priority_queue<P,vector<P>,greater<P> > que;
			fill(leng,leng+m+1,M);
			cin>>s>>g>>f;
			leng[s]=0;
			que.push(P(0,s));
			if(f==0){
				while(!que.empty()){
					P p=que.top(); que.pop();
					int v=p.second;
					if(leng[v]<p.first) continue;
					for(int j=0;j<cost[v].size();j++){
						P x=cost[v][j];
						if(leng[x.first]>leng[v]+x.second){
							leng[x.first]=leng[v]+x.second;
							que.push(P(leng[x.first],x.first));
						}
					}
				}
				cout<<leng[g]<<endl;
			}else{
				while(!que.empty()){
					P p=que.top(); que.pop();
					int v=p.second;
					if(leng[v]<p.first) continue;
					for(int j=0;j<time[v].size();j++){
						P x=time[v][j];
						if(leng[x.first]>leng[v]+x.second){
							leng[x.first]=leng[v]+x.second;
							que.push(P(leng[x.first],x.first));
						}
					}
				}
				cout<<leng[g]<<endl;
			}
		}
	}
	return 0;
}