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
			c_matrix[b-1][a-1] = c
			t_matrix[a-1][b-1] = t
			t_matrix[b-1][a-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<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
void f(int c[][101],int t[][101],int a[],int p,int e,int n,int b,int v){
  int i;  
  if(p==e){
    if(a[p]==-1||a[p]>v)
      a[p]=v;
  }else if(a[p]==-1||a[p]>v){
    a[p]=v;
    for(i=1;i<n+1;i++){
      if(b){
	if(t[p][i]!=-1)
	  f(c,t,a,i,e,n,b,v+t[p][i]);
      }else{
	if(c[p][i]!=-1)
	  f(c,t,a,i,e,n,b,v+c[p][i]);
      }
    }
  }
  return;
}
int main(){
  int i;
  int c[101][101],t[101][101],a[101],m,n,o;
  int *s;
  for(;;){
    cin>>m>>n;
    if(m+n==0)
      break;
    memset(c,-1,sizeof(c));
    memset(t,-1,sizeof(t));
    for(i=0;i<m;i++){
      int p,q,r,s;
      cin>>p>>q>>r>>s;
      if(c[p][q]==-1||c[p][q]>r){
	c[p][q]=r;
	c[q][p]=r;
      }
      if(t[p][q]==-1||t[p][q]>s){
	t[p][q]=s;
	t[q][p]=s;
      }
    }
    cin>>o;
    s=new int[o];
    for(i=0;i<o;i++){
      int p,q,r;
      cin>>p>>q>>r;
      memset(a,-1,sizeof(a));
      f(c,t,a,p,q,n,r,0);
      s[i]=a[q];
    }
    for(i=0;i<o;i++)
      cout<<s[i]<<endl;
    delete[]s;
  }
  return 0;
}

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

#define INF 10000000

// 0: cost , 1 :time
int d[2][101][101];


int main() {
    int n, m, a, b, cost, time, k, p, q, r, i, j;

    cin >> n >> m;
    for (i = 1; i <= m; i++) {
        for (j = 1; j <= m; j++) {
            d[0][i][j] = d[1][i][j] = INF;
        }
    }
    for (i = 0; i < n; i++) {
        cin >> a >> b >> cost >> time;
        d[0][a][b] = d[0][b][a] = cost;
        d[1][a][b] = d[1][b][a] = time;
    }

    for (k = 1; k <= m; k++) {
        for (i = 1; i <= m; i++) {
            for (j = 1; j <= m; j++) {
                for (r = 0; r < 2; r++) {
                    if (d[r][i][j] > d[r][i][k] + d[r][k][j]) {
                        d[r][j][i] = d[r][i][j] = d[r][i][k] + d[r][k][j];
                    }
                }
            }
        }
    }

    cin >> k;
    while (k--) {
        cin >> p >> q >> r;
        cout << d[r][p][q] << endl;
    }

    return 0;
}

C++: #define INF 100000;

#include<iostream>
#include<vector>
using namespace std;

struct Node{
  int num;

  vector<int> edge_to;
  vector<int> edge_cost;
  vector<int> edge_time;

  bool done;
  int cost;
  int time;
};

int main(){
  for(;;){
    int n,m,a,b,c,t,p,q,r;
    cin>>n>>m;
    if(n==0&&m==0)break;
    vector<Node> node;

    for(int i=1;i<=m;i++){
      Node no;
      no.num=i;
      no.done=false;
      no.cost=-1;
      no.time=-1;
      node.push_back(no);
    }


    while(n--){
      cin>>a>>b>>c>>t;
      node[a-1].edge_to.push_back(b);
      node[b-1].edge_to.push_back(a);
      node[a-1].edge_cost.push_back(c);
      node[b-1].edge_cost.push_back(c);
      node[a-1].edge_time.push_back(t);
      node[b-1].edge_time.push_back(t);
    }

    cin>>n;
    while(n--){
      cin>>p>>q>>r;
      vector<Node> n(node.size());
      copy(node.begin(),node.end(),n.begin());
      p--;
      q--;
      if(r){

	n[p].time = 0;
	while(1){

	  Node doneNode;
	  int d=-1;
	  doneNode.time = -1;

	  for(int i=0;i<n.size();++i){
	    if( n[i].done || n[i].time < 0) {
	    } else if(doneNode.time==-1 || n[i].time < doneNode.time) {
	      doneNode = n[i];
	      d=i;
	    }
	  }

	  if ( doneNode.time == -1 ) {
	    break;
	  }

	  n[d].done = true;

	  for(int i=0;i<doneNode.edge_to.size();++i) {
	    int ttime= doneNode.time + doneNode.edge_time[i];
	    if(n[doneNode.edge_to[i]-1].time < 0 || ttime < n[doneNode.edge_to[i]-1].time){
	      n[doneNode.edge_to[i]-1].time = ttime;
	    }
	  }

	}
	cout<<n[q].time<<endl;

      }else{

	n[p].cost = 0;

	while(1){

	  Node doneNode;
	  int d=-1;
	  doneNode.cost = -1;

	  for(int i=0;i<n.size();++i){
	    if( n[i].done || n[i].cost < 0) {
	    } else if(doneNode.cost==-1 || n[i].cost < doneNode.cost) {
	      doneNode = n[i];
	      d=i;
	    }
	  }

	  if ( doneNode.cost == -1 ) {
	    break;
	  }

	  n[d].done = 1;

	  for(int i=0;i<doneNode.edge_to.size();++i) {
	    int tcost= doneNode.cost + doneNode.edge_cost[i];
	    if(n[doneNode.edge_to[i]-1].cost < 0 || tcost < n[doneNode.edge_to[i]-1].cost){
	      n[doneNode.edge_to[i]-1].cost = tcost;
	    }
	  }

	}
	cout<<n[q].cost<<endl;
      }

    }
  }
}