Go: // Warshall–Floyd Algorithm

package main

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

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
				} else {
					c_dist[i][j] = c_matrix[i][j]
				}
				if c_matrix[i][j] == 0{
					c_prev[i][j] = -1
				} else {
					c_prev[i][j] = i
				}
				if t_matrix[i][j] == 0{
					t_dist[i][j] = INF
				} else {
					t_dist[i][j] = t_matrix[i][j]
				}
				if t_matrix[i][j] == 0{
					t_prev[i][j] = -1
				} else {
					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 <algorithm>
using namespace std;
int main(){
  int road1[111][111];
  int road2[111][111];
  int n,m;
  int a,b,cost,time;
  int p,q,r;
  int x;
  while(1){
    for(int i=0;i<111;i++){
      for(int j=0;j<111;j++){
	road1[i][j]=road2[i][j]=111111111;
      }
    }
    cin >> n >> m;
    if(n==0 && m==0)break;
    for(int i=0;i<n;i++){
      cin >> a >> b >> cost >> time;
      road1[a][b]=road1[b][a]=cost;
      road2[a][b]=road2[b][a]=time;
    }

    for(int k=1;k<=n;k++){
      for(int i=1;i<=n;i++){
	for(int j=1;j<=n;j++){
	  if(road1[i][j]>road1[i][k]+road1[k][j]){
	    road1[i][j]=road1[i][k]+road1[k][j];
	  }
	  if(road2[i][j]>road2[i][k]+road2[k][j]){
	    road2[i][j]=road2[i][k]+road2[k][j];
	  }
	}
      }
    }
    
    cin >> x;
    for(int i=0;i<x;i++){
      cin >> p >> q >> r;
      cout << (r==0 ? road1[p][q] : road2[p][q]) << endl;
    }
  }
}

C++: #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>

#define INF 10000

int t_rail[101][101],c_rail[101][101];
int ans[101],used[101];

using namespace std;
int main(void)
{
   int m,n;
   int a,b,cost,time;
   int call,s,g,r;
   while(scanf("%d%d",&m,&n),m+n)
   {
      for(int i = 0;i < 101;i++)
         for(int j = 0;j < 101;j++)
            {
               t_rail[i][j] = INF;
               c_rail[i][j] = INF;
            }

      for(int i = 0;i < m;i++)
      {
         scanf("%d%d%d%d",&a,&b,&cost,&time);
         t_rail[a][b] = time;
         t_rail[b][a] = time;
         c_rail[a][b] = cost;
         c_rail[b][a] = cost;
      }

      scanf("%d",&call);
      for(int j = 0;j < call;j++)
      {
         scanf("%d%d%d",&s,&g,&r);
         fill(ans,ans+101,INF);
         fill(used,used+101,0);
         ans[s] = 0;
         if(r == 0)
         {
            while(1)
            {
               int v = -1;
               for(int k = 0;k < 101;k++) if(used[k] == 0 && (v == -1 || ans[v] > ans[k]))v = k;
               if(v == -1) break;
               used[v] = 1;
               for(int k = 0;k < 101;k++) ans[k] = min(ans[k],ans[v]+c_rail[v][k]);
            }
         }
         else
         {
            while(1)
            {
               int v = -1;
               for(int k = 0;k < 101;k++) if(used[k] == 0 && (v == -1 || ans[v] > ans[k])) v = k;
               if(v == -1) break;
               used[v] = 1;
               for(int k = 0;k < 101;k++) ans[k] = min(ans[k],ans[v]+t_rail[v][k]);
            }
         }
         printf("%d\n",ans[g]);
      }
   }
   return 0;
}

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

#define MMAX 101
const int INF=1<<10;

int gtime[MMAX+1][MMAX+1];
int gcost[MMAX+1][MMAX+1];

int n,m;
int p,q,r;

struct node{
	int st;//
	int cost;//
}typedef Node;

bool operator < (const Node n1,const Node n2){
	return n1.cost > n2.cost;
}
bool visited[MMAX];
int dijkstra(){
	//fill(visited,visited+MMAX,false);
	priority_queue<Node> pq;
	Node start={p,0};
	pq.push(start);
	while(!pq.empty()){
		Node cur=pq.top();pq.pop();
		if(cur.st==q)return cur.cost;
//		if(visited[cur.st])continue;
//		visited[cur.st]=true;
		for(int i=1;i<=m;i++){
			if(cur.st==i||gtime[cur.st][i]==INF||gcost[cur.st][i]==INF)continue;
			if(visited[i])continue;
			if(r==1){
				Node add={i,cur.cost+gtime[cur.st][i]};
				pq.push(add);
			}
			else {//
				Node add={i,cur.cost+gcost[cur.st][i]};
				pq.push(add);
			}
		}
	}
}
void solve(){
	cout<<dijkstra()<<endl;
}
int main(){
	int a,b,cost,time,k;
	while(cin>>n>>m,(n||m)){
		for(int i=0;i<MMAX;i++){
			fill(gtime[i],gtime[i]+MMAX,INF);
			fill(gcost[i],gcost[i]+MMAX,INF);
		}
		for(int i=0;i<n;i++){
			cin>>a>>b>>cost>>time;
			if(gtime[a][b]>time)gtime[a][b]=gtime[b][a]=time;
			if(gcost[a][b]>cost)gcost[a][b]=gcost[b][a]=cost;
		}
		cin>>k;
		for(int i=0;i<k;i++){
			cin>>p>>q>>r;
			solve();
		}		
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <functional>
#include <vector>

using namespace std;

struct edge {
	int to;
	int cost;
};

typedef pair<int, int> P;

int V;
vector<edge> G[3010];
vector<edge> G2[3010];
int d[3010];

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

	while (!que.empty()) {
		P p = que.top();
		que.pop();
		int v = p.second;
		if (d[v] < p.first) {
			continue;
		}
		if (h == 0) {
			for (int i = 0; i < G[v].size(); i++) {
				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 {
			for (int i = 0; i < G2[v].size(); i++) {
				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;
		}
		for (int i = 1; i <= m; i++) {
			G[i].clear();
			G2[i].clear();
		}
		V = m+1;
		int a, b, time, cost;
		for (int i = 0; i < n; i++) {
			cin >> a >> b >> cost >> time;
			edge e;
			e.to = b;
			e.cost = cost;
			G[a].push_back(e);
			e.to = a;
			G[b].push_back(e);
			e.to = b;
			e.cost = time;
			G2[a].push_back(e);
			e.to = a;
			G2[b].push_back(e);
		}
		int w;
		cin >> w;
		int p, q, r;
		for (int i = 0; i < w; i++) {
			cin >> p >> q >> r;
			dijkstra(p, r);
			cout << d[q] << endl;
		}
	}
	return 0;
}