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 <algorithm>
#include <queue>
#include <vector>

#define MAX_V 100
#define INF 10000000

using namespace std;

typedef pair<int, int> P;

struct edge {
	int to, info;
	edge(int _to, int _info) { to = _to, info = _info; };
};

vector<edge> Gc[MAX_V];
vector<edge> Gt[MAX_V];
int V, E;
int c[MAX_V];
int t[MAX_V];
int cm[MAX_V][MAX_V];
int tm[MAX_V][MAX_V];

void solve();
void dijkstra(int s, int g, int r);

int main()
{
	while (cin>>E>>V, E != 0 && V != 0) {
		for (int i = 0; i < V; i++) {
			fill(cm[i], cm[i] + V, INF);
			fill(tm[i], tm[i] + V, INF);
			cm[i][i] = 0, tm[i][i] = 0;
			Gc[i].clear(), Gt[i].clear();
		}
		for (int i = 0; i < E; i++) {
			int fr, to, co, ti;
			cin>>fr>>to>>co>>ti;
			Gc[fr - 1].push_back(edge(to - 1, co));
			Gc[to - 1].push_back(edge(fr - 1, co));
			Gt[fr - 1].push_back(edge(to - 1, ti));
			Gt[to - 1].push_back(edge(fr - 1, ti));
		}
		solve();
	}
}

void solve()
{
	int k,p,q,r;
	cin>>k;
	for (int i = 0; i < k; i++) {
		cin>>p>>q>>r;
		if (r == 0 && (cm[p-1][q-1] != INF || cm[q-1][p-1] != INF)) {
			cout<<min(cm[p-1][q-1], cm[q-1][p-1])<<endl;
		} else if (r == 1 && (tm[p-1][q-1] != INF || tm[q-1][p-1] != INF)) {
			cout<<min(tm[p-1][q-1], tm[q-1][p-1])<<endl;
		} else {
			dijkstra(p - 1, q - 1, r);
		}
	}
}

void dijkstra(int s, int g, int r)
{
	priority_queue<P, vector<P>, greater<P> > que;
	switch (r) {
			case 0:
			que.push(P(0,s));
			fill(c, c + V, INF);
			c[s] = 0;
			while (!que.empty()) {
				P p = que.top();
				que.pop();
				int v = p.second;
	
				if (c[v] < p.first) continue;
				for (int i = 0; i < Gc[v].size(); i++) {
					edge e = Gc[v][i];
					if (c[e.to] > c[v] + e.info) {
						c[e.to] = c[v] + e.info;
						que.push(P(c[e.to], e.to));
					}
				}
			}
			
			for (int i = 0; i < V; i++) {
				cm[s][i] = c[i];
			}
			cout<<c[g]<<endl;
			
			break;
			
			case 1:
			que.push(P(0,s));
			fill(t, t + V, INF);
			t[s] = 0;
			while (!que.empty()) {
				P p = que.top();
				que.pop();
				int v = p.second;
				
				if (t[v] < p.first) continue;
				for (int i = 0; i < Gt[v].size(); i++) {
					edge e = Gt[v][i];
					if (t[e.to] > t[v] + e.info) {
						t[e.to] = t[v] + e.info;
						que.push(P(t[e.to], e.to));
					}
				}
			}
			
			for (int i = 0; i < V; i++) {
				tm[s][i] = t[i];
			}
			
			cout<<t[g]<<endl;
			
			break;
	}
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#define INF (1<<21)
using namespace std;

int main(){
  int i,j,k,kk,aa,bb,n,m,a,b,d,dd,c[101][101],t[101][101],f;
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;

    for(i=0;i<=m;i++){
      for(j=0;j<=m;j++){
	c[i][j] = INF;
	t[i][j] = INF;
	if(i == j){
	  c[i][j] = 0;
	  t[i][j] = 0;
	}
      }
    }

    for(i=0;i<n;i++){
      cin >> a >> b >> d >> dd;
      c[a][b] = c[b][a] = d;
      t[a][b] = t[b][a] = dd;
    }

    for(i=0;i<=m;i++){
      for(j=0;j<=m;j++){
	for(k=0;k<=m;k++){
	  if(t[i][j] > t[i][k] + t[k][j]) t[i][j] = t[i][k] + t[k][j];
	  if(c[i][j] > c[i][k] + c[k][j]) c[i][j] = c[i][k] + c[k][j];
	}
      }
    }

    cin >> kk;
    for(i=0;i<kk;i++){
      cin >> aa >> bb >> f;
      if(f) cout << t[aa][bb] << endl;
      else cout << c[aa][bb] << endl;
    }

  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999
#define MAX_V 100

struct edge{int to,cost;};
vector<edge> G[2][MAX_V];
int d[2][MAX_V];

int dijkstra(int s,int V,int ver){
    priority_queue< pii,vector<pii>,greater<pii> > que;
    rep(i,MAX_V)d[ver][i]=INF;
    d[ver][s]=0;
    que.push(pii(0,s));
    
    while(que.size()){
        pii p=que.top();
        que.pop();
        
        int v=p.second;
        if(d[ver][v]<p.first)continue;
        
        rep(i,G[ver][v].size()){
            edge e=G[ver][v][i];
            if(d[ver][e.to]>d[ver][v]+e.cost){
                d[ver][e.to]=d[ver][v]+e.cost;
                que.push(pii(d[ver][e.to],e.to));
            }
        }
    }
    return d[ver][V];
}




int main(){
    int n,m;
    while(cin>>n>>m){
        if(n==0&&m==0)break;
    rep(i,n){
        int a,b,cost,time;
        cin>>a>>b>>cost>>time;
        a--,b--;
        G[0][a].pb(edge{b,cost});
        G[0][b].pb(edge{a,cost});
        G[1][a].pb(edge{b,time});
        G[1][b].pb(edge{a,time});
    }
    int k;
    cin>>k;
    rep(i,k){
        int p,q,r;
        cin>>p>>q>>r;
        p--,q--;
        cout<<dijkstra(p,q,r)<<endl;
    }
    }
}