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
			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 <string>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

#define REP(i,k,n) for(int i=k;i<n;++i)
#define rep(i,n) REP(i,0,n)

int N,M,K;
const int MAXN=128;
const int INF=1e9;
int G1[MAXN][MAXN];
int G2[MAXN][MAXN];

int main()
{
	while(cin>>M>>N&&(M||N))
	{
		rep(i,N){ rep(j,N){ G1[i][j]=INF; G2[i][j]=INF; } }

		rep(i,M)
		{ 
			int a,b,c,d;
			cin>>a>>b>>c>>d; --a; --b;
			G1[a][b]=G1[b][a]=c;
			G2[a][b]=G2[b][a]=d;
		}

		rep(from,N) rep(to,N) rep(point,N)
		{
			G1[from][to] = min(G1[from][to],G1[from][point]+G1[point][to]);
			G2[from][to] = min(G2[from][to],G2[from][point]+G2[point][to]);
		}

		cin>>K;
		rep(i,K)
		{
			int a,b,c; cin>>a>>b>>c; --a; --b;
			cout<<(c?G2[a][b]:G1[a][b])<<endl;
		}
	}

	return 0;
}

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

using namespace std;

int main() {
	int a, b, time, cost, querys;
	int i, j, k, p, q, r, n, m;
	int times[100][100], costs[100][100];

	while (true) {
		cin >> n >> m;
		if (!n && !m)
			break;

		for (i = 0; i < m; i++) {
			for (j = 0; j < m; j++) {
				times[i][j] = (i == j) ? 0 : 1e9;
				times[j][i] = (i == j) ? 0 : 1e9;
				costs[i][j] = (i == j) ? 0 : 1e9;
				costs[j][i] = (i == j) ? 0 : 1e9;
			}
		}

		for (i = 0; i < n; i++) {
			cin >> a >> b >> cost >> time;
			times[a - 1][b - 1] = time;
			costs[a - 1][b - 1] = cost;
		}

		for (k = 0; k < m; k++) {
			for (i = 0; i < m; i++) {
				for (j = 0; j < m; j++) {
					times[i][j] = min(times[i][j], times[i][k] + times[k][j]);
					costs[i][j] = min(costs[i][j], costs[i][k] + costs[k][j]);
				}
			}
		}

		cin >> querys;
		for (i = 0; i < querys; i++) {
			cin >> p >> q >> r;
			cout << (r ? times[p - 1][q - 1] : costs[p - 1][q - 1]) << endl;
		}
	}
}

C++: #include <iostream>
#include <vector>
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define pb push_back
using namespace std;

const int INF = 10000000;
int cost[2][111][111];
int d[111];
bool used[111];
int V;
struct edge {
    int from,to,money,time;
};
vector<edge> E;

int bf(int s,int g,int r){
    FOR(i,0,111)d[i]=INF;
    d[s]=0;
    while(1){
        bool update=false;
        FOR(i,0,E.size()){
            const edge& e=E[i];
            int from=e.from;
            int to=e.to;
            int cost=r?e.time:e.money;
            if(d[to]>d[from]+cost){
                d[to]=d[from]+cost;
                update=true;
            }
        }
        if(!update)break;
    }
    return d[g];
}

int main(){
    int n;
    while(1){
        cin>>n>>V;
        if(!(n||V)) break;
        E.clear();
        FOR(i,0,n){
            int a,b,m,t;cin>>a>>b>>m>>t;
            E.pb({a,b,m,t});
            E.pb({b,a,m,t});
        }
        int k;cin>>k;
        FOR(i,0,k){
            int p,q,r;cin>>p>>q>>r;
            cout<<bf(p,q,r)<<endl;
        }
    }
}

C++: #include<iostream>
#include<map>

using namespace std;

int main(){

  int x, y;

  while(cin >> x >> y){
    if(x == 0 && y == 0) break;

    int money[y+1][y+1], time[y+1][y+1];

    for(int i = 0; i <= y; i++){
      for(int j = 0; j <= y; j++){
	money[i][j] = 0;
	time[i][j] = 0;
      }
    }

    int from, to, mon, tim;

    for(int i = 0; i < x; i++){
      cin >> from >> to >> mon >> tim;
      money[from][to] = mon;
      money[to][from] = mon;
      time[from][to] = tim;
      time[to][from] = tim;
    }

    int num, order, weight[y+1];

    cin >> num;

    int come[y];

    for(int i = 0; i < num; i++){
      cin >> from >> to >> order;

      for(int i = 0; i <= y; i++){
	weight[i] = -1;
	come[i] = 0;
      }

      weight[from] = 0;
      come[from] = 1;

      int min = from;

      if(order == 1){//time
	while(1){
	  from = min;
	  come[from] = 1;
	  for(int i = 1; i <= y; i++){
	    if(come[i] == 0 && time[from][i] != 0){
	      
	      if(weight[i] == -1 || weight[i] > weight[from] + time[from][i]){
		weight[i] = weight[from] + time[from][i];
	      }
	      
	    }
	  }
	  min = -1;
	  
	  for(int i = 0; i <= y; i++){
	    if(come[i] == 1) continue;
	    if(min == -1) min = i;
	    else{
	      if(weight[min] == -1 || weight[min] > weight[i] && weight[i] != -1) min = i;
	    }
	  }
	  if(min == -1) break;
	}
	    
      }else{//money
	while(1){
	  from = min;
	  come[from] = 1;
	  for(int i = 1; i <= y; i++){
	    if(come[i] == 0 && money[from][i] != 0){
	      
	      if(weight[i] == -1 || weight[i] > weight[from] + money[from][i]){
		weight[i] = weight[from] + money[from][i];
	      }
	      
	    }
	  }
	  min = -1;
	  
	  for(int i = 0; i <= y; i++){
	    if(come[i] == 1) continue;
	    if(min == -1) min = i;
	    else{
	      if(weight[min] == -1 || weight[min] > weight[i] && weight[i] != -1) min = i;
	    }
	  }
	  if(min == -1) break;
	}
	   
      }

      cout << weight[to] << endl;      
    }
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;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,0,n)r(j,0,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,0,n)r(i,0,n)r(j,i+1,n){
      a1[i][j]=a[j][i]=min(a1[i][j],a1[i][k]+a1[k][j]);
      a2[i][j]=a[j][i]=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;
    }
  }
}