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<iostream>
#include<vector>
#include<queue>
using namespace std;

class S{
public:
	S(int pos,int cost){
		p=pos;
		c=cost;
	}
	int p,c;
};
bool operator<(S a,S b){
	return a.c>b.c;
}
int m;
vector<S> edge[100][2];
void add_edge(int a,int b,int c,int t){
	edge[a][0].push_back(S(b,c));
	edge[a][1].push_back(S(b,t));
	edge[b][0].push_back(S(a,c));
	edge[b][1].push_back(S(a,t));
}


int solve(int p,int q,int r){
	int ans=0;
	bool use[100];
	for(int i=0;i<100;i++){
		use[i]=0;
	}
	
	priority_queue<S> que;
	que.push(S(p,0));
	
	while(!que.empty()){
		S n=que.top();que.pop();
		
		if(n.p==q){
			ans=n.c;
			break;
		}
		
		if(use[n.p]){
			continue;
		}
		use[n.p]=1;
		
		
		for(int i=0;i<edge[n.p][r].size();i++){
			if(!use[edge[n.p][r][i].p]){
				que.push(S(edge[n.p][r][i].p,n.c+edge[n.p][r][i].c));
			}
		}
	}
	return ans;
}



int main(){
	int n;
	while(cin>>n>>m,n!=0||m!=0){
		for(int i=0;i<100;i++){
			for(int j=0;j<2;j++){
				edge[i][j].clear();
			}
		}
		for(int i=0;i<n;i++){
			int a,b,c,t;
			cin>>a>>b>>c>>t;
			a--;b--;
			add_edge(a,b,c,t);
		}
		int k;
		cin>>k;
		for(int i=0;i<k;i++){
			int p,q,r;
			cin>>p>>q>>r;
			p--;q--;
			cout<<solve(p,q,r)<<endl;
		}
	}
}

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

const int INF = 1e9 + 7;
int m;
int g1[100][100]; //cost
int g2[100][100]; //time

void floyd()
{
  for (int k = 0; k < m; k++) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        g1[i][j] = min(g1[i][j], g1[i][k] + g1[k][j]);
        g2[i][j] = min(g2[i][j], g2[i][k] + g2[k][j]);
      }
    }
  } 
}

signed main()
{
  int n, k;
  int a, b, c, t;

  while (cin >> n >> m, n || m) {
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        g1[i][j] = INF;
        g2[i][j] = INF;
      }
    }

    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c >> t;
      a--;
      b--;
      g1[a][b] = c;
      g1[b][a] = c;
      g2[a][b] = t;
      g2[b][a] = t;
    }

    floyd();

    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      if (r == 0) {
        cout << g1[p - 1][q - 1] << endl;
      } else {
        cout << g2[p - 1][q - 1] << endl;
      }
    }
  }
  return (0);
}

C++: #include<iostream>
#include<vector>
using namespace std;

const int inf = 1e8;
int m,n;
//n経路の数（枝）　m駅の数（葉）
struct edge{
  int from,to,cost,time;
  edge(int from,int to,int cost,int time): from(from), to(to), cost(cost), time(time){}
};

vector<edge> es;
int shortest_path(int s,int e,int v){
  int d[m];
  for(int i = 0 ; i < m ; i ++)d[i] = inf;
  d[s] = 0 ;
  while(true){
    bool updata = false;
    for(int i = 0 ; i < n ; i ++){
      edge e = es[i];
      if(d[e.from] != inf && d[e.to]> d[e.from] + e.cost && v == 0){
	d[e.to] = d[e.from] + e.cost;
	updata = true;
      }

      if(d[e.from] != inf && d[e.to]> d[e.from] + e.time && v == 1){
	d[e.to] = d[e.from] + e.time;
	updata = true;
      }
    } 
    if(!updata) break;
  }
  return d[e];
}

int main(void){
  while(cin>>n>>m,m+n){
    es.clear();
    for(int i = 0 ; i < n ; i ++){
      int s,t,c,ti;
      cin>>s>>t>>c>>ti;
      s--; t--;
      es.push_back(edge(s,t,c,ti));
    }
    int num;
    cin>>num;
    for(int i = 0 ; i < num ;  i++){
      int s,t,v;
      cin>>s>>t>>v;
      s--; t--;
      //cout<<s <<" " << t << " " << v<<" ";
      cout<<shortest_path(s,t,v)<<endl;
    }
  }
  
    
}

C++: #include <stdio.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>

using namespace std;

#define MAX_N 3000
#define MAX_M 100
#define MAX_K 200

int N, M, K;
//int P, Q, R;
int cost[MAX_M][MAX_M];
int times[MAX_M][MAX_M];
const int inf = 100100;

//R 0:最小金額、 1:最短時間

void init()
{
	for(int y=1; y<=M; ++y)
	{
		for(int x=1; x<=M; ++x)
		{
			cost[x][y] = inf;
			times[x][y] = inf;
		}
	}
}

void show(int R)
{
	auto weight = (R? times : cost);
	for(int x=1; x<=M; ++x)
	{
		for(int y=1; y<=M; ++y)
		{
			if(weight[x][y] == inf){cout << "inf ";}
			else{printf("%3d ",weight[x][y]);}
		}
		cout << endl;
	}
}

void floyd_warshall(int P, int Q, int R)
{
	auto weight = (R? times : cost);
	for(int k=1; k<=M; ++k)
	{
		for(int i=1; i<=M; ++i)
		{
			for(int j=1; j<=M; ++j)
			{
				if(weight[i][j] > weight[i][k]+weight[k][j])
				{
					weight[i][j] = weight[i][k]+weight[k][j];
				}
			}
		}
	}
	cout << weight[P][Q] << endl;
}

void dijkstra()
{
	//for(int i=0; )
}

void bellman_ford(int P, int Q, int R)
{
	auto weight = (R? times : cost);
	
	int D[M+1];
	for(int i=0; i<=M; ++i)
	{
		D[i] = inf;
	}
	D[P] = 0;
	for(int i=0; i<abs(M)-1; ++i)
	{
		for(int u=1; u<=M; ++u)
		{
			for(int v=1; v<=M; ++v)
			{
				D[v] = min(D[v], D[u]+weight[u][v]);
			}
		}
	}
	cout << D[Q] << endl;
}

int main()
{
	while(true)
	{
		cin >> N >> M;
		if(!N && !M){break;}
		init();
		int a, b, c, t;
		for(int i=0; i<N; ++i)
		{
			cin >> a >> b >> c >> t;
			cost[a][b] = c;
			cost[b][a] = c;
			times[a][b] = t;
			times[b][a] = t;
		}
		cin >> K;
	
		int P, Q, R;
		for(int i=0; i<K; ++i)
		{
			cin >> P >> Q >> R;
			bellman_ford(P,Q,R);
			//floyd_warshall(P, Q, R);
		}
	}
}