Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

var pass [][]int

func main() {
	for scanner.Scan() {
		pass = make([][]int, 200)
		for i := range pass {
			pass[i] = make([]int, 200)
		}
		buf := strings.Split(scanner.Text(), " ")
		c, _ := strconv.Atoi(buf[0])
		n, _ := strconv.Atoi(buf[1])
		m, _ := strconv.Atoi(buf[2])
		s, _ := strconv.Atoi(buf[3])
		d, _ := strconv.Atoi(buf[4])
		if c == 0 && n == 0 && m == 0 && s == 0 && d == 0 {
			break
		}
		s--
		d--
		for i := 0; i < 200; i++ {
			for j := 0; j < 200; j++ {
				pass[i][j] = INF
			}
		}
		for i := 0; i < 200; i++ {
			pass[i][i] = 0
		}
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			a, _ := strconv.Atoi(buf_[0])
			b, _ := strconv.Atoi(buf_[1])
			f, _ := strconv.Atoi(buf_[2])
			a--
			b--
			pass[a][b] = f
			pass[b][a] = f
		}
		fmt.Println(dijkstra(s, d, c, n))
	}
}

func dijkstra(s, g, c, n int) float64 {
	d := make([][]float64, 200)
	used := make([][]bool, 200)
	for i := 0; i < 200; i++ {
		d[i] = make([]float64, 20)
		used[i] = make([]bool, 20)
	}
	for i := range d {
		for j := range d[i] {
			d[i][j] = INF
		}
	}
	d[s][c] = 0
	for {
		v := -1
		w := -1
		for u := 0; u < n; u++ {
			for i := 0; i < c+1; i++ {
				if !used[u][i] && (v == -1 || d[u][i] < d[v][w]) {
					v = u
					w = i
				}
			}
		}
		if v == -1 {
			break
		}
		used[v][w] = true
		for u := 0; u < n; u++ {
			d[u][w] = math.Min(d[u][w], d[v][w]+float64(pass[v][u]))
			if w-1 >= 0 && pass[v][u] != INF {
				d[u][w-1] = math.Min(d[u][w-1], d[v][w]+(float64(pass[v][u]/2.0)))
			}
		}
	}
	ret := float64(INF)
	for i := 0; i < c+1; i++ {
		ret = math.Min(ret, d[g][i])
	}
	return ret
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define INF 1000000

int main() {
    while (true) {
        int c, n, m, s, d;
        cin >> c >> n >> m >> s >> d;
        
        if (c==0 && n==0 && m==0 && s==0 && d==0) {
            break;
        }
        
        s -= 1;
        d -= 1;
        
        vector<P> adj_list[n*(c+1)];
    
        for (int i=0; i<m; i++) {
            int ai, bi, fi;
            cin >> ai >> bi >> fi;
            ai -= 1;
            bi -= 1;
        
            for (int j=0; j<=c; j++) {
                adj_list[ai*(c+1)+j].push_back(P(bi*(c+1)+j, fi));
                adj_list[bi*(c+1)+j].push_back(P(ai*(c+1)+j, fi));
            
                if (j>0) {
                    adj_list[ai*(c+1)+j].push_back(P(bi*(c+1)+j-1, fi/2));
                    adj_list[bi*(c+1)+j].push_back(P(ai*(c+1)+j-1, fi/2));
                }
            }
        }
        
        priority_queue<P, vector<P>, greater<P>> pq;
        int dist[n*(c+1)];
        
        for (int i=0; i<n*(c+1); i++) {
            dist[i] = INF;
        }
        
        dist[s*(c+1)+c] = 0;
        pq.push(P(0, s*(c+1)+c));
        
        while (pq.size()) {
            P p = pq.top();
            pq.pop();
            
            int d = p.first;
            int v = p.second;
            
            if (dist[v]<d) {
                continue;
            }
            
            for (int i=0; i<adj_list[v].size(); i++) {
                int nv = adj_list[v][i].first;
                int w = adj_list[v][i].second;
                
                if (dist[nv]>dist[v]+w) {
                    dist[nv] = dist[v]+w;
                    pq.push(P(dist[nv], nv));
                }
            }
        }
        
        int ans = INF;
        
        for (int i=0; i<=c; i++) {
            ans = min(ans, dist[d*(c+1)+i]);
        }
        
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;
typedef int Weight;
Weight INF = 1000000000;
struct Edge{
  int src, dest; Weight weight;
  bool operator < (const Edge &rhs) const {return weight > rhs.weight;}
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

void add_edge(Graph &g, int src, int dest, Weight weight) {
  g[src].push_back((Edge){src, dest, weight});
}

// Dijkstra (Verified: AOJ2005)
void dijkstra(Graph &g, Array &d, int s) {
  d.assign(g.size(), INF);
  d[s] = 0;
  typedef pair<Weight,int> P;
  priority_queue<P, vector<P>, greater<P> > que;
  que.push(P(0, s));
  while (!que.empty()) {
    Weight dist = que.top().first;
    int v = que.top().second;
    que.pop();
    if (d[v] < dist) continue;
    REP(i, g[v].size()) {
      Edge e = g[v][i];
      if (d[e.dest] > d[v] + e.weight) {
        d[e.dest] = d[v] + e.weight;
        que.push(P(d[e.dest], e.dest));
      }
    }
  }
}

int main() {
  while(true) {
    int c,n,m,s,d;
    cin>>c>>n>>m>>s>>d;
    if(!c) break;
    --s;--d;
    Graph g(n*(c+1));
    REP(i,m) {
      int a,b,f;
      cin>>a>>b>>f;
      --a;--b;
      REP(j,c+1) {
        add_edge(g, j*n+a, j*n+b, f);
        add_edge(g, j*n+b, j*n+a, f);
      }
      REP(j,c) {
        add_edge(g, j*n+a, (j+1)*n+b, f/2);
        add_edge(g, j*n+b, (j+1)*n+a, f/2);
      }
    }
    REP(i,n) {
      REP(j,c) {
        add_edge(g, j*n+i, (j+1)*n+i, 0);
      }
    }
    Array ary;
    dijkstra(g, ary, s);
    cout<<ary[c*n+d]<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#include <list>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int,int> P;
const int MAX_V=101;
const int MAX_E=501;
int cost[MAX_V][MAX_V];
int d[MAX_V][11];
bool used[MAX_V][11];

int main(){
    int c,n,m,s,g;
    while(cin>>c>>n>>m>>s>>g){
        if(c==0&&n==0&&m==0&&s==0&&g==0) break;
        for(int i=1; i<=n; ++i){
            for(int j=0; j<=c; ++j){
            	d[i][j]=INF;
            	used[i][j]=false;
            }
            for(int j=1; j<=n; ++j){
                cost[i][j]=INF;
                cost[j][i]=INF;
            }
        }
        for(int i=0; i<m; ++i){
            int a,b,f;
            cin >> a >> b >> f;
            cost[a][b]=f;
            cost[b][a]=f;
        }
        d[s][0]=0;
        while(true){
            int v=-1;
            int cnt=-1;
            for(int u=1; u<=n; ++u){
            	for(int i=0; i<=c; ++i){
                	if(!used[u][i]&&(v==-1||d[u][i]<d[v][cnt])) {
                		v=u;
               			cnt=i;
                	}
                }
            }
            if(v==-1) break;
            used[v][cnt]=true;
            for(int u=1; u<=n; ++u){
            	if(cnt<c) d[u][cnt+1]=min(d[u][cnt+1],d[v][cnt]+cost[v][u]/2);
            	d[u][cnt]=min(d[u][cnt],d[v][cnt]+cost[v][u]);
            }
        }
        int ans=INF;
        for(int i=1; i<=n; ++i){
        	for(int j=1; j<=c; ++j){
        		ans=min(ans,d[g][j]);
        	}
        }
        cout << ans << "\n";
    }
    return 0;
}