Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"fmt"
)

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
				} else {
					c_dist[i][j] = c_matrix[i][j]
				}
				if c_matrix[i][j] == 0{
					c_prev[i][j] = -1
				} else {
					c_prev[i][j] = i
				}
				if t_matrix[i][j] == 0{
					t_dist[i][j] = INF
				} else {
					t_dist[i][j] = t_matrix[i][j]
				}
				if t_matrix[i][j] == 0{
					t_prev[i][j] = -1
				} else {
					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]
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[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 <stdio.h>
#define INF 10000000

int main(void){
	int n,m;
	
	while(scanf("%d %d",&m,&n),n,m){
		int i,j;
		int a,b,cost,time,c;
		int DPc[256][256];
		int DPt[256][256];
		int k,ans[256];
		
		for(i=0;i<256;i++){
			for(j=0;j<256;j++){
				if(i==j){
					DPc[i][j]=0;
					DPt[i][j]=0;
				}
				else{
					DPc[i][j]=INF;
					DPt[i][j]=INF;
				}
			}
		}
		
		for(i=0;i<m;i++){
			scanf("%d %d %d %d", &a, &b, &cost, &time);
			DPc[a][b]=cost;
			DPc[b][a]=cost;
			DPt[a][b]=time;
			DPt[b][a]=time;
		}
		
		for(k=0;k<=n;k++){
			for(i=0;i<=n;i++){
				for(j=0;j<=n;j++){
					if(DPt[i][k]+DPt[k][j]<DPt[i][j]){
						DPt[i][j]=DPt[i][k]+DPt[k][j];
					}
					if(DPc[i][k]+DPc[k][j]<DPc[i][j]){
						DPc[i][j]=DPc[i][k]+DPc[k][j];
					}
				}
			}
		}
		
		scanf("%d", &m);
		for(i=0;i<m;i++){
			scanf("%d %d %d", &a, &b, &c);
			if(c==1){
				ans[i]=DPt[a][b];
			}
			else {
				ans[i]=DPc[a][b];
			}
		}
		for(i=0;i<m;i++){
			printf("%d\n",ans[i]);
		}
	}
	return 0;
}

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()
#define int ll
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;
	}
	int k; cin>>k;
    rep(u,k){
    int s,g; int r;
    cin>>s>>g>>r;
    if(!r){
        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);
                }
            }
        }
        cout<<v[--s][--g].first<<endl;
    }else{
        rep(k,m){
            rep(j,m){
                rep(i,m){
                    v[i][j].second=min(v[i][k].second+v[k][j].second,v[i][j].second);
                }
            }
        }
        cout<<v[--s][--g].second<<endl;
    }
    }
    }
}

C++: #include <cstdio>
#include <vector>

using namespace std;

class Edge{
public:
	int from,to,cost,time;
	void Swap(){
		int self = from;
		from = to;
		to = from;
	}
	int GetValue(int r){
		if(r == 0){
			return cost;
		}else{
			return time;
		}
	}
};

vector<Edge> edge;
int n,m;

const int INF = 100000000;

int solve(const int p,const int q,const int r);

int main(){
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++){
		Edge ed;
		scanf("%d %d %d %d",&ed.from,&ed.to,&ed.cost,&ed.time);
		edge.push_back(ed);
		ed.Swap();
		edge.push_back(ed);
	}
	int k;
	int p,q,r;
	scanf("%d",&k);
	for(int i=0;scanf("%d %d",&p,&q) && p && q && i < k;i++){
		scanf("%d",&r);
		printf("%d\n",solve(p,q,r));
	}
	return 0;
}

int solve(const int p,const int q,const int r){
	//vector<int> d(m+1);
	int *d = new int[m+1];
	for(int i=0;i<=m;i++){
		d[i] = INF;
	}
	d[p] = 0;
	
	bool flag;
	while(1){
		flag = false;
		vector<Edge>::iterator it;
		for(it = edge.begin();it != edge.end();it++){
			if(d[(*it).from] != INF && d[(*it).to] > d[(*it).from] + (*it).GetValue(r)){
				d[(*it).to] = d[(*it).from] + (*it).GetValue(r);
				flag = true;
			}
		}
		if(!flag){
			break;
		}
	}
	
	int ans = d[q];
	delete [] d;
	return ans;
}

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 i = 0; i < N; i++) t[i][i] = c[i][i] = 0;
    
    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;
    }
    
  }
}