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 <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <string>
#include <queue>

const int INF = 1<<27;
using namespace std;
typedef pair<int,int> P;
string IntToString(int);
int StringToInt(string);
struct edje{
public:
  int from;
  int to;
  int cost;
  edje(int f,int t,int c){
    from = f;
    to = t;
    cost = c;
  }
};

void bellmanFord(vector<int> &map,vector<edje> &cost){
  for(int i = 0;i < map.size()-1;i++){
    for(int j = 0;j < cost.size();j++){
      map[cost[j].to] = min(map[cost[j].to],map[cost[j].from] + cost[j].cost);
    }
  }
}
void dijkstra(int s,vector<vector<P> >& cost,vector<int>& map){
  map[s] = 0;
  
  priority_queue<P,vector<P>,greater<P> > que;
  que.push(P(0,s));
  while(!que.empty()){
    P p = que.top();
    que.pop();
    int v = p.second;
    if(map[v] < p.first)continue;//
    for(int i = 0;i < cost[v].size();i++){
      if(map[cost[v][i].first] > map[v] + cost[v][i].second){
	map[cost[v][i].first] = map[v] + cost[v][i].second;
	que.push(P(map[cost[v][i].first],cost[v][i].first));
      }
    }

  }
}
int main(){
  while(1){
    int n,m;
    cin >> n >> m;
    if(n == 0 && m == 0)break;
    vector<vector<pair<int,int> > > cost(m);
    vector<vector<pair<int,int> > > time(m);
    for(int i = 0;i < n;i++){
      int a,b,c,t;
      cin >> a >> b >> c >> t;
      cost[a-1].push_back(pair<int,int>(b-1,c));
      cost[b-1].push_back(pair<int,int>(a-1,c));
      time[a-1].push_back(pair<int,int>(b-1,t));
      time[b-1].push_back(pair<int,int>(a-1,t));
    }
    int k;
    cin >> k;
    for(int i = 0;i < k;i++){
      int s,g,q;
      cin >> s >> g >> q;
      vector<vector<pair<int,int> > > tmp;
      if(q == 0) tmp = vector<vector<pair<int,int> > >(cost);
      else if(q == 1) tmp = vector<vector<pair<int,int> > >(time);
      vector<int> map(m,INF);
      //      map[s-1] = 0;
      dijkstra(s-1,tmp,map);
      cout << map[g-1] << endl;
      

    }
  }
  return 0;   
}
string IntToString(int number){
  stringstream ss;
  ss << number;
  return ss.str();
}
int StringToInt(string number){
  stringstream ss;
  ss << number;
  int numInt;
  ss >> numInt;
  return numInt;
  
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 24;
int c[200][200];
int t[200][200];
int cost[200][200];
int tim[200][200];

int main(){
  int n, m, K;
  int p, q, r;
  while(cin >> n >> m && (n || m)){
    fill(&cost[0][0], &cost[199][199] + 1, INF);
    fill(&tim[0][0], &tim[199][199] + 1, INF);
    fill(&c[0][0], &c[199][199] + 1, INF);
    fill(&t[0][0], &t[199][199] + 1, INF);
    for(int i = 1; i <= m; i++){
      cost[i][i] = tim[i][i] = 0;
      c[i][i] = t[i][i] = 0;
    }
    for(int i = 0; i < n; i++){
      int a, b;
      cin >> a >> b;
      cin >> c[a][b] >> t[a][b];
      c[b][a] = c[a][b];
      t[b][a] = t[a][b];
    }
    for(int k = 1; k <= m; k++){
      for(int i = 1; i <= m; i++){
	for(int j = 1; j <= m; j++){
	  if(cost[i][j] > cost[i][k] + c[k][j]){
	    cost[i][j] = cost[i][k] + c[k][j];
	  }
	  if(tim[i][j] > tim[i][k] + t[k][j]){
	    tim[i][j] = tim[i][k] + t[k][j];
	  }
	}
      }
    }
    cin >> K;    
    for(int i = 0; i < K; i++){
      cin >> p >> q >> r;
      if(!r){
	cout << cost[p][q] << endl;
      }else{
	cout << tim[p][q] << endl;
      }
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <queue>

#define INF 100000000

using namespace std;

int dijk(vector<vector<pair<int,int> > > &graph, int start, int goal){
        int total;
        priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;
        que.push( make_pair(0,start) );
        vector<int> cost(graph.size(), INF);
        while( !que.empty() ){
                pair<int,int> tmp = que.top(); 
                que.pop();
                if( cost[ tmp.second ] < INF ) continue;

        //      cout << tmp.second << "  " << tmp.first << "  " << endl;
                cost[tmp.second] = tmp.first;

                if( tmp.second == goal ) break;
                for(int i=0; i < graph[tmp.second].size(); i++){
                        if( cost[ graph[tmp.second][i].first ] == INF )
                                que.push( make_pair( tmp.first + graph[tmp.second][i].second, graph[tmp.second][i].first ) );
                }
        }
        return cost[goal];
}


int main(){
        int n, m, a, b, cost, time;

        while( cin >> n >> m , n && m ){
                vector<vector<pair<int, int> > > graph(m);
                vector<vector<pair<int, int> > > gtime(m);

                for(int i=0; i<n; i++){
                        cin >> a >> b >> cost >> time;
                        --a; --b;
                        graph[a].push_back( make_pair(b, cost) );
                        graph[b].push_back( make_pair(a, cost) );
                        gtime[a].push_back( make_pair(b, time) );
                        gtime[b].push_back( make_pair(a, time) );
                }

                int k;

                cin >> k;
                for(int i=0; i<k; i++){
                        int p, q, r;
                        cin >> p >> q >> r;
                        p--; q--;
                        if( r ){
                                cout << dijk( gtime, p, q ) << endl;
                        }else{
                                cout << dijk( graph, p, q ) << endl;
                        }
                }
        }

        return 0;
}