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<vector>
#include<map>
#include<queue>
#include<functional>
#define INF 1000000000//INF=∞
using namespace std;
typedef pair<int, int> P;//firstは最短距離,secondは頂点番号
struct edge {
  int to, cost;
  edge() : to(0), cost(0) {}
  edge(int to, int cost) : to(to), cost(cost) {}
};
int V;//頂点の数
vector<vector<edge> > G;//頂点間の距離情報(隣接リスト)

int dijkstra(int s, int g){
  priority_queue<P, vector<P>, greater<P> > que;
  vector<int> d(V, INF);//最短距離を記録する配列(∞で初期化しておく)
  d[s] = 0;//頂点sから頂点sへの最短距離は0で確定
  que.push(P(0, s));//距離0,頂点sからスタート

  while(!que.empty()){
    /*処理1*/
    //キューの中で最も頂点sからの距離が近い頂点vとその距離cを取得
    P p = que.top();
    que.pop();
    int v = p.second, c = p.first;

    /*処理2*/
    //頂点vの最短距離がすでに確定していたらcontenue
    if(d[v] < c)
      continue;

    /*処理3*/
    //頂点vの最短距離が既に確定していなければ頂点vへの最短経路はcで確定
      d[v]=c;

    /*処理4*/
    //頂点vから移動しうる頂点とその距離をキューに入れる
    for(int i = 0; i < G[v].size(); i++){
      //頂点vはG[v].size()個の行き先を持つ
      edge e = G[v][i];
      if(d[e.to] > d[v] + e.cost){
        que.push(P(d[v] + e.cost, e.to));
      }
    }
  }

  return d[g];//頂点gへの最短距離を返す
}


int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    V = m;
    vector<vector<edge> > time_graph(m);
    vector<vector<edge> > cost_graph(m);
    for (int i = 0; i < n; i++) {
      int a, b, time, cost;
      cin >> a >> b >> time >> cost;
      a--; b--; // 0-indexedにする
      
      time_graph[a].push_back(edge(b, time));
      time_graph[b].push_back(edge(a, time));
      cost_graph[a].push_back(edge(b, cost));
      cost_graph[b].push_back(edge(a, cost));
    }

    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      p--; q--;
      if (r == 0) G = time_graph;
      else G = cost_graph;
      cout << dijkstra(p, q) << endl;
    }
  }
}

C++: #include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
const int INF=(1<<29);
int min(int a,int b){
    if(a>b)return b;
    return a;
}
int main(){
    int n,m,l,r,dis[110][110],cost[110][110],c,t;
    while(1){
    scanf("%d%d",&m,&n);
        if(n==0&&m==0)return 0;
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)dis[i][j]=cost[i][j]=INF;
    for(int i=0;i<m;i++){
        scanf("%d%d%d%d",&l,&r,&c,&t);
        dis[l][r]=dis[r][l]=t;
        cost[l][r]=cost[r][l]=c;
    }
    for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
        dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
        cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
    }
    scanf("%d",&n);
    while(n--){
        scanf("%d%d%d",&l,&r,&m);
        if(m==0)printf("%d\n",cost[l][r]);
        else printf("%d\n",dis[l][r]);
    }
    }
}

C++: #include<iostream>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)  REP(i,0,n)

#define NODE 301
const int INF = ( 1 << 21);

int table[2][NODE][NODE];
void solve(int n){
  rep(k,n)
    rep(i,n)
    rep(j,n)table[0][i][j]=min(table[0][i][j],table[0][i][k]+table[0][k][j]),
    table[1][i][j]=min(table[1][i][j],table[1][i][k]+table[1][k][j]);
}


main(){
  int n,m;
  while(cin>>m>>n && n){
    rep(i,n){
      rep(j,n)table[0][i][j]=table[1][i][j]=INF;
      table[0][i][i]=table[1][i][i]=0;
    }
    rep(i,m){
      int a,b,cost,time;
      cin>>a>>b>>cost>>time;
      a--,b--;
      table[0][a][b]=cost;table[0][b][a]=cost;
      table[1][a][b]=table[1][b][a]=time;
    }

    solve(n);
    int q;
    cin>>q;
    rep(i,q){
      int f,t,type;
      cin>>f>>t>>type;
      cout << table[type][f-1][t-1] << endl;
    }
    
  }
}

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

const int INF = 1000000000;

int main()
{
	int C[100][100];
	int T[100][100];
	int z=0;
	int p[200];
	int q[200];
	int r[200];
	int A[200];
	while(true){
		int n, m;
		cin >> n >> m;

		if(n==0 && m==0)
			break;

		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 k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					if(C[i][j]>C[i][k]+C[k][j]){
						C[i][j]=C[i][k]+C[k][j];
						
					}
				}
			}
		}
		for(int k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					if(T[i][j]>T[i][k]+T[k][j]){
						T[i][j]=T[i][k]+T[k][j];
						
					}
				}
			}
		}
		cin>>z;
		for(int i=0;i<z;i++){
			cin>>p[i]>>q[i]>>r[i];
		}
		
	}
	for(int i=0;i<z;i++){
		if(r[i]==0){
			cout<<C[p[i]-1][q[i]-1]<<endl;
		}
		if(r[i]==1){
			cout<<T[p[i]-1][q[i]-1]<<endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 1010001000
#define loop(i, n) for (int i = 1; i < n; i++)

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

}