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>
 #include<stdio.h>
 #include<string>
 #include<math.h>
 #include<iomanip>
 #include<algorithm>
 #include<string.h>
 #include<cctype>
 #include<map>
 #include<set>
 #include<vector>
 #include<sstream>
 #include<stack>
 #include<queue>
 #include<deque>
 #include<functional>
 #include<utility>
 
 using namespace std;
 
 const int MAX_V=100+5;
 const int INF=100000000;

 int main()
 {
   int graph[2][MAX_V][MAX_V];
   int path,V;
   while(cin>>path>>V&&(path||V))
   {
     for(int m=0;m<2;m++)
     {
       for(int i=0;i<V;i++)
       {
         for(int j=0;j<V;j++)
         {
           if(i==j) graph[m][i][j]=0;
           else graph[m][i][j]=INF;        
         }       
       }        
     }  
     while(path--)
     {
       int p,q;
       int cost,time;
       cin>>p>>q>>cost>>time;
       graph[0][p-1][q-1]=cost;
       graph[1][p-1][q-1]=time;             
     }                       
     for(int m=0;m<2;m++)
     {
       for(int k=0;k<V;k++)
       {
         for(int i=0;i<V;i++)
         {
           for(int j=0;j<V;j++)
           {
             graph[m][i][j]=min(graph[m][i][j],graph[m][i][k]+graph[m][k][j]);        
           }        
         }          
       }        
     }     
     int que;
     cin>>que;
     while(que--)
     {
       int start,end,type;
       cin>>start>>end>>type;
       cout<<graph[type][start-1][end-1]<<endl;           
     }
   }
   //while(1);
   return 0;
 }

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int n,m;
  int a,b,c,d;
  int x;
  int p,q,r;
  int cost[111][111];
  int time[111][111];
  while(1){
    cin >> n >> m;
    if(n==0 && m==0)break;
    for(int i=0;i<111;i++){
      for(int j=0;j<111;j++){
	cost[i][j]=9999999;
	time[i][j]=9999999;
      }
    }
    for(int i=0;i<n;i++){
      cin >> a >> b >> c >> d;
      cost[a-1][b-1]=c;
      cost[b-1][a-1]=c;
      time[a-1][b-1]=d;
      time[b-1][a-1]=d;
    }
    for(int k=0;k<n;k++){
      for(int i=0;i<n;i++){
	for(int j=0;j<n;j++){
	  cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
	  time[i][j]=min(time[i][j],time[i][k]+time[k][j]);
	}
      }
    }
    cin >> x;
    for(int i=0;i<x;i++){
      cin >> p >> q >> r;
      cout << (r==0 ? cost[p-1][q-1] : time[p-1][q-1]) << endl;
    }
  }
}

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

struct edge {
    int from, to, cost, time;
};

int main() {
    int n, m, a, b, v, u, k, p, q, r;
    while (cin >> n >> m, n || m) {
        vector<edge> edges;
        for (int i=0; i<n; ++i) {
            cin >> a >> b >> v >> u;
            edges.push_back((edge){ a, b, v, u });
        }
        cin >> k;
        for (int i=0; i<k; ++i) {
            cin >> p >> q >> r;
            int d[100];
            for (int i=0; i<100; ++i) d[i] = 1e8;
            d[p] = 0;
            for (int i=0; i<100; ++i) {
                for (int j=0; j<(int)edges.size(); ++j) {
                    edge &e = edges[j];
                    if (r == 0) {
                        d[e.to] = min(d[e.to], d[e.from] + e.cost);
                    } else {
                        d[e.to] = min(d[e.to], d[e.from] + e.time);
                    }
                }
            }
            cout << d[q] << endl;
        }
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define INF 1<<10

int main(){
  int cost[101][101];
  int times[101][101];
  int n,m;
  int a,b,c,t;
  while(cin>>n>>m,n|m){
    for(int i=0;i<101;i++){
      for(int j=0;j<101;j++){
	cost[i][j]=times[i][j]=INF;
      }
    }
    for(int i=0;i<n;i++){
      cin>>a>>b>>c>>t;
      cost[a][b]=cost[b][a]=c;
      times[a][b]=times[b][a]=c;
    }
    for(int i=1;i<=m;i++){
      for(int j=1;j<=m;j++){
	for(int k=1;k<=m;k++){
	  cost[j][k]=min(cost[j][k],cost[j][i]+cost[i][k]);
	  times[j][k]=min(times[j][k],times[j][i]+times[i][k]);
	}
      }
    }
    int k,p,q,r;
    cin>>k;
    for(int i=0;i<k;i++){
      cin>>p>>q>>r;
      cout<<(r==0 ? cost[p][q] : times[p][q] ) <<endl;
    }
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int INF = 1 << 25;

int cost[100][100], time[100][100];

int main() {
	int n, m, query;

	while (cin >> n >> m , n) {
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				cost[i][j] = (i == j) ? 0 : INF;
				time[i][j] = (i == j) ? 0 : INF;
			}
		}

		while (n--) {
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[a - 1][b - 1] = cost[b - 1][a - 1] = c;
			time[a - 1][b - 1] = time[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++) {
					cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
					time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
				}
			}
		}

		cin >> query;
		while (query--) {
			int p, q, r;
			cin >> p >> q >> r;
			if (r == 0) {
				cout << cost[p - 1][q - 1] << endl;
			}
			else {
				cout << time[p - 1][q - 1] << endl;
			}
		}
	}
}