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


class Edge {
public:
    int sid, cost, time;
    Edge(int s, int c, int t) {
        sid = s;
        cost = c;
        time = t;
    }
};


class Node {
public:
    static int mode;
    int sid, cost, time;
    bool visited;
    vector<Edge*> edge;

    bool operator() (Node *a, Node *b) {
        return Node::mode == 0 ? a->cost > b->cost: a->time > b->time;
    }
};
int Node::mode = 0;


int main() {
    Node node, station[100], *s;
    priority_queue<Node*, vector<Node*>, Node> pq(node);
    int n, m, s1, s2, i, cost, time, k, p, q;

    while (cin >> n >> m && n) {
        for (i = 1; i <= m; i++) {
            station[i].sid = i;
            station[i].edge.clear();
        }
        for (i = 0; i < n; i++) {
            cin >> s1 >> s2 >> cost >> time;
            station[s1].edge.push_back(new Edge(s2, cost, time));
            station[s2].edge.push_back(new Edge(s1, cost, time));
        }

        cin >> k;
        while (k--) {
            for (i = 1; i <= m; i++) {
                station[i].cost = 3000000;
                station[i].time = 3000000;
                station[i].visited = false;
            }
            cin >> p >> q >> Node::mode;
            s = &station[p];
            s->cost = 0;
            s->time = 0;
            pq = priority_queue<Node*, vector<Node*>, Node>(node);
            pq.push(s);
            while (!pq.empty()) {
                s = pq.top(); pq.pop();
                if (s->sid == q) break;
                if (s->visited) continue;
                s->visited = true;
                for (vector<Edge*>::iterator e = s->edge.begin(); e != s->edge.end(); e++) {
                    Node *ds = &station[(*e)->sid];
                    if (Node::mode == 0) {
                        cost = s->cost + (*e)->cost;
                        if (cost < ds->cost) {
                            ds->cost = cost;
                            pq.push(ds);
                        }
                    }
                    else {
                        time = s->time + (*e)->time;
                        if (time < ds->time) {
                            ds->time = time;
                            pq.push(ds);
                        }
                    }
                }
            }
            cout << (Node::mode == 0 ? s->cost: s->time) << endl;
        }
    }

    return 0;
}

C++: #include<iostream>
#include<cstring>
#include<vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define INF 300000

struct E
{
    int a,b,c,t;
    E(){};
    E(int a,int b,int c,int t):a(a),b(b),c(c),t(t){};
};
//E cs[700];
unsigned int d[150];

int main()
{
    int n,m,k,p,q,r;
    while(cin >> n >> m)
    {
        if(n==0 && m==0)break;
        vector<E> cs;   
        int ta,tb,tc,tt;
        rep(i,n)
        {
            cin >> ta>>tb>>tc >>tt;
            cs.push_back( E(ta,tb,tc,tt));
            cs.push_back( E(tb,ta,tc,tt));
        }

        cin >> k;
        rep(i,k)
        {
            memset(d,INF,150*sizeof(int));
            cin >> p >> q >>r;
            d[p] = 0;

            bool update = false;
            if(r==0)while(true)
            {
                update = false;
                rep(j,m*2)
                {
                   if(d[cs[j].a] != INF && d[cs[j].b] > d[cs[j].a] + cs[j].c)
                   {
                       update = true;
                       d[cs[j].b] =  d[cs[j].a] + cs[j].c;
                   }
                }
                if(!update) break;
            }
            if(r==1)while(true)
            {
                update = false;
                rep(j,m*2)
                {
                   if(d[cs[j].a] != INF && d[cs[j].b] > d[cs[j].a] + cs[j].t)
                   {
                       update = true;
                       d[cs[j].b] =  d[cs[j].a] + cs[j].t;
                   }
                }
                if(!update) break;
            }
            cout << d[q] << endl;
        }
        
    }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
struct Graph{
  Graph(int n):E(n),d(n){}
  vector<vector<tuple<int,int,int>>>E;
  vector<int>d;
  void add_edge(int a,int b,int c,int d){
    E[a].push_back(make_tuple(b,c,d));
  }
  void dijkstra(int s,int f){
    priority_queue<P,vector<P>,greater<P>>q;
    fill(d.begin(),d.end(),1e9);
    q.push({d[s]=0,s});
    while(q.size()){
      auto p=q.top();q.pop();
      if(p.first>d[p.second])continue;
      for(auto& a:E[p.second]){
        int x=get<0>(a),y=get<1>(a)+p.first;
        if(f)y=get<2>(a)+p.first;
        if(y<d[x])d[x]=y,q.push({y,x});
      }
    }
  }
};
main(){
  int n,m,a,b,c,d,k;
  while(cin>>n>>m,n){
    Graph E(m);
    for(int i=0;i++<n;E.add_edge(--a,--b,c,d),E.add_edge(b,a,c,d))cin>>a>>b>>c>>d;
    cin>>k;
    for(int i=0;i++<k;cout<<E.d[--b]<<endl){
      cin>>a>>b>>c;
      E.dijkstra(--a,c);
    }
  }
}