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<map>
#include<queue>
#include<vector>
using namespace std;
#define fr first
#define sc second
typedef pair<int,int>Pi;
struct edge{
  int to,element/*時間もしくは金額*/;
};
typedef vector< vector< edge > > Graph;

int dijkstr(Graph& info, int s, int g){
  vector<int> min_cost;
  priority_queue<Pi, vector<Pi>, greater<Pi> >que;
  min_cost.resize(info.size(),-1);
  que.push( Pi(0,s) ); //firstが時間(金額) , secondが場所
  min_cost[s]=0;
  while(!que.empty()){
    Pi p = que.top();
    que.pop();
    if(p.sc == g) return p.fr; //ゴールに着いたら
    if(p.fr > min_cost[p.sc]) continue; //現在地が前にきたときの最小より大きかった場合continue
    for(int i=0;i<info[p.sc].size();i++){ //現在地からの道の数だけ回る
      edge& e = info[p.sc][i];
      if(min_cost[e.to] == -1 || p.fr + e.element < min_cost[e.to]){ //次の場所はまだコストを調べていない || 次の場所の最小値よりも移動後のコストが小さい場合。
	min_cost[e.to] = p.fr + e.element;
	que.push( Pi(min_cost[e.to] , e.to));
      }
    }
  }
  return min_cost[g];
}

int main(){
  int n,m;
  while(1){
    cin >> n >> m;
    if(n==0 && m==0) break;
    Graph info;
    info.resize(n);
    
    int a[3010],b[3010],COST[3010],TIME[3010];
    for(int i=0;i<n;i++){
      cin >> a[i] >> b[i] >> COST[i] >> TIME[i];
      //info[a].push_back( edge{b, COST, TIME} );
      //info[b].push_back( edge{a, COST, TIME} );
    }
    
    int k,p,q,r;
    cin >> k;
    for(int i=0;i<k;i++){
      cin >> p >> q >> r;
      
      if(r==0){ //最小金額
	for(int i=0;i<n;i++){
	  info[a[i]].push_back( (edge){b[i], COST[i]} );
	  info[b[i]].push_back( (edge){a[i], COST[i]} );
	}
      }
      else if(r==1){ //最短時間
	for(int i=0;i<n;i++){
	  info[a[i]].push_back( (edge){b[i], TIME[i]} );
	  info[b[i]].push_back( (edge){a[i], TIME[i]} );
	}
      }
      cout << dijkstr(info,p,q) << endl;
    }
  }
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class Edge
{
public:
    int to, cost;
    Edge(){};
    Edge(int to0, int cost0){to = to0; cost = cost0;}
};

void shortestPath(const vector<vector<Edge> >& edges, int start, vector<int>& dist)
{
    dist.assign(edges.size(), INT_MAX);
    dist[start] = 0;
    priority_queue<pair<int,int> ,vector<pair<int,int> >, greater<pair<int,int> > > q;
    q.push(make_pair(0, start));

    while(!q.empty()){
        pair<int, int> p = q.top();
        q.pop();
        int v = p.second;
        if(dist[v] < p.first)
            continue;
        for(unsigned i=0; i<edges[v].size(); ++i){
            Edge e = edges[v][i];
            if(dist[v] + e.cost < dist[e.to]){
                dist[e.to] = dist[v] + e.cost;
                q.push(make_pair(dist[e.to], e.to));
            }
        }
    }

    replace(dist.begin(), dist.end(), INT_MAX, -1);
}

void shortestPath(const vector<vector<Edge> >& edges, vector<vector<int> >& dist)
{
    dist.resize(edges.size());
    for(unsigned i=0; i<edges.size(); ++i)
        shortestPath(edges, i, dist[i]);
}

int main()
{
    for(;;){
        int n, m;
        cin >> n >> m;
        if(n == 0)
            return 0;

        vector<vector<Edge> > cost(m+1);
        vector<vector<Edge> > time(m+1);
        for(int i=0; i<n; ++i){
            int a, b, x, y;
            cin >> a >> b >> x >> y;
            cost[a].push_back(Edge(b, x));
            cost[b].push_back(Edge(a, x));
            time[a].push_back(Edge(b, y));
            time[b].push_back(Edge(a, y));
        }

        vector<vector<int> > minCost, minTime;
        shortestPath(cost, minCost);
        shortestPath(time, minTime);

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