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;
const int INF = 1000000000;

int c, n, m, S, D;

struct EDGE { int to, cost; };
vector<EDGE> g[100];

struct STATE
{
	int cost, now, num;
	
	bool operator<(const STATE t)const{
		return cost > t.cost;
	}
};

int d[100][11];

void init()
{
	for (int i = 0; i < 100; i++){
		g[i].clear();
	}
	for (int i = 0; i < 100; i++){
		for (int j = 0; j <= 10; j++){
			d[i][j] = INF;
		}
	}
}

void search(int s)
{	
	for (int i = 0; i <= 10; i++){
		d[s][i] = 0;
	}
	
	priority_queue<STATE, vector<STATE> > que;
	que.push({0, s, 0});
	while (!que.empty()){
		STATE st = que.top(); que.pop();
		if (d[st.now][st.num] < st.cost) continue;
		
		for (int i = 0; i < g[st.now].size(); i++){
			EDGE e = g[st.now][i];
			if (d[e.to][st.num] > d[st.now][st.num] + e.cost){
				d[e.to][st.num] = d[st.now][st.num] + e.cost;
				que.push({d[e.to][st.num], e.to, st.num});
			}
		}
		
		if (st.num == c) continue;
		
		for (int i = 0; i < g[st.now].size(); i++){
			EDGE e = g[st.now][i];
			if (d[e.to][st.num + 1] > d[st.now][st.num] + e.cost / 2){
				d[e.to][st.num + 1] = d[st.now][st.num] + e.cost / 2;
				que.push({d[e.to][st.num + 1], e.to, st.num + 1});
			}
		}
	}
}

int main()
{
	while (scanf("%d %d %d %d %d", &c, &n, &m, &S, &D), c){
		S--; D--;
		
		init();
		
		for (int i = 0; i < m; i++){
			int a, b, f;
			scanf("%d %d %d", &a, &b, &f);
			a--; b--;
			
			g[a].push_back({b, f});
			g[b].push_back({a, f});
		}
		
		search(S);
		int mini = INF;
		for (int i = 0; i <= c; i++) mini = min(mini, d[D][i]);
		printf("%d\n", mini);
	}
	
	return 0;
}

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

int calcCost(int c, int n, vector<int> path, vector<int> rt){
	priority_queue<int> costs;
	for(int i=1; i<rt.size(); i++){
		costs.push(path[rt[i]+n*rt[i-1]]);
	}
	int total = 0;
	while(costs.size() && c--){
		total += costs.top() / 2;
		costs.pop();
	}
	while(costs.size()){
		total += costs.top();
		costs.pop();
	}
	return total;
}
int main(){
	for(;;){
		int c,n,m,s,d;
		cin>>c>>n>>m>>s>>d;
		if(c+n+m+s+d==0) break;
		vector<pair<int, vector<int> > > route(n,make_pair(-1,vector<int>()));
		vector<int> path(n*n,-1);
		for(int i=0; i<m; i++){
			int a,b,f;
			cin>>a>>b>>f;
			path[--a+n*--b] = path[b+n*a] = f;
		}
		priority_queue<pair<int, vector<int> >, vector<pair<int, vector<int> > >, greater<pair<int, vector<int> > > > pq;
		pq.push(make_pair(0,vector<int>(1,s-1)));
		while(pq.size()){
			int cur = *(pq.top().second.rbegin());
			if(route[cur].first == -1){
				route[cur] = pq.top();
				for(int i=0; i<m; i++){
					if(route[i].first == -1 && path[i+n*cur] != -1){
						pair<int, vector<int> > copy = pq.top();
						copy.second.push_back(i);
						copy.first = calcCost(c,n,path,copy.second);
						pq.push(copy);
					}
				}
			}
			pq.pop();
		}
		cout<<route[d-1].first<<endl;
	}
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>
#include <ctime>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 29;
const double EPS = 1e-10;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
#define repD(i, n) for(int i = n; i >= 0; i--)

int cost[200][20];
vector<P> G[200];// to cost;

int main(){
	int c, n, m, s, d;
	int a, b, money;
	while(cin >> c  >> n >> m >> s >> d && (c || n || m || s || d)){
		rep(i, 200) G[i].clear();
		fill(&cost[0][0], &cost[199][19] + 1, INF);
		rep(i, m){
			cin >> a >> b >> money;
			G[a].push_back(P(b, money));
			G[b].push_back(P(a, money));
		}

		cost[s][c] = 0;
		priority_queue<PP, vector<PP>, greater<PP> > que;
		que.push(PP(0, P(s, c)));
		while(!que.empty()){
			PP pp = que.top();
			que.pop();
			int cos = pp.first;
			int pos = pp.second.first;
			int num = pp.second.second;
			if(pos == d){
				cout << cos << endl;
				break;
			}

			if(cos > cost[pos][num]) continue;
			rep(i,(int)G[pos].size()){
				int v = G[pos][i].first;
				int fee = G[pos][i].second;
				if(cost[v][num] > cos + fee){
					cost[v][num] = cos + fee;
					que.push(PP(cost[v][num], P(v, num)));
				}
				if(num > 0 && cost[v][num-1] > cos + fee / 2){
					cost[v][num-1] = cos + fee / 2;
					que.push(PP(cost[v][num-1], P(v, num-1)));
				}
			}
		}
	}
	return 0;
}