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>

#define INF 1001;
using namespace std;


int main(){
  int n,m;
  while(1){
    cin>>n>>m;
    if(n==0&&m==0)break;
    int s_tim[m][m];
    int c_money[m][m];
    int a,b,cost,tim;

    for(int i=0;i<m;i++){
      for(int j=0;j<n;j++){
        s_tim[i][j]=INF;
        c_money[i][j]=INF;
      }
    }
    for(int i=0;i<m;i++){
      s_tim[i][i]=0;
      c_money[i][i]=0;
    }
    for(int i=0;i<n;i++){
      cin>>a>>b>>cost>>tim;
      s_tim[a-1][b-1]=tim;s_tim[b-1][a-1]=tim;
      c_money[a-1][b-1]=cost;c_money[b-1][a-1]=cost;
    }

    for(int k=0;k<m;k++){
      for(int i=0;i<m;i++){
        for(int j=0;j<m;j++){
          if(s_tim[i][j]>s_tim[i][k]+s_tim[k][j]){
            s_tim[i][j]=s_tim[i][k]+s_tim[k][j];
          }
          if(c_money[i][j]>c_money[i][k]+c_money[k][j]){
            c_money[i][j]=c_money[i][k]+c_money[k][j];
          }
        }
      }
    }

    int contacts,p,q,r;
    cin>>contacts;

    for(int i=0;i<contacts;i++){
      cin>>p>>q>>r;
      if(r){cout<<s_tim[p-1][q-1];}
      else{ cout<<c_money[p-1][q-1];}

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

void open(int max[101],int name[101][101][2],int m,int basic,int r,int to,vector<int> got[101])
{
	unsigned int i;
	for(i=0;i<got[basic].size();i++)
	{
		if(max[got[basic][i]] == -1 || max[got[basic][i]] > max[basic] + name[basic][got[basic][i]][r])
		{
			max[got[basic][i]] = max[basic] + name[basic][got[basic][i]][r];
			if(got[basic][i] != to)
				open(max,name,m,got[basic][i],r,to,got);
		}
	}
}

int main(void)
{
	while(1)
	{
		int n,m;
		int a,b,cost,time;
		int k;
		int p,q,r;
		int mintime;
		int mincost;
		int i,j,x,y;

		int name[101][101][2] = {};//OÍ¿à
		int max[101]={};
		vector<int> got[101];

		cin >> n >> m;

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

		for(i=0;i<n;i++)
		{
			cin >> a >> b >> cost >> time;
			name[a][b][0] = cost;
			name[b][a][0] = cost;
			name[a][b][1] = time;
			name[b][a][1] = time;
			got[a].push_back(b);
			got[b].push_back(a);
		}

		cin >> k;

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

			fill(max , max+101 , -1);

			max[p] = 0;

			open(max,name,m,p,r,q,got);

			cout << max[q] << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<s;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int,int> pii;
#define MAX 99999999
signed main(){
while(1){
	int n,m;
	cin>>n>>m;
    if(!(n+m))break;
	vector<vector<pii>>v(n+1,vector<pii>(n+1,pii(INF,INF)));
	rep(i,n+1)v[i][i]=pii(0,0?\);
	rep(i,n){
		pii temp;
		int s,g;
		cin>>s>>g>>temp.first>>temp.second;
        v[--s][--g]=temp;
	}
    rep(k,m){
        rep(i,m){
            rep(j,m){
                v[i][j].first=min(v[i][k].first+v[k][j].first,v[i][j].first);
                v[i][j].second=min(v[i][k].second+v[k][j].second,v[i][j].second);

            }
        }
    }
    int k; cin>>k;
    rep(i,k){
        int s,g,r; cin>>s>>g>>r;
        if(!r)cout<<v[--s][--g].first<<endl;
        else cout<<v[--s][--g].second<<endl;
    }
    
    }
}

C++: #include<iostream>

#define INF 1000000

std::pair<int,int>list[101][101];
int cost[101];
bool used[101];
int n,m,k;

int dijkstra(int s,int g,int r){
	used[s]=true;
	if(used[g])return cost[g];
	for(int i=1;i<=m;i++){
		if(r==0){
			if(list[s][i].first!=INF){
				if(cost[i]>cost[s]+list[s][i].first){
					cost[i]=cost[s]+list[s][i].first;
				}
			}
		}else if(r==1){
			if(list[s][i].second!=INF){
				if(cost[i]>cost[s]+list[s][i].second){
					cost[i]=cost[s]+list[s][i].second;
				}
			}
		}
	}
	int min=0;
	for(int i=1;i<=m;i++){
		if(!used[i]){
			if(cost[min]>cost[i])min=i;
		}
	}
	dijkstra(min,g,r);
}

int main(){
	while(std::cin>>n>>m){
		if(n==0&&m==0)break;
		for(int i=0;i<=m;i++){
			for(int j=0;j<=m;j++){
				list[i][j].first=INF;
				list[i][j].second=INF;
			}
		}
		for(int i=0;i<n;i++){
			int a,b,c,d;
			std::cin>>a>>b>>c>>d;
			list[a][b].first=c;list[a][b].second=d;
			list[b][a].first=c;list[b][a].second=d;
		}
		std::cin>>k;
		for(int i=0;i<k;i++){
			for(int j=0;j<=m;j++)cost[j]=INF,used[j]=false;
			int p,q,r;
			std::cin>>p>>q>>r;
			cost[p]=0;
			std::cout<<dijkstra(p,q,r)<<std::endl;
		}
	}
	return 0;
}

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

int main() {
	int i,j,k,m,n,a,b,c,t,co[100][100],ti[100][100];
	while(cin >> n >> m) {
		if (n+m==0) break;
		for (i=0;i<m;i++) for (j=0;j<m;j++) { co[i][j]=ti[i][j]=999999;}
		for (i=0;i<n;i++) {
			cin >> a >> b >> c >> t; a--; b--;
            co[a][b]=co[b][a]=c; ti[a][b]=ti[b][a]=t;
		}
		for (k=0;k<m;k++) for (i=0;i<m;i++) for (j=0;j<m;j++) {
			co[i][j]=min(co[i][j],co[i][k]+co[k][j]);
			ti[i][j]=min(ti[i][j],ti[i][k]+ti[k][j]);
		}
		cin >> k;
		for (i=0;i<k;i++) {
			cin >> a >> b >> c; a--; b--;
			if (c==0) cout << co[a][b] << endl; else cout << ti[a][b] << endl;
		}
	}
	return 0;
}