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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<28

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

struct edge
{
	int to;
	int cost;

	edge(int to,int cost) : to(to),cost(cost) {}
};

vector<edge> G[3001];
vector<edge> G2[3001];
ll d[3001];

void dijkstra(int s,int n,int type)
{
	priority_queue<P,vector<P>,greater<P> > que;
	fill(d,d+n,INF);
	d[s] = 0;
	que.push(P(0,s));

	while(!que.empty())
	{
		P p = que.top();
		que.pop();

		ll v = p.second;
		if(d[v] < p.first) continue;


		if(type == 0)
		{
			rep(i,G[v].size())
			{
				edge e = G[v][i];
				if(d[e.to] > d[v] + e.cost)
				{
					d[e.to] = d[v] + e.cost;
					que.push(P(d[e.to],e.to));
				}
			}
		}
		else
		{
			rep(i,G2[v].size())
			{
				edge e = G2[v][i];
				if(d[e.to] > d[v] + e.cost)
				{
					d[e.to] = d[v] + e.cost;
					que.push(P(d[e.to],e.to));
				}
			}
		}


	}
}

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

		rep(i,m)
		{
			G[i].clear();
			G2[i].clear();
		}

		rep(i,n)
		{
			int a,b,cost,time;
			cin >> a >> b >> cost >> time;
		
			a--;
			b--;

			G[a].push_back(edge(b,cost));
			G[b].push_back(edge(a,cost));

			G2[a].push_back(edge(b,time));
			G2[b].push_back(edge(a,time));
		}

		int k;
		cin >> k;

		rep(i,k)
		{
			int s,t,type;
			cin >> s >> t >> type;

			s--;
			t--;

			dijkstra(s,m,type);
			cout << d[t] << endl;
		}

	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#include<iostream>
#include<queue>
#define rep(i,a,b) for(i=a;i<b;i++)
#define F1 first.first
#define F2 first.second
#define F3 second
#define INF 2100000000
#define long long int int
using namespace std;
typedef pair<pair<int,int>,int> P1;
typedef pair<int,int> P2;
int main(void)
{
	int i,j,k,n,m,x,s,e;
	int a,b,cos,tim,st,en,way;
	int leng[1001],kami[1002];
	P1 glid1[6000],glid2[6000];
	P2 tori;
	while(cin>>n>>m,n&&m) {
		rep(i,0,n) {
			cin>>a>>b>>cos>>tim;
			glid1[i*2].F1=a,glid1[i*2].F2=b,glid1[i*2].F3=cos;
			glid2[i*2].F1=a,glid2[i*2].F2=b,glid2[i*2].F3=tim;
			glid1[i*2+1].F1=b,glid1[i*2+1].F2=a,glid1[i*2+1].F3=cos;
			glid2[i*2+1].F1=b,glid2[i*2+1].F2=a,glid2[i*2+1].F3=tim;
		}
		sort(glid1,glid1+m*2);sort(glid2,glid2+m*2);
		kami[1]=0;
		rep(i,1,m*2) {
			if(glid1[i].F1!=glid1[i-1].F1) {
				x=glid1[i].F1;
				kami[x]=i;
			}
		}
		kami[n+1]=m*2;
		priority_queue<P2,vector<P2>,greater<P2> > que;
		cin>>k;
		rep(i,0,k) {
			cin>>st>>en>>way;
			for(j=1;j<=n;j++) leng[j]=INF;
			while(!que.empty()) que.pop();
			que.push(P2(0,st));
			while(!que.empty()) {
				tori=que.top(),que.pop();
				a=tori.first,b=tori.second;
				if(a<leng[b]) {
					leng[b]=a;
					if(b==en) break;
					s=kami[b];e=kami[b+1];
					if(way==0)rep(j,s,e) que.push(P2(a+glid1[j].F3,glid1[j].F2));
					else rep(j,s,e) que.push(P2(a+glid2[j].F3,glid2[j].F2));
				}
			}
			cout<<leng[en]<<endl;
		}
	}
	
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <cstring>
#include <functional>

using namespace std;

#define REP(i,a,b) for((i)=(a);(i)<(int)(b);(i)++)
#define rep(i,n) REP(i,0,n)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

typedef pair<int,int> P;
const int INF = 1<<29;

struct edge{ int to, cost; };

vector<int> dijkstra(int m, int s, vector<vector<edge> >& G){
  int i;
  vector<int> d(m,INF);
  priority_queue<P,vector<P>,greater<P> > q;

  d[s] = 0;
  q.push(P(0,s));
  while(!q.empty()){
    P p = q.top(); q.pop();
    if(d[p.second] < p.first) continue;

    rep(i,G[p.second].size()){
      edge e = G[p.second][i];
      if(d[e.to] > d[p.second] + e.cost){
        d[e.to] = d[p.second] + e.cost;
        q.push(P(d[e.to], e.to));
      }
    }
  }

  return d;
}

int main(){
  int i,e,m,n;
  vector<vector<edge> > times,costs;

  while(scanf("%d %d",&e,&m)){
    if(e == 0 && m == 0) break;
    times.resize(m);
    costs.resize(m);

    while(e--){
      edge a,b;
      int from, to, cost, time;

      scanf("%d %d %d %d",&from,&to,&cost,&time);
      from--; to--;
      a.to = to; a.cost = cost;
      b.to = to; b.cost = time;
      times[from].push_back(b);
      costs[from].push_back(a);
    }

    scanf("%d",&n);
    while(n--){
      int s,g,bit;

      scanf("%d %d %d",&s,&g,&bit);
      s--; g--;
      if(bit == 0){
        vector<int> d = dijkstra(m,s,costs);
        printf("%d\n",d[g]);
      }
      if(bit == 1){
        vector<int> d = dijkstra(m,s,times);
        printf("%d\n",d[g]);
      }
    }
  }

  return 0;
}

C++: #include<iostream>
using namespace std;
#define INFTY (1<<21)
#define MAX 100
void floyd(int m, int C[MAX][MAX], int T[MAX][MAX]){
for(int k = 0;k<m;k++)
for(int i = 0;i<m;i++)
for(int j = 0;j<m;j++){
C[i][j] = min( C[i][j], C[i][k] + C[k][j] );
T[i][j] = min( T[i][j], T[i][k] + T[k][j] );
}
}
main(){
int n, m, a, b, c, t, k, q, C[MAX][MAX], T[MAX][MAX];
while( cin >> n >> m && !(n == 0 && m == 0) ){
for(int i = 0;i<m;i++)
for(int j = 0;j<m;j++) { C[i][j] = T[i][j] = INFTY; }
for(int i = 0;i<n;i++){
cin >> a >> b >> c >> t;
C[a-1][b-1] = C[b-1][a-1] = c;
T[a-1][b-1] = T[b-1][a-1] = t;
}
floyd(m, C, T);
cin >> k;
for(int i = 0;i<k;i++){
cin >> a >> b >> q;
if ( q ) cout << T[a-1][b-1] << endl;
else cout << C[a-1][b-1] << endl;
}
}
}