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>
using namespace std;

const int INF = 1000000000;

int main()
{
  int count=0;
	while(count!=50){
		int n, m;
		cin >> n >> m;

		if(n==0 && m==0)
			break;

		 int C[100][100];
		 int T[100][100];
	   for(int i=0; i<m; i++){
		   for(int j=0; j<m; j++){
			   if(i==j){
	         C[i][j] = T[i][j] = 0;
		     }else{
			     C[i][j] = T[i][j] = INF;
		     }
		   }
     }

	   for(int i=0; i<n; i++){
		   int a, b, c, t;
		   cin >> a >> b >> c >> t;

	     C[a-1][b-1] = C[b-1][a-1] = c;
		   T[a-1][b-1] = T[b-1][a-1] = t;
    }

    int k;
    cin>>k;
    for(int i=0;i<k;i++){
      int p, q, r;
      int a=0;
      cin>>p>>q>>r;
      int d[m][m];
      if(r==0){
        for(int i=0;i<m;i++){
          for(int j=0;j<m;j++){
            d[i][j]=C[i][j];
          }
        }
        for(int k=0;k<m;k++){
          for(int i=0;i<m;i++){
            for(int j=0;j<m;j++){
              if(d[i][j]>d[i][k]+d[k][j]){
                d[i][j]=d[i][k]+d[k][j];
              }
            }
          }
        }
        a=d[p-1][q-1];
        cout<<a<<endl;
      }
      if(r==1){
        for(int i=0;i<m;i++){
          for(int j=0;j<m;j++){
            d[i][j]=T[i][j];
          }
        }
        for(int k=0;k<m;k++){
          for(int i=0;i<m;i++){
            for(int j=0;j<m;j++){
              if(d[i][j]>d[i][k]+d[k][j]){
                d[i][j]=d[i][k]+d[k][j];
              }
            }
          }
        }
        a=d[p-1][q-1];
        cout<<a<<endl;
      }
    }
    count++;
	}

	return 0;
}

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

class Node
{
public:
	vector<int> to,tm,my;
};

int T;
Node node[301];


class State
{
public:
	int p, tm, my;
	State(int p, int tm, int my)
	:p(p),tm(tm),my(my)
	{}
	
	bool operator<(const State& s) const
	{
		if(T==0) return my>s.my;
		return tm>s.tm;
	}
};

int Dijkstra(int s, int d)
{
	priority_queue<State> q;
	q.push(State(s,0,0));
	
	bool v[301]={0};
	while(!q.empty())
	{
		State t=q.top(); q.pop();
		if(v[t.p]) continue;
		v[t.p]=1;
		if(t.p==d)
		{
			if(T==0) return t.my;
			else return t.tm;
		}
		
		for(int i=0; i<node[t.p].to.size(); i++)
		{
			int next=node[t.p].to[i];
			if(v[next]) continue;
			q.push(State(next, t.tm+node[t.p].tm[i], t.my+node[t.p].my[i]));
		}
	}
	
	return 0;
}

int main()
{
	int M,N;
	while(cin >> M >> N, (M||N))
	{
		for(int i=0; i<301; i++)
		{
			 node[i].to.clear();
			 node[i].tm.clear();
			 node[i].my.clear();
		}
		while(M--)
		{
			int s,d,t,m;
			cin >> s >> d >> m >> t;
			node[s].to.push_back(d);
			node[d].to.push_back(s);
			node[s].tm.push_back(t);
			node[d].tm.push_back(t);
			node[s].my.push_back(m);
			node[d].my.push_back(m);
		}
		
		int Q;
		cin >> Q;
		while(Q--)
		{
			int s,d;
			cin >> s >> d >> T;
			cout << Dijkstra(s,d) << endl;
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <string>
#include <sstream>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define rep2(i, n) for(int i = 1; i <= (int)n; ++i)
#define repf(i, f, l) for(int i = f; i < l; ++i)
#define repit(it, t) for(__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++)

#define pb push_back
#define lb lower_bound
#define mp make_pair
#define ite iterator
#define ul unsigned long
#define ull unsigned long long
#define INF 1000000007
#define MOD 1000000007
#define EPS 1.0e-6
#define fs first
#define sd second

template<typename T>
bool elem(set<T> st, T val){
	return (st.find(val) != st.end());
}

template<typename T>
void printv(vector<T> vt){
    if(vt.size() == 0) return;
    rep(i, vt.size() - 1)
        cout << vt[i] << " ";
    cout << vt[vt.size() - 1] << endl;
}

typedef vector<int> vint;
typedef vector<ul> vul;
typedef vector<ull> vull;
typedef pair<int, int> pii;

int edge[2][301][301];
bool vst[301];
int main(void){
    ios::sync_with_stdio(false);
    while(true){
        int n, m;
        cin >> n >> m;
        if(n == 0 && m == 0) break;
        rep(ii, 2) rep(i, m) rep(j, m) edge[ii][i][j] = -1;
        rep(i, n){
            int a, b, ed[2];
            cin >> a >> b >> ed[0] >> ed[1];
            a--; b--;
            rep(j, 2){
                edge[j][a][b] = ed[j];
                edge[j][b][a] = ed[j];
            }
        }
        int k;
        cin >> k;
        while(k-- > 0){
            rep(i, n) vst[i] = false;
            int p, q, r;
            cin >> p >> q >> r;
            p--; q--;
            set<pii> st;
            st.insert(mp(0, p));
            vst[p]++;

            int mn = 1000000009;
            while(!st.empty()){
                int  cs = (*st.begin()).fs;
                int cur = (*st.begin()).sd;
                st.erase(st.begin());
                if(cur == q){
                    mn = cs;
                    break;
                }
                vst[cur]++;
                rep(i, n)
                    if(edge[r][cur][i] > 0 && !vst[i])
                        st.insert(mp(cs + edge[r][cur][i], i));
            }
            cout << mn << "\n";
        }
    }
    return 0;
}