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 <vector>
#include <utility>
#include <queue>
#define INF 100000000
using namespace std;
typedef pair<int, int> P;
struct line {
  int to,time,cost;
};

vector<line> L[100];
int costv[100];
int timev[100];

void mincost(int from,int to) {
  priority_queue<P, vector<P>, greater<P> > que;
  for(int i=0;i<100;i++) {
    costv[i] = INF;
  }
  costv[from] = 0;
  que.push(P(0,from));
  while(!que.empty()) {
    P p = que.top(); que.pop();
    int v = p.second;
    if(costv[v] < p.first) continue;
    for(unsigned int i=0;i < L[v].size();i++) {
      line l = L[v][i];
      if(costv[l.to] > costv[v] + l.cost) {
	costv[l.to] = costv[v] + l.cost;
	que.push(P(costv[l.to], l.to));
      }
    }
  }
  cout << costv[to] << endl;
}

void mintime(int from,int to) {
  priority_queue<P, vector<P>, greater<P> > que;
  for(int i=0;i<100;i++) {
    timev[i] = INF;
  }
  timev[from] = 0;
  que.push(P(0,from));
  while(!que.empty()) {
    P p = que.top(); que.pop();
    int v = p.second;
    if(timev[v] < p.first) continue;
    for(unsigned int i=0;i < L[v].size();i++) {
      line l = L[v][i];
      if(timev[l.to] > timev[v] + l.time) {
	timev[l.to] = timev[v] + l.time;
	que.push(P(timev[l.to], l.to));
      }
    }
  }
  cout << timev[to] << endl;
}

int main() {
  int n,m,k;
  while(cin >> n >> m) {
    if(n==0&&m==0)break;
    for(int i=0;i<100;i++) {
      L[i].clear();
    }
    for(int i=0;i<n;i++) { //input line
      line temp;
      int f,t,ti,co;
      cin >> f >> t >> co >> ti;
      temp.to = t;
      temp.time = ti;
      temp.cost = co;
      L[f].push_back(temp);
      temp.to = f;
      L[t].push_back(temp);
    }

    cin >> k;
    for(int i=0;i<k;i++) {
      int p,q,r;
      cin >> p >> q >> r;
      if(r == 0)
	mincost(p,q);
      else
	mintime(p,q);
    }
  }
}

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 = a; x.cost[0] = cost; x.cost[1] = tim;
			//mapp[b].push_back(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);
	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];
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
#define INF 100000000

int n,m,k,p,q,r;
int d[2][100][100];

int main(){
	while(scanf("%d%d", &n, &m)){
		if(n == 0 && m == 0) break;
		for(int i = 0; i < 2; i++){
			for(int j = 0; j < 100; j++){
				for(int k = 0; k < 100; k++) d[i][j][k] = INF;
			}
		}
		for(int i = 0; i < 2; i++){
			for(int j = 0; j < 100; j++) d[i][j][j] = 0;
		}
			
		for(int i = 0; i < n; i++){
			int a,b,c[2];
			scanf("%d%d%d%d", &a, &b, &c[0], &c[1]);
			a--; b--;
			for(int j = 0; j < 2; j++){
				d[j][a][b] = c[j];
				d[j][b][a] = c[j];
			}
		}
		for(int u = 0; u < 2; u++){
			for(int k = 0; k < m; k++){
				for(int i = 0; i < m; i++){
					for(int j = 0; j < m; j++){
						d[u][i][j] = min(d[u][i][j],d[u][i][k]+d[u][k][j]);
					}
				}
			}
		}
		scanf("%d", &k);
		for(int i = 0; i < k; i++){
			scanf("%d%d%d", &p, &q, &r);
			p--; q--;
			printf("%d\n", d[r][p][q]); 
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int INF = 1 << 28;
int n, m;
int a[3010], b[3010], c[3010], t[3010];
int dis0[210][210], dis1[210][210];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  while (cin >> n >> m, n) {
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i] >> c[i] >> t[i];
      a[i]--, b[i]--;
    }
    fill_n((int*)dis0, 210 * 210, INF);
    fill_n((int*)dis1, 210 * 210, INF);
    for (int i = 0; i < m; i++) {
      dis0[i][i] = dis1[i][i] = 0;
    }
    for (int i = 0; i < n; i++) {
      dis0[a[i]][b[i]] = dis0[b[i]][a[i]] = c[i];
      dis1[a[i]][b[i]] = dis1[b[i]][a[i]] = t[i];
    }
    for (int k = 0; k < m; k++) {
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          dis0[i][j] = min(dis0[i][j], dis0[i][k] + dis0[k][j]);
          dis1[i][j] = min(dis1[i][j], dis1[i][k] + dis1[k][j]);
        }
      }
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      p--, q--;
      if (r == 0) cout << dis0[p][q] << endl;
      else cout << dis1[p][q] << endl;
    }
  }  

  return 0;
}