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]
					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()
{
	while (true) {
		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;
		}

		for (int i = 1; i < m; i++) {
			for (int j = 1; j < m; j++) {
				for (int k = 1; k < m; k++) {
					if (C[j][k] > C[j][i] + C[i][k]) {
						C[j][k] = C[j][i] + C[i][k];
					}
					if (T[j][k] > T[j][i] + T[i][k]) {
						T[j][k] = T[j][i] + T[i][k];
					}
				}
			}
		}

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











	}

	return 0;
}

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

struct Node{
  int num;

  vector<int> edge_to;
  vector<int> edge_cost;

  bool done;
  int cost;
};

int dijkstra (vector<Node> g,int start,int goal){
  int d;
  g[start].cost=0;

  while(1){
    d=-1;

    for(int i=1;i<=g.size();i++){
      if( g[i].done || g[i].cost < 0 ) continue;
      if( d==-1 || g[i].cost < g[d].cost ) d=i;
    }

    if(d==-1) break;
    g[d].done=true;

    for(int i=0; i< g[d].edge_to.size(); i++){
      int to = g[d].edge_to[i];
      int cost = g[d].cost + g[d].edge_cost[i];
      if(g[to].cost < 0 || cost < g[to].cost)
	g[to].cost = cost;
    }
  }

  return g[goal].cost;
}

int main(){
  for(;;){
    int n,m,a,b,c,t,p,q,r;
    cin>>n>>m;
    if(n==0&&m==0)break;

    vector<Node> time_graph,cost_graph;
    time_graph.reserve(m+1);
    time_graph.resize(m+1);
    cost_graph.reserve(m+1);
    cost_graph.resize(m+1);

    Node no;
    for(int i=1;i<=m;i++){
      no.num = i;
      no.done = false;
      no.cost = -1;
      no.edge_to.clear();
      no.edge_cost.clear();
      time_graph[i] = no;
      cost_graph[i] = no;
    }


    while(n--){
      cin>>a>>b>>c>>t;
      time_graph[a].edge_to.push_back(b);
      time_graph[b].edge_to.push_back(a);
      cost_graph[a].edge_to.push_back(b);
      cost_graph[b].edge_to.push_back(a);
      cost_graph[a].edge_cost.push_back(c);
      cost_graph[b].edge_cost.push_back(c);
      time_graph[a].edge_cost.push_back(t);
      time_graph[b].edge_cost.push_back(t);
    }

    cin>>n;
    while(n--){
      cin>>p>>q>>r;

      vector<Node> n;

      if(r==1){
	n.reserve(time_graph.size());
	n.resize(time_graph.size());
	n.assign(time_graph.begin(),time_graph.end());
      }else{
	n.reserve(cost_graph.size());
	n.resize(cost_graph.size());
	n.assign(cost_graph.begin(),cost_graph.end());
      }

      cout<<dijkstra(n,p,q)<<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)r(i,n)r(j,n){
      if(a1[i][k]+a1[k][j]<=INF)a1[i][j]=min(a1[i][j],a1[i][k]+a1[k][j]);
      if(a2[i][k]+a2[k][j]<=INF)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 <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define IREP(it,array) for(vector<int>::iterator it=array.begin(); it!=array.end(); ++it)
#define SREP(it,array) for(vector<string>::iterator it=array.begin(); it!=array.end(); ++it)

#define MP       make_pair
#define PB       push_back
#define ALL(x)   (x).begin(),(x).end()

const int INF = 1<<29;
const double EPS = 1e-9;
double zero(double d){
  return d < EPS ? 0.0 : d;
}

typedef long long LL;
typedef pair<int,int> P;


int main()
{
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  int n,m;
  cin >> n >> m;

  vector< vector<int> > cost(m+1, vector<int>(m+1,INF)),
    time(m+1, vector<int>(m+1,INF));

  REP(i,cost.size()){
    cost[i][i] = 0;
    time[i][i] = 0;
  }

  REP(i,n){
    int a,b,c,t;
    cin >> a >> b >> c >> t;
    cost[a][b] = c;
    cost[b][a] = c;
    time[a][b] = t;
    time[b][a] = t;
  }

  int k;
  cin >> k;
  REP(_tmp,k){
    int p,q,r;
    cin >> p >> q >> r;

    vector< vector<int> > data(m+1, vector<int>(m+1,0));
    data = r ? time : cost;

    deque<bool> reached(m+1,0);
    vector<int> dis(m+1,INF);
    dis[p] = 0;

    while(true){
      int minn = -1;
      REP(i,reached.size()){
	if( ! reached[i] ){
	  if( minn==-1 || dis[minn] > dis[i] ){
	    minn = i;
	  }
	}
      }
      if( minn == q )
	break;

      reached[minn] = true;
      REP(i,data[minn].size()){
	dis[i] = min( dis[minn] + data[minn][i], dis[i] );
      }
    }

    cout << dis[q] << endl;
  }

  return 0;
}