Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

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<cstdio>
#include<queue>
#include <algorithm>
using namespace std;



int d[100];
bool used[100];

int n,m,k;
int INF=200000000;



int main(){

	int i,j;
	int a,b,c,e;

	int cost[100][100];
	int time[100][100];


	scanf("%d %d",&n,&m);

	fill((int*)cost,(int*)(cost+m),INF);
	fill((int*)time,(int*)(time+m),INF);

	/*
	fill(d,d+m,INF);
	memset(used,0,sizeof(used));
	*/

	for(i=0;i<n;i++){
		scanf("%d %d %d %d",&a,&b,&c,&e);
		a--;
		b--;
		cost[a][b]=cost[b][a]=c;
		time[a][b]=time[b][a]=e;
	}

	scanf("%d",&k);

	for(i=0;i<k;i++){
		scanf("%d %d %d",&a,&b,&c);
		a--;
		b--;
		
		fill(d,d+m,INF);
		fill(used,used+m,false);

		d[a]=0;
		
		
		while(true){
			int v=-1;
			
			//printf("-\n");

			for(j=0;j<m;j++){
				if(!used[j] && (v==-1 || d[j]<d[v])) v=j;
			}	
			
			if(v==-1)break;
			used[v]=true;

			if(c) for(j=0;j<m;j++) d[j]=min(d[j],d[v]+time[v][j]);
			else  for(j=0;j<m;j++) d[j]=min(d[j],d[v]+cost[v][j]);
		}
		printf("%d",d[b]);
		
		

	}
	scanf("%d %d",&a,&b);


	return 0;
}



	//scanf("%s",&i);

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int > P;
const int INF = 1 << 30;
int Dijkstra(int s,int g,int r);

int m,n;

int min_cost[2][111];

typedef struct{
  vector<int>edge_to;
  vector<int>edge_cost[2];
}Node_data;

Node_data Graph[111];

priority_queue<P ,vector< P >, greater< P > >que;

int main(){
  int a,b,c,d,k;
  int s,g,r;
  while(1){
    cin >> m >> n;
    if(m == 0 && n == 0)break;
    for(int i = 0;i < m;i++){
      scanf("%d %d %d %d",&a,&b,&c,&d);
      Graph[a - 1].edge_to.push_back(b - 1);
      Graph[a - 1].edge_cost[0].push_back(c);
      Graph[a - 1].edge_cost[1].push_back(d);
      Graph[b - 1].edge_to.push_back(a - 1);
      Graph[b - 1].edge_cost[0].push_back(c);
      Graph[b - 1].edge_cost[1].push_back(d);
    }
    cin >> k;
    for(int i = 0;i < k;i++){

      cin >> s >> g >> r;

      for(int j = 0;j < n;j++){
	min_cost[r][j] = INF;
      }
      cout << Dijkstra(s - 1,g - 1,r) << endl;
    }
  }
  return(0);
}

int Dijkstra(int s,int g,int r){
  int node,cost;
  int newnode,newcost;
  min_cost[r][s] = 0;
  P a;
  a.first = 0;
  a.second = s;
  que.push(a);
  while(!que.empty()){
    node = que.top().second;
    cost = que.top().first;
    que.pop();
    if(node == g)return cost;
    if(cost > min_cost[r][node])continue;
    for(int i = 0;i < Graph[node].edge_to.size();i++){
      newnode = Graph[node].edge_to[i];
      newcost = min_cost[r][node] + Graph[node].edge_cost[r][i];
      if(min_cost[r][newnode] > newcost){
	min_cost[r][newnode] = newcost;
	a.first = newcost;
	a.second = newnode;
	que.push(a);
      }
    }
  }
  return -1;
}

C++: #include <iostream>
#include <math.h>
#include <vector>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)

int main(void){
	int n, m;
	while(cin >> n >> m, n||m){
		int cost[120][120];
		int timm[120][120];
		fill_n(*cost, 120*120, 10000000);
		fill_n(*timm, 120*120, 10000000);
		for(int i = 0; i < n; i++){
			int a, b,c,t;
			cin >> a >> b >> c >> t;
			cost[a][b] = c;
			cost[b][a] = c;
			timm[a][b] = t;
			timm[b][a] = t;
		}
		for(int k=0;k<n;++k)for(int i=0;i<n;++i)for(int j=0;j<n;++j){
			cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
			timm[i][j] = min(timm[i][j], timm[i][k] + timm[k][j]);
		}
		int k;
		cin >> k;
		rep(i, k){
			int a, b, c;
			cin >> a >> b >> c;
			if(!c)
				cout << cost[a][b] << endl;
			else
				cout << timm[a][b] << 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;
	
	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", &mcost[a][b], &tcost[a][b]);
			mcost[b][a] = mcost[a][b];
			tcost[b][a] = tcost[a][b]; 
		}
		
		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);
}