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<iostream>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;

const int INF=1e9;

//struct edge{int to,cost,time;};
struct edge{int cost,time;};

int main(){
	int n,m;
	while(cin >> n >> m, n || m){

		//vector<edge> es[n];
		edge es[m][m];
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++) es[i][j] = (edge){INF,INF};
		}

		int a,b,cost,time;
		for(int i=0;i<n;i++){
			cin >> a >> b >> cost >> time;
		//	es[a].emplace_back((edge){b,cost,time});
			es[a-1][b-1] = (edge){cost,time};
			es[b-1][a-1] = es[a-1][b-1];
		}

		int d_cost[m][m];
		int d_time[m][m];
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				d_cost[i][j] = es[i][j].cost;
				d_time[i][j] = es[i][j].time;
			}
		}

		for(int k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					d_cost[i][j] = min(d_cost[i][j],d_cost[i][k] + d_cost[k][j]);
					d_time[i][j] = min(d_time[i][j],d_time[i][k] + d_time[k][j]);
				}
			}
		}

		int k;
		cin >> k;
		for(int n=0;n<k;n++){
			int p,q,r;
			cin >> p >> q >> r;

			if(r==0) cout << d_cost[p-1][q-1] << "\n";
			else	cout << d_time[p-1][q-1] << "\n";
		}
	}

	return 0;
}

C++: #include <iostream>
#include <cstring>

using namespace std;

const int INF=100000;
int cost[101][101];
int tim[101][101];

int main(void){
	int n,m,a,b,c;
	while(1){
		for(a=0;a<=100;a++){
			for(b=0;b<=100;b++){
				cost[a][b]=INF;
				tim[a][b]=INF;
				if(a==b)cost[a][b]=0,tim[a][b]=0;
			}
		}
		cin >> n >> m;
		if(n==0 && m==0)break;
		for(a=0;a<n;a++){
			int f,t,co,ti;
			scanf("%d%d%d%d",&f,&t,&co,&ti);
			if(cost[f][t]>co)cost[f][t]=co,cost[t][f]=co;
			if(tim[f][t]>ti)tim[f][t]=ti,tim[t][f]=ti;
		}
		for(a=1;a<=m;a++){
			for(b=1;b<=m;b++){
				for(c=1;c<=m;c++){
					cost[a][b]=min(cost[a][b],cost[a][c]+cost[c][b]);
					tim[a][b]=min(tim[a][b],tim[a][c]+tim[c][b]);
				}
			}
		}
		cin >> n;
		for(a=0;a<n;a++){
			int d,f,t;
			scanf("%d%d%d",&f,&t,&d);
			if(d==0)printf("%d\n",cost[f][t]);
			if(d==1)printf("%d\n",tim[f][t]);
		}
	}
    return 0;
}

C++: #include<stdio.h>
#include<string.h>
#define min(x,y) (x<y)?(x):(y)
int main(){
	int n,m,k,p,q,r,c[105][105],t[105][105],v[105],u[105],i,a,b,f,x,y;
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(c, 0,sizeof(c));
		memset(t, 0,sizeof(t));
		for(i=0;i<n;i++){
			scanf("%d%d",&p,&q);
			scanf("%d%d",&c[p][q],&t[p][q]);
			c[q][p]=c[p][q];t[q][p]=t[p][q];
		}
		scanf("%d",&k);
		for(i=0;i<k;i++){
			scanf("%d%d%d",&p,&q,&r);
			//memset(v,-1,sizeof(v));
			memset(u,0,sizeof(u));
			for(a=0;a<=m;a++)v[a]=100000000;
			v[p]=0;f=1;
			while(1){
				x=-1;
				for(a=1;a<=m;a++){
					if(u[a]==0 && (x==-1 || v[a]<v[x]))x=a;
				}
				if(x==-1)break;
				u[x]=1;
				for(a=1;a<=m;a++){
					if(r==0 && c[x][a]!=0)v[a]=min(v[a],v[x]+c[x][a]);
					if(r==1 && t[x][a]!=0)v[a]=min(v[a],v[x]+t[x][a]);
				}
			}
			printf("%d\n",v[q]);
		}
	}
	return 0;
}

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

constexpr int INF = 1e9;

int main() {
    int n, m;
    while(cin >> n >> m, n) {
        vector<vector<vector<int>>> d(2, vector<vector<int>>(m, vector<int>(m, INF)));
        for(int i=0; i<m; ++i) {
            d[0][i][i] = d[1][i][i] = 0;
        }
        for(int i=0; i<n; ++i) {
            int a, b, cost, time;
            cin >> a >> b >> cost >> time;
            a--; b--;
            d[0][a][b] = d[0][b][a] = cost;
            d[1][a][b] = d[1][b][a] = time;
        }

        for(int k=0; k<m; ++k) {
            for(int i=0; i<m; ++i) {
                for(int j=0; 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 k;
        cin >> k;
        for(int i=0; i<k; ++i) {
            int p, q, r;
            cin >> p >> q >> r;
            p--; q--;
            cout << d[r][p][q] << endl;
        }
    }
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef pair<int,int> P;
typedef struct{
	int next;
	int cost;
	int time;
}edge;

int n,m;
vector<edge> mapp[101];
int bfs_c(int now,int goal);
int bfs_t(int now,int goal);

int main(){
	while(1){
		cin >> n >> m;
		if(n == 0 && m == 0) break;
		for(int i=0;i<n+1;i++){
			mapp[i].clear();
		}
		for(int i=0;i<n;i++){
			int a,b,cost,time;
			cin >> a >> b >> cost >> time;
			edge x;
			x.next = b; x.cost = cost; x.time = time;
			mapp[a].push_back(x);
		}
		int k;
		cin >> k;
		int ans;
		for(int i=0;i<k;i++){
			int p,q,r;
			cin >> p >> q >> r;
			if(r == 0) ans = bfs_c(p,q);
			else if(r == 1) ans = bfs_t(p,q);
			cout << ans << endl;
		}
	}
}

int bfs_c(int now,int goal){
	priority_queue<P, vector<P>,greater<P> > que;
	que.push(P(now,0));
	while(!que.empty()){
		P p = que.top(); que.pop();
		int no = p.first;
		int co = p.second;
		if(no == goal) return co;
		int len=mapp[no].size();
		for(int i=0;i<len;i++){
			edge x = mapp[no][i];
			que.push(P(x.next,co+x.cost));
		}
	}
	return 0;
}

int bfs_t(int now,int goal){
	priority_queue<P, vector<P>,greater<P> > que;
	que.push(P(now,0));
	while(!que.empty()){
		P p = que.top(); que.pop();
		int no = p.first;
		int co = p.second;
		if(no == goal) return co;
		int len=mapp[no].size();
		for(int i=0;i<len;i++){
			edge x = mapp[no][i];
			que.push(P(x.next,co+x.time));
		}
	}
	return 0;
}