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>
#include <vector>
#include <queue>
#include <utility>
using namespace std;

#define INF 1e9

// ??????????????????
// s: ????§??????????, adjlist: ??£??\????????? (first: ????????? second: ?????????)
vector<int> dijk(int s, vector<vector<pair<int, int> > > adjlist){
  int v = adjlist.size();
  priority_queue<pair<int, int > > wait;
  vector<int> result(v, 1e9);
  int nowpoint, nowcost;

  // ??????????????°????????????
  result[s] = 0;
  wait.push(make_pair(0, s));

  // ????????????????????¬???
  while (!wait.empty()) {
    // ??????
    nowpoint = wait.top().second;
    nowcost = -wait.top().first;
    wait.pop();
    // ????????????????????¢?´¢??????????????????????????§????????´?????????????£???°???
    if (result[nowpoint] < nowcost) { continue; }
    
    // ?????????????????¨??£??\??????????????????????????????????????????
    for (int i = 0; i < adjlist[nowpoint].size(); i++) {
      int nextpoint = adjlist[nowpoint][i].second;
      int nextcost = nowcost + adjlist[nowpoint][i].first;
      // ??¢?´¢????????????????????°?????§???????????§????????°??????????????´??°??????????????????????????\??????????´?
      if(result[nextpoint] > nextcost){
        result[nextpoint] = nextcost;
        wait.push(make_pair(-nextcost, nextpoint));
      }
    }
  }

  // ?????????: ?????????
  return result;
}


int main(void){
  while(true){
    int n, m;
    cin >> n >> m;
    if(n == 0 && m == 0){ break; }
    
    int a, b, cost, time;
    vector<vector<pair<int, int> > > adjlist_cost(m);
    vector<vector<pair<int, int> > > adjlist_time(m);
    for(int i = 0; i < n; i++){
      cin >> a >> b >> cost >> time;
      --a;
      --b;
      adjlist_cost[a].push_back(make_pair(cost, b));
      adjlist_cost[b].push_back(make_pair(cost, a));
      adjlist_time[a].push_back(make_pair(time, b));
      adjlist_time[b].push_back(make_pair(time, a));
    }

    int k;
    cin >> k;

    int p, q, r;
    vector<int> result;
    for(int i = 0; i < k; i++){
      cin >> p >> q >> r;
      --p;
      --q;
      result = (r == 0) ? dijk(p, adjlist_cost) : dijk(p, adjlist_time);
      cout << result[q] << endl;
    }
  }

  return 0;
}

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

int cost[101][101];
int gtime[101][101];

struct tSta{
	int val, sta;
	tSta( int s ){
		val = 0;
		sta = s;
	}
};

bool operator<( const tSta& l, const tSta& r ){
	return l.val > r.val;
}

int main(){
	priority_queue<tSta> que;
	int n, m, k;
	while( cin >> n >> m, n||m ){
		memset( cost, 0, sizeof(cost) );
		memset( gtime, 0, sizeof(gtime) );
		for( int i=0; i<n; i++ ){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[a][b] = cost[b][a] = c;
			gtime[a][b] = gtime[b][a] = t;
		}
		cin >> k;
		for( int i=0; i<k; i++ ){
			int p, q, r;
			cin >> p >> q >> r;
			while( !que.empty() )	que.pop();
			que.push( tSta(p) );
			while( !que.empty() ){
				tSta ts = que.top();
				if( ts.sta == q )	break;
				que.pop();
				if( r==0 ){	// cost
					for( int j=1; j<=m; j++ ){
						if( cost[ts.sta][j] > 0 ){
							tSta next(j);
							next.val = ts.val + cost[ts.sta][j];
							que.push( next );
						}
					}
				}else if( r==1 ){	// time
					for( int j=1; j<=m; j++ ){
						if( gtime[ts.sta][j] > 0 ){
							tSta next(j);
							next.sta = j;
							next.val = ts.val + gtime[ts.sta][j];
							que.push( next );
						}
					}
				}
			}
			cout << que.top().val << endl;
		}
	}
	return 0;
}

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

int n,m;
vector<edge> mapp[111];
int bfs(int now,int goal,int k);

int main(){
	while(1){
		cin >> n >> m;
		if(n == 0 && m == 0) break;
		for(int i=0;i<m+1;i++){
			mapp[i].clear();
		}
		for(int i=0;i<n;i++){
			int a,b,cost,tim;
			cin >> a >> b >> cost >> tim;
			edge x;
			x.next = b; x.cost[0] = cost; x.cost[1] = tim;
			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(p,q,r);
			else if(r == 1) ans = bfs(p,q,r);
			cout << ans << endl;
		}
	}
}

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