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<vector>
using namespace std;

struct Node{
  int num;

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

  bool done;
  int cost;
};

int dijkstra (vector<Node> g,int start,int goal){
  int d;
  g[start].cost=0;

  while(1){
    d=-1;

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

    if(d==-1) break;
    g[d].done=true;

    for(int i=0; i< g[d].edge_to.size(); i++){
      int to = g[d].edge_to[i];
      int cost = g[d].cost + g[d].edge_cost[i];
      if(g[to].cost < 0 || cost < g[to].cost)
	g[to].cost = cost;
    }
  }

  return g[goal].cost;
}

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

    vector<Node> time_graph,cost_graph;
    time_graph.reserve(m+1);
    time_graph.resize(m+1);
    cost_graph.reserve(m+1);
    cost_graph.resize(m+1);

    Node no;
    for(int i=1;i<=m;i++){
      no.num = i;
      no.done = false;
      no.cost = -1;
      time_graph[i] = no;
      cost_graph[i] = no;
    }


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

    cin>>n;
    while(n--){
      cin>>p>>q>>r;

      vector<Node> n;
      n.reserve(time_graph.size());
      n.resize(time_graph.size());

      if(r==1){
	n.assign(time_graph.begin(),time_graph.end());
	cout<<dijkstra(n,p,q)<<endl;
      }else{
	n.assign(cost_graph.begin(),cost_graph.end());
	cout<<dijkstra(n,p,q)<<endl;
      }

    }
  }
}

C++: #include <climits>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define min(a, b) (a > b ? b : a)

int main() {
    int m, n;
    while (std::cin >> m >> n && m != 0 && n != 0) {
        int tb[n][n][2];

        rep(i, n) rep(j, n) tb[i][j][0] = tb[i][j][1] = tb[j][i][0] =
            tb[j][i][1] = (i == j ? 0 : INT_MAX / 3);
        rep(i, m) {
            int f, t, c, tm;
            std::cin >> f >> t >> c >> tm;
            f--;
            t--;
            tb[f][t][0] = tb[t][f][0] = c;
            tb[f][t][1] = tb[t][f][1] = tm;
        }
        rep(x, n) rep(y, n) rep(z, n) rep(i, 2) tb[z][y][i] = tb[y][z][i] =
            min(tb[y][z][i], tb[y][x][i] + tb[x][z][i]);

        int k;
        std::cin >> k;
        rep(i, k) {
            int s, e, q;
            std::cin >> s >> e >> q;
            s--;
            e--;
            std::cout << tb[s][e][q] << std::endl;
        }
    }
}

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

int main(){
  while(true){
    int n,m;
    cin >> n >> m;
    if(n==0 && m==0){
      break;
    }

    int INF=100000;
    int way_co[m][m];
    int way_ti[m][m];
    for(int i=0; i<m; i++){
      for(int j=0; j<m; j++){
        way_co[i][j] = INF;
        way_ti[i][j] = INF;
        if(i==j){
          way_co[i][j] = 0;
          way_ti[i][j] = 0;
        }
      }
    }

    int a,b,cost,tim;
    for(int i=0; i<n; i++){
      cin >> a >> b >> cost >> tim;
      way_co[a-1][b-1] = cost;
      way_ti[a-1][b-1] = tim;
    }

    int d_co[m][m];
    int d_ti[m][m];
    for(int i=0; i<m; i++){
      for(int j=0; j<m; j++){
        d_co[i][j] = way_co[i][j];
        d_ti[i][j] = way_ti[i][j];
      }
    }

    for(int k=0; k<m; k++){
      for(int i=0; i<m; i++){
        for(int j=0; j<m; j++){
          if(d_co[i][j] > d_co[i][k] + d_co[k][j]){
            d_co[i][j] = d_co[i][k] + d_co[k][j];
          }
          if(d_ti[i][j] > d_ti[i][k] + d_ti[k][j]){
            d_ti[i][j] = d_ti[i][k] + d_ti[k][j];
          }
        }
      }
    }
    int y;
    cin >> y;
    for(int i=0; i<y; i++){
      int a,b,c;
      cin >> a >> b >> c;
      if(c==0){
        cout << d_co[a-1][b-1] << endl;
      }else{
        cout << d_ti[a-1][b-1] << endl;
      }
    }
  }
  return 0;
}

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

const double inf = (int)1e8;

int cost[2][100][100];
int d[100];
bool used[100];
int n, m;

int dijkstra(int s, int e, int req){
	fill(d, d+m, inf);
	fill(used, used+m, false);
	d[s] = 0;

	while(true){
//		cout << "hogehoge" << endl;

		int v = -1;

		for(int u =  0; u < m; u++){
			if(!used[u] && (v == -1 || d[u] < d[v])) v = u;
		}

		if(v == -1) break;

		used[v] = true;

		for(int u = 0; u < m; u++){
			d[u] = min(d[u], d[v] + cost[req][v][u]);
		}
	}
	return d[e];
}

//#define DEBUG

int main(){
	while(cin >> n >> m){
		if(n == 0 && m == 0) break;
		for(int i = 0; i < 2; i++){
			for(int j = 0; j < m; j++)
				for(int k = 0; k < m; k++){
					cost[i][j][k] = inf;
				}
		}
		for(int i = 0; i < n; i++){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			a--; b--;
			cost[0][a][b] = c;
			cost[0][b][a] = c;

			cost[1][a][b] = t;
			cost[1][b][a] = t;
		}

#ifdef DEBUG
	//test
		for(int i	= 0; i < m; i++){
			for(int j = 0; j < m; j++){
				cout << cost[0][i][j] << " ";
			}
		}
#endif

		int  num;
		cin >> num;
		for(int i = 0; i < num; i++){
			int p, q, req;
			cin >> p >> q >> req;
			p--; q--;
			cout << dijkstra(p, q, req) << endl;
		}
	}
}