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

using namespace std;

#define fst first
#define snd second
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
typedef pair<int,int> pii;

int n,m;
pii cost[110][110];

const int inf=1<<27;

int main(void){
	while(cin >> n >> m,n|m){
		rep(i,110)rep(j,110) cost[i][j].fst=cost[i][j].snd=((i==j)?0:inf);
		rep(i,n){
			int f,t;
			cin >> f >> t;
			f--;t--;
			int cos,tim;
			cin >> cos >> tim;
			cost[f][t].fst=cost[t][f].fst=cos; 
			cost[f][t].snd=cost[t][f].snd=tim;
		}

		rep(k,m)rep(i,m)rep(j,m){
			cost[i][j].fst=min(cost[i][j].fst,cost[i][k].fst+cost[k][j].fst);
			cost[i][j].snd=min(cost[i][j].snd,cost[i][k].snd+cost[k][j].snd);
		}
		int q;
		cin >> q;
		rep(i,q){
			int f,t,c;
			cin >> f >> t >> c;
			f--;t--;
			if(c)
				cout << cost[f][t].snd << endl;
			else
				cout << cost[f][t].fst << endl;
		}
	}
	return 0;
}

C++: #include <stdio.h>
#include <limits.h>
 
int n,m;
 
int Selection(int start,int goal,int Graph[101][101])
{
    int i;
    int min,minp = start;
    int Station[101];
    int Decided[101] = {0,};
 
    for(i=0;i<101;i++)
    {
        Station[i] = INT_MAX;
    }
 
    Station[start] = 0;
 
    while(Decided[goal] == 0)
    {
        for(i=0;i<m+1;i++)
        {
            if(Graph[minp][i] == -1) continue;
 
            if(Station[i] > Graph[minp][i] + Station[minp])
            {
                Station[i] = Graph[minp][i] + Station[minp];
            }
        }
 
        min = INT_MAX;
        for(i=0;i<m+1;i++)
        {
            if(min > Station[i] && Decided[i] != 1)
            {
                min = Station[i];
                minp = i;
            }
        }
        Decided[minp] = 1;
    }
 
    return Station[goal];
}
 
int main()
{
    int Gcost[101][101];
    int Gtime[101][101];
 
    int i,j;
    int a,b,time,cost;
    int k,start,goal,r;
    int ans;
 
    while(1)
    {
        for(i=0;i<101;i++)
        {
            for(j=0;j<101;j++)
            {
                Gcost[i][j] = -1;
                Gtime[i][j] = -1;
            }
        }
 
        scanf("%d%d",&n,&m);
        if(n == 0 && m==0) break;
 
        for(i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&a,&b,&cost,&time);
            Gcost[a][b] = cost;
            Gcost[b][a] = cost;
            Gtime[a][b] = time;
            Gtime[b][a] = time;
        }
 
        scanf("%d",&k);
        for(i=0;i<k;i++)
        {
            scanf("%d%d%d",&start,&goal,&r);
            if(r==0) ans = Selection(start,goal,Gcost);
            else if(r==1) ans = Selection(start,goal,Gtime);
            printf("%d\n",ans);
        }
    }
 
    return 0;
}