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++: #define INF 100000;

#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_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<iostream>
using namespace std;
int main(){
  const int MAX = 1e5;
  for(int n,m;cin>>n>>m,n;){
    int cost[m][m];
    int time[m][m];
    for(int i=0;i<m;i++){
      for(int j=0;j<m;j++){
	cost[i][j] = MAX;
	time[i][j] = MAX;
      }
    }
    int a,b,c,t;
    for(int i=0;i<n;i++){
      cin>>a>>b>>c>>t;
      cost[a-1][b-1] = c;
      cost[b-1][a-1] = c;
      time[a-1][b-1] = t;
      time[b-1][a-1] = t;
    }
    for(int k=0;k<m;k++){
      for(int i=0;i<n;i++){
	for(int j=0;j<n;j++){
	  if(cost[i][j]>cost[i][k]+cost[k][j])
	    cost[i][j] = cost[i][k]+cost[k][j];
	  if(time[i][j]>time[i][k]+time[k][j])
	    time[i][j] = time[i][k]+time[k][j];
	}
      }
    }
    int l,r;
    cin>>l;
    for(int i=0;i<l;i++){
      cin>>a>>b>>r;
      if(r)
	cout<<time[a-1][b-1]<<endl;
      else
	cout<<cost[a-1][b-1]<<endl;      
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
typedef long long ll;
using namespace std;
const int MAX = 101;


struct E{
	E(int from, int to, int cost, int time):from(from), to(to){
		ct[0] = cost;
		ct[1] = time;
	}
	int from, to;
	int ct[2];	//ct[0]がcost、ct[1]がtime
};

void bf(int s, int e, int mode, vector<E> &tb, int *d){
	d[s] = 0;
	while(1){
		bool update = false;
		for(int i = 0; i < tb.size(); i++){
			if(d[tb[i].to] > d[tb[i].from] + tb[i].ct[mode]){
				d[tb[i].to] = d[tb[i].from] + tb[i].ct[mode];
				update = true;
			}
		}
		if(!update) break;
	}
}

int main(){
	int n, m;	//nが線路数、mが駅数
	while(cin >> n >> m && n|m){
		vector<E> tb;
		for(int i = 0; i < n; i++){
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			tb.push_back(E(a, b, cost, time));
			tb.push_back(E(b, a, cost, time));
		}

		int minv[101];

		int k;
		cin >> k;
		for(int i = 0; i < k; i++){
			int s, e, mode;
			cin >> s >> e >> mode;

			fill(minv, minv+101, 1<<29);
			bf(s, e, mode, tb, minv);

			cout << minv[e] << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#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 it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
const double inf=1e8;
using namespace std;
int main(){
  int a,b;
  while(cin>>a>>b,a||b){
    int d1[101][101];
    int d2[101][101];
    rep(i,101)rep(j,101){
      d1[i][j]=inf;
      d2[i][j]=inf;
    }
    rep(i,a){
      int c,d,e,f;
      cin>>c>>d>>e>>f;
      c--;d--;
      d1[c][d]=e;d1[d][c]=e;
      d2[c][d]=f;d2[d][c]=f;
    }
    rep(k,b)rep(i,b)rep(j,b){
      d1[i][j]=min(d1[i][j],d1[i][k]+d1[k][j]);
      d2[i][j]=min(d2[i][j],d2[i][k]+d2[k][j]);
    }
    int n;cin>>n;
    rep(i,n){
      int g,h,l;cin>>g>>h>>l;
      g--;h--;
      if(l)cout<<d2[g][h]<<endl;
      else cout<<d1[g][h]<<endl;
    }
  }
}