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<cstring>
#include<vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define INF 300000

struct E
{
    int a,b,c,t;
    E(){};
    E(int a,int b,int c,int t):a(a),b(b),c(c),t(t){};
};
//E cs[700];
unsigned int d[150];

int main()
{
    int n,m,k,p,q,r;
    while(cin >> n >> m)
    {
        if(n==0 && m==0)break;
        vector<E> cs;   
        int ta,tb,tc,tt;
        rep(i,n)
        {
            cin >> ta>>tb>>tc >>tt;
            cs.push_back( E(ta,tb,tc,tt));
            cs.push_back( E(tb,ta,tc,tt));
        }

        cin >> k;
        rep(i,k)
        {
            memset(d,INF,150*sizeof(int));
            cin >> p >> q >>r;
            d[p] = 0;

            bool update = false;
            if(r==0)while(true)
            {
                update = false;
                rep(j,n*2)
                {
                   if(d[cs[j].a] != INF && d[cs[j].b] > d[cs[j].a] + cs[j].c)
                   {
                       update = true;
                       d[cs[j].b] =  d[cs[j].a] + cs[j].c;
                   }
                }
                if(!update) break;
            }
            if(r==1)while(true)
            {
                update = false;
                rep(j,n*2)
                {
                   if(d[cs[j].a] != INF && d[cs[j].b] > d[cs[j].a] + cs[j].t)
                   {
                       update = true;
                       d[cs[j].b] =  d[cs[j].a] + cs[j].t;
                   }
                }
                if(!update) break;
            }
            cout << d[q] << endl;
        }
        
    }

    return 0;
}

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];
inline 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>
#include<algorithm>
#include<climits>
using namespace std;
const int INF = INT_MAX/3;
const int MAXN = 2000;
const int TIME = 1,COST = 0;
int graph[2][MAXN][MAXN];
int n,m,a,b,c,t;
void solve();
void init(){
  for(int i = 0;i < m;i++)
    for(int j = 0;j < m;j++)
      graph[0][i][j] =graph[0][j][i] = graph[1][j][i] = graph[1][i][j] = INF;
}
int main(){
  cin >> n >> m;
  init();
  for(int i = 0;i < n;i++){
    cin >> a >> b >> c >> t;
    graph[TIME][a-1][b-1] = graph[TIME][b-1][a-1] = t;
    graph[COST][a-1][b-1] = graph[TIME][b-1][a-1] = c;
  }

  solve();
  
  int X;
  cin >> X;
  for(int i = 0;i < X;i++){
    cin >> a >> b >> c;
    cout << graph[c][a-1][b-1] << endl;
  }
  return 0;
}

void solve(){
  for(int k = 0;k < m;k++){
    for(int i = 0;i < m;i++){
      for(int j = 0;j < m;j++){
	graph[0][i][j] = min(graph[0][i][j],graph[0][i][k] + graph[0][k][j]);
	graph[1][i][j] = min(graph[1][i][j],graph[1][i][k] + graph[1][k][j]);
      }
    }
  }
}

C++: #include <iostream>

using namespace std;

const int INF = 1000000;
int C[101][101], T[101][101];

int main() {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (!n && !m) return 0;
    for (int i = 1; i <= m; i++)
      for (int j = 1; j <= m; j++)
        C[i][j] = T[i][j] = INF;
    for (int i = 0; i < n; i++) {
      int a, b, cost, time;
      cin >> a >> b >> cost >> time;
      C[a][b] = C[b][a] = cost;
      T[a][b] = T[b][a] = time;
    }
    for (int k = 1; k <= m; k++)
      for (int i = 1; i <= m; i++)
        for (int j = 1; j <= m; j++) {
          C[i][j] = min(C[i][j], C[i][k] + C[k][j]);
          T[i][j] = min(T[i][j], T[i][k] + T[k][j]);
        }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      cout << (!r ? C[p][q] : T[p][q]) << endl;
    }
  }
  return 0;
}

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

using namespace std;

int cost[100][100];

int main()
{
	for(;;){
		int t,tt,ttt,tttt;
		int time[100][100];
		
		//n:f[^Ì m:wÌ
		int n, m;
		
		//k:â¢í¹Ì
		int k;
		
		cin >> n >> m;
		
		if(n == 0 && m == 0){
			break;
		}
		
		for(int i = 0; i < 100; i++){
			for(int j = 0; j < 100; j++){
				if(i!=j)
				{
					time[i][j] = 10000000;
					cost[i][j] = 10000000;
				}
				else{
					cost[i][j]=time[i][j]=0;
				}
			}
		}
		
		for(int i = 0; i < n; i++){
			cin >> t >> tt >> ttt >> tttt;
			t--;
			tt--;
			cost[t][tt]=ttt;
			time[t][tt]=tttt;
			cost[tt][t]=ttt;
			time[tt][t]=tttt;
		}
		
		cin >> k;
		
		for(int i = 0; i < k; i++){
			cin >> t >> tt >> ttt;
			
			for(int f = 0; f < m; f++){
				for(int fd = 0; fd < m; fd++){
					for(int fds = 0; fds < m; fds++){
						if(ttt == 0){
							cost[fd][fds] = min(cost[fd][fds] , cost[fd][f] + cost[f][fds]);
						}
						else{
							time[fd][fds] = min(time[fd][fds] , time[fd][f] + time[f][fds]);
						}
					}
				}
			}
			if(ttt == 0){
				cout << cost[t-1][tt-1] << endl;
			}
			else{
				cout << time[t-1][tt-1] << endl;
			}
		}
	}
	
}