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
			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++: #define NODE 0
#define WEIGHT 1
#define COST_IDX 0
#define TIME_IDX 1
#define P_IDX 0
#define Q_IDX 1
#define R_IDX 2
#include <iostream>
#include <cstring>
using namespace std;

int n, m, k;
int weights[101][101][2];
int min_w[101][101][2];
int requests[200][3];

bool read_input() {
  cin >> n >> m;
  if (n*m == 0) 
    return false;
  memset(weights, 0, sizeof(int)*101*101*2);
  memset(min_w, 0, sizeof(int)*101*101*2);
  for (int i = 0; i < n; i++) {
    int a, b, c, t;
    cin >> a >> b >> c >> t;
    weights[a][b][COST_IDX] = weights[b][a][COST_IDX] = c;
    weights[a][b][TIME_IDX] = weights[b][a][TIME_IDX] = t;
  }
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> requests[i][P_IDX] >> requests[i][Q_IDX] >> requests[i][R_IDX];
  }
  return true;
}

int resolve_request(int i_th) {
  int p, q, r;
  p = requests[i_th][P_IDX];
  q = requests[i_th][Q_IDX];
  r = requests[i_th][R_IDX];

  if (min_w[p][q][r])
    return min_w[p][q][r];

  int weight_sum_map[101];
  int in_queue[101] = {}; // set all to 0
  memset(weight_sum_map, -1, sizeof(int)*101);
  
  /* starting node has weight_sum of 0 */
  weight_sum_map[p] = 0;
  in_queue[p] = 1;
  int current_node = p; // first in queue is p

  while (current_node != q) {
    int weight_sum = weight_sum_map[current_node];
    in_queue[current_node] = 0;
    min_w[p][current_node][r] = weight_sum;
    
    for (int i = 1; i < 101; i++) {
      if (!weights[current_node][i][r]) 
	continue;
      int adj_node = i;
      int w = weights[current_node][adj_node][r];
      int new_weight_sum =  weight_sum + w;
      if (weight_sum_map[adj_node] == -1 || new_weight_sum < weight_sum_map[adj_node]) {
	weight_sum_map[adj_node] = new_weight_sum;
	in_queue[adj_node] = 1;
      }
    }
    
    int min_sum = -1;
    for (int i = 1; i < 101; i++) {
      if (in_queue[i] && (min_sum == -1 || weight_sum_map[i] < min_sum)) {
	min_sum = weight_sum_map[i];
	current_node = i;
      }
    }
  }
  return weight_sum_map[q];
}

void solve() {
  for (int i = 0; i < k; i++)
    cout << resolve_request(i) << endl;
}

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  while(read_input()) {
    solve();
  }
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <functional>

using namespace std;

//FILE* in = freopen("./B","r",stdin);

typedef long long LL;
#define REP(i,a,b)for(LL i=(LL)a;i<(LL)b;i++)


#define MAX_V 3100
#define INF 1000000

struct edge{int to,cost,time;edge(int t,int c,int h):to(t),cost(c),time(h){}};
typedef pair<int, int> P;

int V=MAX_V;
vector<edge> G[MAX_V];
int d[MAX_V];

int dijkstra(int s,int g,bool flag){
	priority_queue<P, vector<P>, greater<P> > que;
	fill(d, d+V, INF);
	d[s] = 0;
	que.push(P(0,s));

	while(!que.empty()){
		P p=que.top(); que.pop();
		int v = p.second;
		if(v==g)return d[v];
		if( d[v] < p.first ) continue;
		for(int i = 0;i<G[v].size();i++){
			edge e = G[v][i];
			if(flag==0){
			if(d[e.to] > d[v] + e.cost ){
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to],e.to));
			}
			}else{
				if(d[e.to] > d[v] + e.time ){
				d[e.to] = d[v] + e.time;
				que.push(P(d[e.to],e.to));
			}

			}
		}
	}
	return -1;
}

int main(void){

	while(1){

		REP(i,0,MAX_V)
			G[i].clear();

		int N,M;
		int cost,time,a,b;
		int K,start,goal,R;

		cin >> N>>M;

		if(!N&&!M)break;

		REP(i,0,N){
			cin >> a>>b>>cost>>time;
			G[a].push_back(edge(b,cost,time));
			G[b].push_back(edge(a,cost,time));
		}

		cin >> K;

		REP(i,0,K){
			cin >> start>>goal>>R;
			cout << dijkstra(start,goal,R)<<endl;
		}

	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#define INF 1000000000
using namespace std;
int mp[2][101][101];
int n,m;

int visit(int p,int q,int r) {


  bool flg[101] = {};
  long long dis[101];
  for(int i=1;i<=m;i++) dis[i] = INF;

  dis[p] = 0;
 
  while(1) {
    int mindis = INF;
    int ima;
    for(int i=1;i<=m;i++) 
      if(mindis > dis[i] && flg[i] == 0) {
	mindis = dis[i];
	ima = i;
      }

    flg[ima] = 1;
    if(ima == q) return dis[ima];

    for(int i=1;i<=m;i++) 
      if(dis[i] > mp[r][ima][i]+dis[ima]) 
	dis[i] = dis[ima] + mp[r][ima][i];
    
  }


}


int main (){

  while(1) {
    cin >> n >> m;
    if(n == 0 && m == 0) break;
  
  for(int k=0;k<2;k++) 
    for(int i=0;i<=m;i++) 
      for(int j=0;j<=m;j++)
	mp[k][i][j] = INF;

	
  for(int i=0;i<n;i++){
  int a , b , cost,time;
  cin >> a >> b >> cost >> time;
  mp[0][a][b] = min(mp[0][a][b] ,cost);
  mp[0][b][a] = min(mp[0][b][a] ,cost);
  mp[1][a][b] = min(mp[1][a][b] ,time);
  mp[1][b][a] = min(mp[1][b][a] ,time);
  }

  int qu;
  cin >> qu;
  int p,q,r;

  for(int i=0;i<qu;i++) {
    cin >> p >> q >> r;

    int ans = visit(p,q,r);
    cout << ans << endl;
  }
  }
  
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int INF=100000;

int main(void){
  int time[101][101];
  int cost[101][101];
  int a,b,c,d;
  int n,m;
  int p,q,r;
  int k;
  int i,j,l;
  while(true){
    cin>>n>>m;
    if(n==0&&m==0)return(0);
    for(i=0;i<101;i++){
      for(j=0;j<101;j++){
	time[i][j]=INF;
	cost[i][j]=INF;
      }
    }

    for(i=0;i<n;i++){
      cin>>a>>b>>c>>d;
      cost[a][b]=cost[b][a]=c;
      time[a][b]=time[b][a]=d;
    }
    for(i=1;i<m+1;i++){
      for(j=1;j<m+1;j++){
	for(l=1;l<m+1;l++){
	  cost[j][l]=min(cost[j][l],cost[j][i]+cost[i][l]);
	  time[j][l]=min(time[j][l],time[j][i]+time[i][l]);
	}
      }
    }

    cin>>k;
    for(i=0;i<k;i++){
      cin>>p>>q>>r;
      if(r==0){
	cout<<cost[p][q]<<endl;
      }
      else if(r==1){
	cout<<time[p][q]<<endl;;
      }
    }
  }
}