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>

#define INF 1000000
using namespace std;

int main(){
	int data[2][110][110];
	for(int i=0;i<110;i++){
		for(int j=0;j<110;j++){
				data[1][i][j]=INF;
				data[0][i][j]=INF;
			
		}
	}	
	int n,m;
	cin>>n>>m;
	int a,b,cost,time;
	for(int i=0;i<n;i++){
		cin>>a>>b>>cost>>time;
		data[0][a][b]=cost;
		data[0][b][a]=cost;
		data[1][a][b]=time;
		data[1][b][a]=time;
	}

	for(int count=0;count<2;count++){
		for(int k=1;k<=m;k++){
			for(int i=1;i<=m;i++){
				for(int j=1;j<=m;j++)data[count][i][j]=min(data[count][i][j],data[count][i][k]+data[count][k][j]);
			}
		}
	}			

	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		int p,q,r;
		cin>>p>>q>>r;
		cout<<data[r][p][q]<<endl;
	}
}

C++: #include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

const int INFTY=1<<29;

struct Edge{
	int src,dst,weight;
	Edge(){}
	Edge(int s,int d,int w):src(s),dst(d),weight(w){}
	bool operator<(const Edge& e)const{return weight<e.weight;}
	bool operator>(const Edge& e)const{return weight>e.weight;}
};
typedef vector<vector<Edge> > Graph;

void dijkstra(const Graph& g,int start,vector<int>& dist)
{
	priority_queue<Edge,vector<Edge>,greater<Edge> > pq;
	pq.push(Edge(-1,start,0));
	while(pq.size()){
		Edge cur=pq.top(); pq.pop();
		if(dist[cur.dst]<=cur.weight) continue;
		dist[cur.dst]=cur.weight;
		for(int i=0;i<g[cur.dst].size();i++){
			const Edge& e=g[cur.dst][i];
			if(dist[e.dst]<=e.weight) continue;
			pq.push(Edge(e.src,e.dst,cur.weight+e.weight));
		}
	}
}

int main()
{
	for(int n,m;scanf("%d%d",&n,&m),n|m;){
		Graph g1(m),g2(m);
		for(int i=0;i<n;i++){
			int a,b,cost,time; scanf("%d%d%d%d",&a,&b,&cost,&time);
			a--; b--;
			g1[a].push_back(Edge(a,b,cost));
			g1[b].push_back(Edge(b,a,cost));
			g2[a].push_back(Edge(a,b,time));
			g2[b].push_back(Edge(b,a,time));
		}
		vector<vector<int> > w1(m,vector<int>(m,INFTY)),w2=w1;
		for(int i=0;i<m;i++){
			dijkstra(g1,i,w1[i]);
			dijkstra(g2,i,w2[i]);
		}
		int k; scanf("%d",&k);
		for(int i=0;i<k;i++){
			int p,q,r; scanf("%d%d%d",&p,&q,&r);
			if(r==0)
				printf("%d\n",w1[p-1][q-1]);
			else
				printf("%d\n",w2[p-1][q-1]);
		}
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)
#define MIN(a, b) ((a)<(b)?(a):(b))
#define INF 10e6
using namespace std;
 /*temp*/

int main(){
    int n, m, l;
    while(cin>>m>>n,n){
        ll cost[n][n],time[n][n];
        // set table
        REP(i,n){
            REP(j,n){
                cost[i][j]=time[i][j]=INF;
            }
            cost[i][i]=time[i][i]=0;
        }
        // read
        REP(i,n){
            int a,b,c,t;
            cin>>a>>b>>c>>t;
            a--;
            b--;
            cost[a][b]=cost[b][a]=MIN(c,cost[a][b]);
            time[a][b]=time[b][a]=MIN(t,time[a][b]);
        }
        // Warshall???Floyd Algorithm
        REP(a,n){
            REP(b,n){
                REP(k,n){
                    cost[a][b]=MIN(cost[a][b],cost[a][k]+cost[k][b]);
                    time[a][b]=MIN(time[a][b],time[a][k]+time[k][b]);
                }
            }
        }
        // 
        int q;
        cin>>q;
        REP(i,q){
            int s,d,type;
            cin>>s>>d>>type;
            s--;
            d--;
            cout<<type==0?cost[s][d]:time[s][d]<<'\n';
        }
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int d[2][101][101];
	int n,m;
	while(cin>>n>>m,n,m){
	for(int i=1;i<=m;i++)for(int j=i;j<=m;j++)for(int k=0;k<2;k++)d[k][i][j]=999999999;
	for(int i=0;i<n;i++){
		int a,b,c,t;
		cin>>a>>b>>c>>t;
		d[0][a][b]=d[0][b][a]=c;
		d[1][a][b]=d[1][b][a]=t;
	}
	for(int k=1;k<=m;k++){
		for(int i=1;i<=m;i++){
			for(int j=1;j<=m;j++){
				d[0][i][j]=min(d[0][i][j],d[0][i][k]+d[0][k][j]);
				d[1][i][j]=min(d[1][i][j],d[1][i][k]+d[1][k][j]);
			}
		}
	}
	int kl;
	cin>>kl;
	for(int i=0;i<kl;i++){
		int p,q,r;
		cin>>p>>q>>r;
		cout<<d[r][p][q]<<endl;
	}
	}
}