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

#define M 101
#define INF 100000003

#define min(a,b) (a<b?a:b)

int main() {
	int n, m, a, b, c, t, pn, p, q, r;
	int dc[M][M], dt[M][M];
	while(1) {
		cin >> n >> m;
		if(!n&&!m) break;
		for(int i=1; i<=m; ++i) {
			for(int j=1; j<=m; ++j) {
				dc[i][j] = dt[i][j] = INF;
			}
		}
		for(int i=0; i<n; ++i) {
			cin >> a >> b >> c >> t;
			dc[a][b] = dc[b][a] = c;
			dt[a][b] = dt[b][a] = t;
		}
		for(int k=1; k<=m; ++k) {
			for(int i=1; i<=m; ++i) {
				for(int j=1; j<=m; ++j) {
					dc[i][j] = min(dc[i][j], dc[i][k]+dc[k][j]);
					dt[i][j] = min(dt[i][j], dt[i][k]+dt[k][j]);
				}
			}
		}
		cin >> pn;
		for(int i=0; i<pn; ++i) {
			cin >> p >> q >> r;
			cout << (!r ? dc[p][q] : dt[p][q]) << endl;
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INF 1<<29
int V;
vector<vector<vector<int> > > G;
int d[101];
bool used[101];

void dijkstra(int s,int r){
  memset(used,false,sizeof(used));
  fill(d,d+101,INF);
  d[s]=0;
  while(1){
    int id=-1;
    for(int i=1;i<=V;++i)
      if(!used[i] && (id==-1||d[id]>d[i]))
        id=i;
    if(id==-1) break;
    used[id]=true;
    for(int i=1;i<=V;++i)
      if(r)
        d[i]=min(d[i],d[id]+G[id][i][1]);
      else
        d[i]=min(d[i],d[id]+G[id][i][0]);
  }
}
int main(){
  int n;
  while(cin>>n>>V,n){
    G=vector<vector<vector<int>>>(V+1,vector<vector<int>>(V+1,vector<int>(2,INF)));
    for(int i=0;i<n;++i){
      int a,b,c,t; cin>>a>>b>>c>>t;
      G[a][b][0]=G[b][a][0]=c;
      G[a][b][1]=G[b][a][1]=t;
    }
    int k; cin>>k;
    for(int i=0;i<k;++i){
      int p,q,r; cin>>p>>q>>r;
      dijkstra(p,r);
      cout<<d[q]<<endl;
    }
    
  }
  
  return 0;
}

C++: #include <stdio.h>
#include <limits.h>

const int INF = INT_MAX;

int main(void)
{
	int n, m, a, b, k, t;
	int tcost[105][105], mcost[105][105], node[105], used[105];
	int i, j, min, no1, no2;
	
	while (1){
		scanf("%d %d", &n, &m);
		
		if (n == 0 && m == 0){
			break;
		}
		
		for (i = 0; i < m; i++){
			for (j = 0; j < m; j++){
				mcost[i][j] = INF;
				tcost[i][j] = INF;
			}
		}
		
		for (i = 0; i < n; i++){
			scanf("%d %d", &a, &b);
			a--; b--;
			scanf("%d %d", &no1, &no2);
			
			if (no1 < mcost[a][b]){
				mcost[a][b] = no1;
				mcost[b][a] = no1;
			}
			if (no2 < tcost[a][b]){
				tcost[a][b] = no2;
				tcost[b][a] = no2;
			}
			
		}
		
		scanf("%d", &k);
		
		for (i = 0; i < k; i++){
			scanf("%d %d %d", &a, &b, &t);
			a--; b--;
			for (j = 0; j < m; j++){
				node[j] = INF;
				used[j] = 0;
			}
			
			node[a] = 0;
			while (1){
				used[a] = 1;
				min = INF;
				
				for (j = 0; j < m; j++){
					if (t == 1){
						if (used[j] == 0 && tcost[a][j] != INF && node[j] > node[a] + tcost[a][j]){
							node[j] = node[a] + tcost[a][j];
						}
						if (used[j] == 0 && min > node[j]){
							min = j;
						}
					}
					else{
						if (used[j] == 0 && tcost[a][j] != INF && node[j] > node[a] + mcost[a][j]){
							node[j] = node[a] + mcost[a][j];
						}
						if (used[j] == 0 && min > node[j]){
							min = j;
						}
					}
				}
				a = min;
				if (a == b){
					break;
				}
			}
			printf("%d\n", node[b]);
		}
	}
	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;

int mindis[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
};

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

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));
		}
		int k;
		cin >> k;
		for(int i = 0; i < k; i++){
			fill(mindis, mindis+101, 1<<29);
			int s, e, mode;
			cin >> s >> e >> mode;
			cout << bf(s, e, mode, tb) << endl;
		}
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<queue>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)

class L{
	public:
	int a,c,t;
	L(int a,int c,int t):a(a),c(c),t(t){}
};

class S{
	public:
	int now;
	int p;
	S(int now,int p):now(now),p(p){}
	bool operator<(const S&a)const{
		return p>a.p;
	}
};

int main(){
	A:;
	
	int n,m;
	cin>>n>>m;
	
	if(n==0)return 0;
	
	vector<L> load[111];
	
	rep(i,n){
		int a,b,c,t;
		cin>>a>>b>>c>>t;
		
		load[a].push_back(L(b,c,t));
		load[b].push_back(L(a,c,t));
	}
	
	
	int k;
	cin>>k;
	
	rep(pp,k){
		int aa,bb,cc;
		cin>>aa>>bb>>cc;
		
		priority_queue<S> que;
		que.push(S(1,0));
		
		int visit[111]={0};
		while(1){
			if(que.empty())break;
			
			S u = que.top();
			que.pop();
			
			//printf("%d %d\n",u.now,u.p);
			
			if(u.now==m){
				printf("%d\n",u.p);
				break;
			}
			
			if(visit[u.now]==1)continue;
			visit[u.now]=1;
			
			rep(i,load[u.now].size()){
				
				L e = load[u.now][i];
				
				if(cc==0){
					que.push(S(e.a, u.p+e.c));
				}else{
					que.push(S(e.a, u.p+e.t));
				}
			}
		}
	}
	
	goto A;
}