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<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);
      //cout<<"Node: "<<node[a-1].num<<"edge:"<<endl;
      //cout<<"Node: "<<node[b-1].num<<"edge:"<<endl;
    }

    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;
	//cout<<"n[p].time "<<n[p].time<<endl;

	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) {
	      //cout<<"Done!!"<<endl;
	    } else if(doneNode.time==-1 || n[i].time < doneNode.time) {
	      doneNode = n[i];
	      d=i;
	    }
	  }

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

	  //cout<<"doneNode :"<<doneNode.num<<" "<<doneNode.time<<" "<<doneNode.done<<endl;
	  n[d].done = 1;

	  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[doneNode.edge_to[i]-1].num<<" "<<n[doneNode.edge_to[i]-1].time<<endl;
	    }
	  }

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

      }else{

	n[p].cost = 0;
	//cout<<"n[p].time "<<n[p].cost<<endl;

	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) {
	      //cout<<"Done!!"<<endl;
	    } else if(doneNode.cost==-1 || n[i].cost < doneNode.cost) {
	      doneNode = n[i];
	      d=i;
	    }
	  }

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

	  //cout<<"doneNode :"<<doneNode.num<<" "<<doneNode.time<<" "<<doneNode.done<<endl;
	  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[doneNode.edge_to[i]-1].num<<" "<<n[doneNode.edge_to[i]-1].cost<<endl;
	    }
	  }

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

    }
  }
}

C++: #include<bits/stdc++.h>

using namespace std;

int N, M;
int DpCost[101][101], DpTime[101][101];
const int INF = 1e8;


void solve() {
	for (int k = 1; k <= M; ++k) {
		for (int i = 1; i <= M; ++i) {
			for (int j = 1; j <= M; ++j) {
				if (i == j) {
					DpCost[i][j] = DpTime[i][j] = 0;
					continue;
				}
				DpCost[i][j] = min(DpCost[i][j], DpCost[i][k] + DpCost[k][j]);
				DpTime[i][j] = min(DpTime[i][j], DpTime[i][k] + DpTime[k][j]);
			}
		}
	}
}

int main() {
	int a, b, cost, time, k, r;

	while (cin >> N >> M, N) {
		for (int i = 1; i <= M; ++i) {
			fill(DpCost[i], DpCost[i] + 101, INF);
			fill(DpTime[i], DpTime[i] + 101, INF);
		}
		for (int i = 0; i < N; ++i) {
			cin >> a >> b >> cost >> time;
			DpCost[a][b] = DpCost[b][a] = cost;
			DpTime[a][b] = DpTime[b][a] = time;
		}
		solve();
		cin >> k;
		for (int i = 0; i < k; ++i) {
			cin >> a >> b >> r;
			if (r == 0) {
				cout << DpCost[a][b] << endl;
			}
			else {
				cout << DpTime[a][b] << endl;
			}
		}
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
vector<vector<char> >tmp;
vector<vector<int> >scr;
int main(){
	int n,m;
	while(cin>>n>>m,n||m){
		vector<int>d(m);
		vector<vi >cost(m,vector<int>(m));
		vector<vi >tim(m,vector<int>(m));
		vector<bool>used(m);
		rep(i,m)rep(j,m){cost[i][j]=inf;tim[i][j]=inf;}
		rep(i,n){
			int a,b,c,d;
			cin>>a>>b>>c>>d;
			a--;b--;
			cost[a][b]=cost[b][a]=c;
			tim[a][b]=tim[b][a]=d;
		}
		cin>>n;
		rep(i,n){
			int a,b,c;
			cin>>a>>b>>c;
			a--;b--;
			rep(j,m)d[j]=inf;
			rep(j,m)used[j]=false;
			if(c){
				d[a]=0;
				while(1){
					int v=-1;
					rep(j,m)if(!used[j]&&(v==-1||d[v]>d[j]))v=j;
					if(v==-1)break;
					used[v]=true;
					rep(j,m)d[j]=min(d[j],d[v]+tim[v][j]);
				}
			}else{
				d[a]=0;
				while(1){
					int v=-1;
					rep(j,m)if(!used[j]&&(v==-1||d[v]>d[j]))v=j;
					if(v==-1)break;
					used[v]=true;
					rep(j,m)d[j]=min(d[j],d[v]+cost[v][j]);
				}
			}
			cout<<d[b]<<endl;
		}
	}
}