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<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
using namespace std;
typedef long long int ll;

int main()
{
    int n,m;
	int a[1000],b[1000],cost[1000],time[1000];
	int k;
	int p,q,r;
	int cos[100][100];
	int tim[100][100];
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++){
		for(int j=0;j<m;j++){
			cos[i][j]=1000000;
			tim[i][j]=1000000;
		}
	}
	for(int i=0;i<n;i++){
		scanf("%d%d%d%d",&a[i],&b[i],&cost[i],&time[i]);
		cos[a[i]-1][b[i]-1]=cost[i];
		cos[b[i]-1][a[i]-1]=cost[i];
		tim[a[i]-1][b[i]-1]=time[i];
		tim[b[i]-1][a[i]-1]=time[i];
	}
	scanf("%d",&k);
	for(int i=0;i<k;i++){
		scanf("%d%d%d",&p,&q,&r);
		p--;
		q--;
		if(r==0){
			int d[110];
			d[101]=1000000;
			bool used[100];
			fill(used,used+m,false);
			d[p]=0;
			while(true){
				int v=101;
				for(int u=0;u<m;u++){
					if(!used[u]&&(v==101||d[u]<d[v]))v=u;
				}
				if(v==101)break;
				used[v]=true;
				for(int u=0;u<m;u++){
					d[u]=min(d[u],d[v]+cos[v][u]);
				}
			}
			printf("%d\n",d[q]);
		}
		else{
			int d[110];
			d[101]=1000000;
			bool used[100];
			d[p]=0;
			fill(used,used+m,false);
			while(true){
				int v=101;
				for(int u=0;u<m;u++){
					if(!used[u]&&(v==101||d[u]<d[v]))v=u;
				}
				if(v==101)break;
				used[v]=true;
				for(int u=0;u<m;u++){
					d[u]=min(d[u],d[v]+tim[v][u]);
				}
			}
			printf("%d\n",d[q]);
		}
	}
    return 0;
}

C++: #include <iostream>
#include <cstring>
#include <algorithm>
#define INF (1 << 25)
using namespace std;

int main(){
    int cost[128][128],time[128][128];
    int n,m,q,a,b,c,d,s,g,r;
    while(cin >> n >> m,n||m){
        for(int i=0;i<128;i++){
            for(int j=0;j<128;j++){
                cost[i][j] = INF; time[i][j] = INF;
            }
        }
        for(int i=0;i<n;i++){
            cin >> a >> b >> c >> d;
            cost[a][b] = c; cost[b][a] = c;
            time[a][b] = d; time[b][a] = d;
        }
        for(int i=1;i<=m;i++){
            for(int j=1;j<=m;j++){
                for(int k=1;k<=m;k++){
                    cost[j][k] = min(cost[j][k],cost[j][i] + cost[i][k]);
                    time[j][k] = min(time[j][k],time[j][i] + time[i][k]);
                }
            }
        }
        cin >> q;
        while(q--){
            cin >> s >> g >> r;
            cout << ((r == 0)? cost[s][g] : time[s][g]) << endl;
        }
    }
}

C++: //#include "bipartite-graph.hpp"
//#include <vector>
//using namespace std;
//int main(){
//	vector<vector<int> > G(4);
//	cout << bipartite_graph(G) << endl;
//}

// AOJ0200
#include <iostream>
#include <vector>
using namespace std;

struct edge {
    edge(){}
    edge(int to, int from, int cost, int time): to(to), from(from), cost(cost), time(time) {}
    int to, from, cost, time;
};

const int INF = 100000000;

int d[101];
edge es[7000];
int n,m;

// 頂点sから各頂点への最短経路を求める
void bellman_ford(int s, bool r){
    for (int i = 0; i < m; ++i) d[i]=INF;
    d[s]=0;
    while(1){
        bool update=false;
        for (int i = 0; i < n*2; ++i) {
            edge e=es[i];
            int cost;
            if(r) cost=e.time;
            else cost=e.cost;
            if(d[e.from]!=INF && d[e.to]>d[e.from]+cost){
                d[e.to]=d[e.from]+cost;
                update=true;
            }
        }
        if(!update) break;
    }
}

int main(){
    while(cin >> n >> m,n+m){
        for (int i = 0; i < n; ++i) {
            int to,from,cost,time;
            cin >> to>>from>>cost>>time;
            es[i]=edge(to-1,from-1,cost,time);
            es[n+i]=edge(from-1,to-1,cost,time);
        }
        int k; cin>>k;
        for (int i = 0; i < k; ++i) {
            int p,q,r;
            cin>>p>>q>>r;
            bellman_ford(p-1,r);
            cout<<d[q-1]<<endl;
        }
    }
}

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

#define rep(i, n) for(int i = 0;i < (int)n;i++)

int d[100][100];
int m_[100][100];

int main() {
    while(true) {
        int n, m;
        cin >> n >> m;
        if(!n && !m) break;

        rep(i, m) {
            rep(j, m) {
                d[i][j] = m_[i][j] = 1001;
            }
        }

        rep(i, n) {
            int a, b, cost, time;
            cin >> a >> b >> cost >> time;
            a--; b--;
            d[a][b] = d[b][a] = cost;
            m_[a][b] = m_[b][a] = time;
        }

        rep(i, m) {
            rep(j, m) {
                rep(k, m) {
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
                    m_[i][j] = min(m_[i][j], m_[i][k] + m_[k][j]);
                }
            }
        }

        int k;
        cin >> k;
        rep(i, k) {
            int p, q, r;
            cin >> p >> q >> r;
            p--; q--;
            cout << (r == 0 ? d[p][q] : m_[p][q]) << endl;
        }
    }

    return 0;
}