Go: // Warshall–Floyd Algorithm

package main

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

var scanner = bufio.NewScanner(os.Stdin)

const INF = 2000000000

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
			c_matrix[b-1][a-1] = c
			t_matrix[a-1][b-1] = t
			t_matrix[b-1][a-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<cstdio>

using namespace std;

int i, j, k, p, q, r, n, m, a, b, c, cost[101], t, dc[101][101], dt[101][101];

int dic(const int d[101][101]) {
  cost[p] = 0;
  int min, f[101] = {};
  for(i = 0; i < m; i++) {
    f[p] = 1;
    for(j = 1; j <= m; j++) {
      if(f[j]) continue;
      if(cost[j] > cost[p] + d[p][j]) cost[j]  = cost[p] + d[p][j];
    }
    min = 200000000;
    for(j = 1; j <= m; j++ ) {
      if(f[j]) continue;
      if(min > cost[j]) min = cost[p = j];
    }
  }
  return cost[q];
}


int main() {
  while(cin >> n >> m, n != 0 || m != 0) {
    for(i = 0; i < 100; i++)
      for(j = 0; j < 100; j++) {
	dc[i][j] = 100000000;
	dt[i][j] = 100000000;
      }
  

  
    while(n--) {
      cin >> a >> b >> c >> t;
      dc[a][b] = dc[b][a] = c;
      dt[a][b] = dt[b][a] = t;
    }
  
    cin >> k;
    
    while(k--) {
      cin >> p >> q >> r;
      for(i = 0; i <= 100; i++) cost[i] = 100000000;
      if(r) cout << dic(dt) << endl;
      else cout << dic(dc) << endl;
    }
  
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
#define rep(i,n) for(int i = 0 ; i < n ; i++)
 
int main(){
    int mon[100][100];
    int tim[100][100];
    int n,m;
    while(cin >> m >> n && n ){
        rep(i,n)rep(j,n) mon[i][j] = tim[i][j] = 1e9;
        for(int i = 0 ; i < m ; i++){
            int a,b,c,d;
            cin >> a >> b >> c >> d;
            a--,b--;
            mon[a][b] = mon[b][a] = c;
            tim[a][b] = tim[b][a] = d;
        }
        rep(i,n)rep(j,n)rep(k,n){
            mon[j][k] = min(mon[j][k],mon[j][i]+mon[i][k]);
            tim[j][k] = min(tim[j][k],tim[j][i]+tim[i][k]);
        }
        int R;
        cin >> R;
        rep(i,R){
            int a,b,c;
            cin >> a >> b >> c;
            a--,b--;
            if(c) cout << tim[a][b];
            else cout << mon[a][b];
            cout << endl;
        }
    }
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
  int n,m;
  while(cin >>n>>m,n||m){
    int ans[100][100][2];
    for(int i=0; i<100; i++){
      for(int j=0; j<100; j++){
	for(int k=0; k<2; k++){
	  ans[i][j][k] = 100000000;
	  if(i==j) ans[i][j][k] = 0;
	}
      }
    }
    for(int i=0,a,b,c,d; i<n; i++){
      cin >>a>>b>>c>>d;
      a--;b--;
      ans[a][b][0] = ans[b][a][0] = c;
      ans[a][b][1] = ans[a][b][1] = d;
    }
    for(int k=0; k<2; k++){
      for(int l=0; l<m; l++){
	for(int i=0; i<m; i++){
	  for(int j=0; j<m; j++){
	    ans[i][j][k] = min(ans[i][j][k],ans[i][l][k]+ans[l][j][k]);
	  }
	}
      }
    }
    int t=0;
    cin >>t;
    while(t--){
      int p,q,r;
      cin >>p>>q>>r;
      cout <<ans[p-1][q-1][r]<<endl;
    }
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INF 1e7
struct edge{int to,cost,time;};
vector<edge> G[101];
int d[101];
bool visited[101];
int V;
void dijkstra(int p,int r){
  fill(d,d+101,INF);
  d[p]=0;
  memset(visited,false,sizeof(visited));
  while(1){
    int id=-1;
    for(int i=0;i<V;++i){
      if(!visited[i])
        if(id==-1 || d[id]>d[i])
          id=i;
    }
    if(id==-1) break;
    visited[id]=true;
    for(int i=0;i<G[id].size();++i){
      edge e=G[id][i];
      if(r) d[e.to]=min(d[e.to],d[id]+e.time);
      else d[e.to]=min(d[e.to],d[id]+e.cost);
    }
  }
}
int main(){
  int n;
  while(cin>>n>>V,n){
    for(int i=0;i<n;++i){
      int a,b,c,t; cin>>a>>b>>c>>t;
      G[a].push_back((edge){b,c,t});
      G[b].push_back((edge){a,c,t});
    }
    int k; cin>>k;
    for(int i=0;i<k;++i){
      int p,q,r; cin>>p>>q>>r;
      dijkstra(p,r);
      cout<<d[q]<<endl;
    }
  }
  return 0;
}

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]=e;
      a2[q][w]=r;
    }
    r(k,n)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,&e);q--;w--;
      if(r)cout<<a2[w][q]<<endl;
      else cout<<a1[w][q]<<endl;
    }
  }
}