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<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<s;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
int main(){
	int n,m;
	cin>>n>>m;
	vector<vector<pii>>v(n+1,vector<pii>(n+1,pii(INF,INF)));
	rep(i,n+1)v[i][i]=pii(INF,INF);
	rep(i,n){
		pii temp;
		int s,g;
		cin>>s>>g>>temp.first>>temp.second;
        v[--s][--g]=temp;
	}
	int k; cin>>k;
    rep(u,k){
    int s,g; int r;
    cin>>s>>g>>r;
    if(!r){
        rep(k,m){
            rep(j,m){
                rep(i,m){
                    v[i][j].first=min(v[i][k].first+v[k][j].first,v[i][j].first);
                }
            }
        }
        cout<<v[--s][--g].first<<endl;
    }else{
        rep(k,m){
            rep(j,m){
                rep(i,m){
                    v[i][j].second=min(v[i][k].second+v[k][j].second,v[i][j].second);
                }
            }
        }
        cout<<v[--s][--g].second<<endl;
    }
    }
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <limits.h>
#include <queue>

#define INF INT_MAX - 1

using namespace std;

typedef long long ll;

struct edge {ll to,cost;};
typedef pair<ll,ll> P;

vector<edge> ek[6000];
vector<edge> em[6000];
priority_queue<P,vector<P>,greater<P> >que;

ll d[256];
int q,r;

void dik()
{
  while(!que.empty())
    {
      P p = que.top();
      que.pop();
      // cout<<p.first<<"  "<<p.second<<endl;
      int v=p.second;
      int h=p.first;
      if(v==q && d[v]!=INF)
	break;
      for(int i=0;i<ek[v].size();i++)
	{
	  edge e;
	  if(r==0)
	    e=em[v][i];
	  else
	    e=ek[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;
  for(;cin>>n>>m,n;)
    {
      //マップ初期化
      for(int i=0;i<6000;i++)
	{
	  ek[i].clear();
	  em[i].clear();
	}
      
      for(int i=0;i<n;i++)
	{
	//エッジ情報
	  int a,b;
	  ll cost,time;
	  cin>>a>>b>>cost>>time;
	  edge xx;
	  xx.to=b;
	  xx.cost=cost;
	  em[a].push_back(xx);
	  xx.cost=time;
	  ek[a].push_back(xx);

	  xx.to=a;
	  xx.cost=cost;
	  em[b].push_back(xx);
	  xx.cost=time;
	  ek[b].push_back(xx);
	  
	}
      int c;
      cin>>c;
      for(;c--;)
	{
	  fill(d,d+102,INF);
	  //問い合わせ
	  int p;
	  cin>>p>>q>>r;
	  d[p]=0;
	  que.push(P(0,p));
	  dik();
	  cout<<d[q]<<endl;
	}
    }
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int x[1000][1000][2], n, m, a, b, c, d, q, e;

int path(int n, int s, int g) {
	int p[1000];
	memset(p, 127, sizeof(p));
	p[s] = 0;
	e = min(n, 20);
	for (int i = 0; i < e; i++){
		for (int j = 1; j <= n; j++) {
			for (int k = 1; k <= n; k++) {
				if (x[j][k][1] < 10000000) {
					p[k] = min(p[k], p[j] + x[j][k][1]);
				}
			}
		}
	}
	return p[g];
}

int score(int n, int s, int g) {
	int p[1000];
	memset(p, 127, sizeof(p));
	p[s] = 0;
	e = min(n, 20);
	for (int i = 0; i < e; i++) {
		for (int j = 1; j <= n; j++) {
			for (int k = 1; k <= n; k++) {
				if (x[j][k][0] < 10000000) {
					p[k] = min(p[k], p[j] + x[j][k][0]);
				}
			}
		}
	}
	return p[g];
}

int main() {
	while (true) {
		memset(x, 0, sizeof(x));
		cin >> m >> n;
		if (m == 0 && n == 0) { break; }
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> c >> d;
			x[a][b][0] = c;
			x[b][a][0] = c;
			x[a][b][1] = d;
			x[b][a][1] = d;
		}
		cin >> q;
		for (int i = 0; i < q; i++) {
			cin >> a >> b >> c;
			if (c == 0) { cout << score(n, a, b); }
			else { cout << path(n, a, b); }
		}
	}
}

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

#define MAX_M 110

struct edge{int to,cost;};
typedef pair<int,int> P;

vector<edge> c[2][MAX_M];

int dijkstra(int s,int e,bool r){
	int d[MAX_M];
	for(int i=0;i<MAX_M;i++){
		d[i]=INT_MAX;
	}
	priority_queue<P,vector<P>,greater< P > > que;
	
	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;
		for(int i=0;i<c[r][v].size();i++){
			edge e=c[r][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));
			}
		}
	}
	
	return d[e];
}

int main(){
	int n,m;
	while(cin>>n>>m,n!=0||m!=0){
		for(int i=0;i<n;i++){
			int a,b,cos,tim;
			cin>>a>>b>>cos>>tim;
			edge co,t;
			co.to=b;co.cost=cos;
			t.to=b;t.cost=tim;
			c[0][a].push_back(co);
			c[1][a].push_back(t);
		}
		cin>>n;
		for(int i=0;i<n;i++){
			int s,e;
			bool r;
			cin>>s>>e>>r;
			int ans=dijkstra(s,e,r);
			cout<<ans<<endl;
		}
	}
}