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<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<s;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<long long,long long> pii;
#define MAX 99999999
signed main(){
while(1){
	int n,m;
	cin>>n>>m;
    if(!(n+m))break;
	vector<vector<pii>>v(n+1,vector<pii>(n+1,pii(INF,INF)));
	rep(i,n+1)v[i][i]=pii(INF,INF);
	rep(i,n){
		pii temp;
		int s,g;
		cin>>s>>g>>temp.first>>temp.second;
        v[--s][--g]=temp;
	}
    rep(k,m){
        rep(j,m){
            rep(i,m){
                v[i][j].first=min(v[i][k].first+v[k][j].first,v[i][j].first);
                v[i][j].second=min(v[i][k].second+v[k][j].second,v[i][j].second);

            }
        }
    }
    int k; cin>>k;
    rep(i,k){
        int s,g,r; cin>>s>>g>>r;
        if(!r)cout<<v[--s][--g].first<<endl;
        else cout<<v[--s][--g].second<<endl;
    }
    
    }
}

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

int main(){
  int n, m, k;
  const int INF = 1 << 30;

  while(cin >> n >> m){
    if((n|m) == 0) break;

    vector<int> d(m, INF);
    vector<vector<vector<int> > > score(2, vector<vector<int> >(m, vector<int>(m, 0)));
    int a, b, cost, time;

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


    int p, q, r;

    cin >> k;
    while(k--){
      cin >> p >> q >> r; 
      --q;
      --p;
      queue<int> req;
      req.push(p);
      d[p] = 0; 
	while(! req.empty()){
	  int v = req.front();
	  req.pop();

	  for(int i=0; i<m; i++){
	    if(score[r][v][i] != 0){
	      if(d[v] + score[r][v][i] < d[i]){
		d[i] = d[v] + score[r][v][i];
		req.push(i);
	      } 
	    }
	  }
	}
	cout << d[q] << endl;
    }
  }
    return 0;
}

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

#define MAX_M 110

struct edge{int to,cost;};
typedef pair<int,int> P;

vector<edge> c[2][MAX_M];

int dijkstra(int s,int e,bool r){
	int d[MAX_M];
	for(int i=0;i<MAX_M;i++){
		d[i]=INT_MAX;
	}
	priority_queue<P,vector<P>,greater< P > > que;
	
	d[s]=0;
	
	que.push(P(0,s));
	
	while(!que.empty()){
		P p=que.top();que.pop();
		int v=p.second;
		if(d[v]<p.first) continue;
		for(int i=0;i<c[r][v].size();i++){
			edge e=c[r][v][i];
			if(d[e.to]>d[v]+e.cost){
				d[e.to]=d[v]+e.cost;
				que.push(P(d[e.to],e.to));
			}
		}
	}
	
	return d[e];
}

int main(){
	int n,m;
	while(cin>>n>>m,n!=0||m!=0){
		for(int i=0;i<n;i++){
			int a,b,cos,tim;
			cin>>a>>b>>cos>>tim;
			edge co,t;
			co.to=b;co.cost=cos;
			t.to=b;t.cost=tim;
			c[0][a].push_back(co);
			c[1][a].push_back(t);
		}
		cin>>n;
		for(int i=0;i<n;i++){
			int s,e;
			bool r;
			cin>>s>>e>>r;
			int ans=dijkstra(s,e,r);
			cout<<ans<<endl;
		}
	}
}

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

int t[303][303];
int c[303][303];

int main() {
  for( int N, M; cin >> M >> N, (N || M); ) {
    
    for(int i = 0; i < N; i++)
      for(int j = 0; j < N; j++) {
        t[i][j] = 987654321;
        c[i][j] = 987654321;
      }
    
    for(int n = 0; n < M; n++) {
      int a, b, time, cost;
      cin >> a >> b >> time >> cost;
      a--, b--;
      t[a][b] = t[b][a] = time;
      c[a][b] = c[b][a] = cost;
    }
    
    for(int k = 0; k < N; k++)
      for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++) {
          t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
          c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
        }
    
    
    cin >> M;
    for(int m = 0; m < M; m++) {
      int a, b, r;
      cin >> a >> b >> r;
      a--, b--;
      if( r == 0 ) 
        cout << c[a][b] << endl;
      else 
        cout << t[a][b] << endl;
    }
    
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	while(cin >> n >> m,n){
		int a,b,c,d;
		int co[m][m];
		int ti[m][m];
		for(int i = 0; i < m; i++)
			for(int j = 0; j < m; j++){
				co[i][j] = 100000000;
				ti[i][j] = 100000000;
			}
		for(int i = 0; i < n; i++){
			cin >> a >> b >> c >> d;
			a--; b--;
			co[a][b] = c;
			co[b][a] = c;
			ti[a][b] = d;
			ti[b][a] = d;
		}
		int asd,p,q,r;
		cin >> asd;
		for(int l = 0; l < m; l++)
			for(int j = 0; j < m; j++)
				for(int k = 0; k < m; k++){
						co[j][k] = min(co[j][k] , co[j][l] + co[l][k]);
						ti[j][k] = min(ti[j][k] , ti[j][l] + ti[l][k]);
				}
		for(int i = 0; i < asd; i++){
			cin >> p >> q >> r;
			p--; q--;
			cout << (r == 0 ? co[p][q] : ti[p][q]) << endl;
		}
	}
	return 0;
}