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<cstdio>
#include<iostream>
#include<cmath>
#include<queue>

#define NMAX 3000
#define MMAX 100
#define TMAX 1000
#define CMAX 1000
#define KMAX 200

using namespace std;

int main(){
	std::ios_base::sync_with_stdio(false);

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

	while(1){
		for(int i=0;i<MMAX;i++){
			for(int j=0;j<MMAX;j++){
				for(int k=0;k<2;k++){
					data[i][j][k]=-1;
				}
			}
		}

		cin>>n>>m;
		if(n==0&&m==0) break;
		for(int i=0;i<n;i++){
			cin>>a>>b>>cost>>time;
			data[a][b][1]=time;
			data[b][a][1]=time;
			data[a][b][0]=cost;
			data[b][a][0]=cost;
		}

		cin>>k;
		for(int i=0;i<k;i++){
			cin>>p>>q>>r;

			for(int i=0;i<MMAX;i++){
				dp[i]=-1;
		    }

			now.push(p);
			dp[p]=0;
			while(!now.empty()){
				for(int i=0;i<n;i++){
					if(data[now.front()][i][r]!=-1){
						if(dp[i]==-1||dp[i]>dp[now.front()]+data[now.front()][i][r]){
							dp[i]=dp[now.front()]+data[now.front()][i][r];
							now.push(i);
						}
					}
				}
				now.pop();
			}
			cout<<dp[q]<<endl;
		}

	}



	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cmath>
#include <queue>

#define INF (1 << 26)

using namespace std;

int cost[200][200], dist[200][200];

int main(){
	
	int n, m;
	
	while(true){
		
		cin >> n >> m;
		
		if(n == 0 && m == 0){
			break;
		}
		
		for(int i = 0; i <= m; i++){
			for(int j = 0; j <= m; j++){
				cost[i][j] = INF;
				cost[j][i] = INF;
				dist[i][j] = INF;
				dist[j][i] = INF;
			}
		}
		
		for(int i = 0; i < n; i++){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[a][b] = c;
			cost[b][a] = c;
			dist[a][b] = t;
			dist[b][a] = t;
		}
		
		for(int i = 0; i <= m; i++){
			for(int j = 0; j <= m; j++){
				for(int k = 0; k <= m; k++){
					cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
					dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
				}
			}
		}
		
		int k;
		
		cin >> k;
		
		for(int loop = 0; loop < k; loop++){
			int p, q, r;
			cin >> p >> q >> r;
			if(r == 0){
				cout << cost[p][q] << endl;
			}else{
				cout << dist[p][q] << endl;
			}
		}
		
	}
	
	return 0;
}

C++: #include<iostream>

#define INF 1000000
using namespace std;

int main(){
	int data[2][110][110];
	for(int i=0;i<110;i++){
		for(int j=0;j<110;j++){
				data[1][i][j]=INF;
				data[0][i][j]=INF;
			
		}
	}	
	int n,m;
	while(cin>>n>>m){
	int a,b,cost,time;
	for(int i=0;i<n;i++){
		cin>>a>>b>>cost>>time;
		data[0][a][b]=cost;
		data[0][b][a]=cost;
		data[1][a][b]=time;
		data[1][b][a]=time;
	}

	for(int count=0;count<2;count++){
		for(int k=1;k<=m;k++){
			for(int i=1;i<=m;i++){
				for(int j=1;j<=m;j++)data[count][i][j]=min(data[count][i][j],data[count][i][k]+data[count][k][j]);
			}
		}
	}			

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

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

using namespace std;
typedef pair<int,int> pii;

struct edge{
  int to;
  int cost;
  int rev;
  bitset<100> flags;
  edge(int t,int c,int r){
    to=t,cost=c,rev=r;
  }
  edge(){}
};

int dijkstra(int s,int t,int N,const vector<vector<edge> > &graph,vector<int> &clusters){
  const int INF=1<<29;
  //int target_cluster=clusters[t];
  priority_queue<pii,vector<pii>,greater<pii> > pq;
  pq.push(pii(0,s));
  vector<int> d(N,INF);
  while(pq.size()){
    pii p=pq.top();pq.pop();
    int ccost=p.first;
    int cnode=p.second;
    if(d[cnode]<ccost)continue;
    for(int i=0;i<(int)graph[cnode].size();i++){
      edge e=graph[cnode][i];
      //if(!e.flags[target_cluster])continue;
      const int ncost=e.cost+ccost;
      const int to=e.to;
      if(d[to]>ncost){
        d[to]=ncost;
        pq.push(pii(ncost,to));
      }
    }
  }
  return d[t];
}

int main(){

  int N,M;
  while(cin>>N>>M&&(N|M)){
    vector<vector<edge> > graph[2];
    for(int i=0;i<2;i++)
      for(int j=0;j<M;j++)
        graph[i].push_back(vector<edge>());
    for(int i=0;i<N;i++){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      a--;b--;
      //cout<<a<<" "<<b<<endl;
      graph[0][a].push_back(edge(b,c,graph[0][b].size()));
      graph[0][b].push_back(edge(a,c,graph[0][a].size()-1));
      graph[1][a].push_back(edge(b,d,graph[1][b].size()));
      graph[1][b].push_back(edge(a,d,graph[1][a].size()-1));
    }
    vector<int> clusters(N,0);
    int K;
    cin>>K;
    for(int q=0;q<K;q++){
      int p,q,r;
      cin>>p>>q>>r;
      p--;q--;
      if(r==0){
        cout<<dijkstra(p,q,M,graph[0],clusters)<<endl;
      }
      else{
        cout<<dijkstra(p,q,M,graph[1],clusters)<<endl;
      }
    }
  }
  
  return 0;
}