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 <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;

int graph0[111][111];
int graph1[111][111];

int main(){
    int n,m;
    while(cin >> m >> n && n){
        rep(i,n+1)rep(j,n+1) graph0[i][j] = graph1[i][j] = i==j ? 0 : 1e8;
        rep(i,m){
            int a,b, c,d;
            cin >> a >> b >> c >> d;
            graph0[a][b] = graph0[b][a] = c;
            graph1[a][b] = graph1[b][a] = d;
        }
        for(auto & g : {graph0, graph1}){
            rep(k,n+1)rep(i,n+1)rep(j,n+1) g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
        }
        int q;
        cin >> q;
        rep(qq,q){
            int a,b,c;
            cin >> a >> b >> c;
            if(c == 0){
                cout << graph0[a][b];
            } else {
                cout << graph1[a][b];
            }
            cout << endl;
        }
    }
}

C++: #include<iostream>
#include<queue>
#include<algorithm>
#define rep(i,b,e) for ( int i = b; i <= e; i++ )
using namespace std;

#define MAX 1000
#define INFTY (1<<21)
int n, C[MAX+1][MAX+1], T[MAX+1][MAX+1];

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

int dijkstra( int s, int t, int type ){
    priority_queue<State> PQ;
    bool visited[MAX+1];
    int d[MAX+1];
    rep(i,1,n){
	visited[i] = false;
	d[i] = INFTY;
    }
    PQ.push(State(s, 0));
    d[s] = 0;
    State u;
    while( !PQ.empty() ){
	u = PQ.top(); PQ.pop();
	if ( u.p == t ) return u.e;
	visited[u.p] = true;
	rep(v,1,n){
	    if ( C[u.p][v] == INFTY || visited[v] ) continue;
	    if ( type == 0 ){
		if ( d[v] > d[u.p] + C[u.p][v] ){
		    d[v] = d[u.p] + C[u.p][v];
		    PQ.push(State(v, d[v]));
		} 
	    } else {
		if ( d[v] > d[u.p] + T[u.p][v] ){
		    d[v] = d[u.p] + T[u.p][v];
		    PQ.push(State(v, d[v]));
		} 
	    }
	}
    }
    return -1;
}

main(){
    int k, s, t, cost, tm, q, r;
    while(1){
	scanf("%d %d", &k, &n);
	if ( n == 0 && k == 0 ) break;
	rep(i,1,n) rep(j,1,n) { C[i][j] = T[i][j] = INFTY; }
	rep(i,1,k){
	    scanf("%d %d %d %d", &s, &t, &cost, &tm);
	    C[s][t] = C[t][s] = cost;
	    T[s][t] = T[t][s] = tm;
	}
	scanf("%d", &q);
	rep(i,1,q){
	    scanf("%d %d %d", &s, &t, &r);
	    cout << dijkstra(s, t, r) << endl;
	}
    }
}

C++: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
using namespace std;
main(){
  int m,n;
  while(cin>>m>>n,n){
    int a1[n][n],a2[n][n],q,w,e,r;
    r(i,n)r(j,n){
      if(i==j)a2[i][j]=a1[i][j]=0;
      else a2[i][j]=a1[i][j]=INF;
    }
    while(m--){
      scanf("%d%d%d%d",&q,&w,&e,&r);q--;w--;
      a1[q][w]=a1[w][q]=e;
      a2[q][w]=a2[w][q]=r;
    }
    r(k,n)
    #pragma omp parallel
    r(i,n)r(j,n){
      a1[i][j]=min(a1[i][j],a1[i][k]+a1[k][j]);
      a2[i][j]=min(a2[i][j],a2[i][k]+a2[k][j]);
    }
    cin>>e;
    while(e--){
      scanf("%d%d%d",&q,&w,&r);q--;w--;
      if(r)cout<<a2[q][w]<<endl;
      else cout<<a1[q][w]<<endl;
    }
  }
}

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

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)

int main(){
	while(1){
		int n,m;
		
		cin>>n>>m;
		if(n==0)break;
		
		
		int froid[111][111][2];
		rep(i,111)rep(j,111)rep(k,2)froid[i][j][k]=1010000000;
		
		
		rep(i,n){
			int a,b,c,d;
			cin>>a>>b>>c>>d;
			
			froid[a][b][0]=c;
			froid[b][a][0]=c;
			froid[a][b][1]=d;
			froid[b][a][1]=d;
			
		}
		
		rep(p,2){
			reps(k,m){
				reps(i,m){
					reps(j,m){
						froid[i][j][p] = min(froid[i][j][p],froid[i][k][p]+froid[k][j][p]);
					}
				}
			}
		}
		
		int k;
		cin>>k;
		
		rep(i,k){
			int a,b,c;
			cin>>a>>b>>c;
			
			printf("%d\n",froid[a][b][c]);
		}
	}
}

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

#define NMAX 3000
#define MMAX 100
#define INF (1000*MMAX+100)*1000
using namespace std;

int main(void){

	int n,m;
	int a[NMAX],b[NMAX],cost[2][NMAX];
	int k;
	int p,q,r;
	int node[MMAX];
	queue<int> now;

	while(1){
		cin>>n>>m;
		if(n==0||m==0) break;
		for(int i=0;i<n;i++){
			cin>>a[i]>>b[i]>>cost[0][i]>>cost[1][i];
		}
		cin>>k;
		for(int i=0;i<k;i++){
			cin>>p>>q>>r;

			for(int i=0;i<MMAX;i++) node[i]=INF;
			now.push(p);
			node[p]=0;

			while(!now.empty()){
				if(node[q]!=INF){
					if(node[now.front()]>=node[q]){
						now.pop();
						continue;
					}
				}
				for(int j=0;j<n;j++){
					if(a[j]==now.front()){
						if(node[b[j]]>node[a[j]]+cost[r][j]){
							now.push(b[j]);
							node[b[j]]=node[a[j]]+cost[r][j];
						}
					}
					if(b[j]==now.front()){
						if(node[a[j]]>node[b[j]]+cost[r][j]){
							now.push(a[j]);
							node[a[j]]=node[b[j]]+cost[r][j];
						}
					}
				}
				now.pop();
			}
			cout<<node[q]<<endl;
		}
	}

	return 0;
}