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<set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define INF 1e6
using namespace std;
int n,m,k,a,b,c,d,p,q,r;
int cost[101][101],tm[101][101],d[101];
bool used[101];

void dfunc(int s,int r){
	d[s]=0;
	rep(i,101){
		used[i]=true;
		d[i]=INF;
	}
	while(1){
		int mem=INF,now=-1;
		rep(i,101){
			if(used[i] && mem>d[i]){
				now=i;
				mem=d[i];
			}
		}
		if(now==-1)break;
		used[now]=false;
		rep(i,101){
			if(r==0){
				if(cost[now][i]!=INF)d[i]=d[now]+cost[now][i];
			}else{
				if(tm[now][i]!=INF)d[i]=d[now]+tm[now][i];
			}
		}
	}
}

int main(){
	while(cin>>n>>m,n||m){
		rep(i,101)rep(j,101){
			cost[i][j]=INF;
			tm[i][j]=INF;
		}
		rep(i,n){
			cin>>a>>b>>c>>d;
			a--;
			b--;
			cost[b][a]=cost[a][b]=c;
			tm[b][a]=tm[a][b]=d;
		}
		cin>>k;
		rep(i,k){
			cin>>p>>q>>r;
			dfunc(p,r);
			cout<<d[q]<<endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#define _m_ 1
const int INF = 1e9;

int cost[101][101];
int tim[101][101];
int d[101];
bool used[101];
int V;

void dijkstra(int s, int o){
	std::fill(d, d + V, INF);
	std::fill(used, used + V, false);
	d[s] = 0;
	while(_m_){
		int v = -1;
		for(int u = 0; u < V; ++u){
			if(!used[u] && (v == -1 || d[u] < d[v])) v = u;
		}
		if(v == -1)break;
		used[v] = true;
		if(o == 0){
			for(int u = 0; u < V; ++u){
				d[u] = std::min(d[u], d[v] + cost[v][u]);
			}
		}else {
			for(int u = 0; u < V; ++u){
				d[u] = std::min(d[u], d[v] + tim[v][u]);
			}
		}
	}
}

int main(){
	int n, m;
	while(scanf("%d %d",&n,&m), n+m){
		for(int i=0;i<m;++i){
			for(int j=0;j<m;++j){
				cost[i][j]=INF;
				tim[i][j]=INF;
			}
		}
		for(int i = 0; i < n; ++i){
			int a, b, co, ti;
			scanf("%d %d %d %d",&a, &b, &co, &ti);
			a--,b--;
			cost[a][b] = co, cost[b][a] = co;
			tim[a][b] = ti, tim[b][a] = ti;
		}
		V = m;
		int k;
		scanf("%d", &k);
		for(int i = 0; i < k; ++i){
			int p, q, r;
			scanf("%d %d %d", &p, &q, &r);
			p--, q--;
			dijkstra(p, r);
			printf("%d\n", d[q]);
		}
	}
}

C++: #include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

struct edge{
	int money;
	int time;
};

int main(){
	int n, m;
	int count;
	edge G[100][100];

	while(cin >> n >> m, n || m){
		for(int i=0; i < 100; i++){
			for(int j=0; j < 100; j++){
				G[i][j].money = 100000;
				G[i][j].time = 100000;
			}
		}

		for(int i=0; i < n; i++){
			int a, b, mo, t;
			cin >> a >> b >> mo >> t;

			G[a][b].time=t;
			G[a][b].money=mo;
			G[b][a].time=t;
			G[b][a].money=mo;
		}

		for(int k=0; k < n; k++){
			for(int i=0; i < n; i++){
				for(int j=0; j < n; j++){
					G[i][j].time = min(G[i][j].time, G[i][k].time+G[k][j].time);
					G[i][j].money = min(G[i][j].money, G[i][k].money+G[k][j].money);
				}
			}
		}

		cin >> count;
		for(int i=0; i < count; i++){
			int p;
			cin >> n >> m >> p;

			if(p==1) cout << G[n][m].time << endl;
			else cout << G[n][m].money << endl;
		}
	}
	return 0;
}

C++: #include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <functional>
#include <queue>
#include <vector>

#define MAX_N 100
#define MAX_M 3000

struct state {
	int v;
	int dist;
	state(int v = 0, int dist = 0):v(v), dist(dist){}
	bool operator>(const state &s) const {
		return dist > s.dist;
	}
};

typedef state value_type;

std::priority_queue<value_type, std::vector<value_type>, std::greater<value_type>> que;

bool empty() {
	return que.empty();
}

void enqueue(value_type value) {
	que.push(value);
}

value_type dequeue() {
	value_type res = que.top();
	que.pop();
	return res;
}

// graph
int n, m;
int num;
int head[MAX_N], next[MAX_M * 2], to[MAX_M * 2];
int costs[MAX_M * 2], times[MAX_M * 2];
int dist[MAX_N];

void init() {
	num = 0;
	memset(head, -1, sizeof(head));
}

void add_edge(int u, int v, int c, int t) {
	next[num] = head[u];
	head[u] = num;
	to[num] = v;
	costs[num] = c;
	times[num] = t;
	++num;
}

// calc dist
int dijkstra(int start, int goal, int r) {
	int i, e;
	const int *weight = (r ? times : costs);

	for(i = 0; i < n; ++i) dist[i] = INT_MAX;
	dist[start] = 0;
	while(!empty()) dequeue();
	enqueue((state){start, 0});

	while(!empty()) {
		state current = dequeue();
		if(dist[current.v] < current.dist)
			continue;

		if(current.v == goal)
			return dist[goal];

		for(e = head[current.v]; e != -1; e = next[e]) {
			int next_dist = current.dist + weight[e];
			if(dist[to[e]] > next_dist) {
				dist[to[e]] = next_dist;
				enqueue((state){to[e], next_dist});
			}
		}
	}
	assert(false);
}

int main(void) {
	int i;
	while(scanf("%d%d", &m, &n) == 2 && n) {
		init();
		for(i = 0; i < m; ++i) {
			int u, v, c, t;
			scanf("%d%d%d%d", &u, &v, &c, &t);
			--u; --v;
			add_edge(u, v, c, t);
			add_edge(v, u, c, t);
		}

		int query;
		scanf("%d", &query);

		while(query--) {
			int start, goal, r;
			scanf("%d%d%d", &start, &goal, &r);
			printf("%d\n", dijkstra(start - 1, goal - 1, r));
		}
	}
	return EXIT_SUCCESS;
}