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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

struct state {
	int p, t, c;
	state(int p, int t, int c) : p(p), t(t), c(c) {};
	bool operator<(const state &o)const {
		return o.c<c;
	}
};

int main() {
	int c, n, m, s, d;
	while(cin >> c >> n >> m >> s >> d, c) {
		vvi dist(n, vi(n, INF));
		int a, b, f;
		REP(i, m) {
			cin >> a >> b >> f;
			dist[a-1][b-1] = dist[b-1][a-1] = f;
		}
	

		priority_queue<state> Q;
		vvi cost(n, vi(c+1, INF));
		Q.push(state(s-1, 0, 0));
		cost[s-1][0] = 0;
		while(!Q.empty()) {
			state st = Q.top();
			Q.pop();

			if(st.p == d-1) {
				continue;
			}

			REP(i, n) {
				if(st.p != i) {
					if(st.t < c) {
						int nc = st.c + dist[st.p][i]/2;
						if(nc < cost[i][st.t+1]) {
							cost[i][st.t+1] = nc;
							Q.push(state(i, st.t+1, nc));
						}
					}
					int nc = st.c + dist[st.p][i];
					if(nc < cost[i][st.t]) {
						cost[i][st.t] = nc;
						Q.push(state(i, st.t, nc));
					}
				}
			}
		}

		int ans = INF;
		REP(i, c+1) {
			ans = min(ans, cost[d-1][i]);
		}

		cout << ans << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <vector>

using namespace std;

class Edge{
public:
	int from,to,cost;
	void Swap(){
		int a = from;
		from = to;
		to = a;
	}
};

vector<Edge> edges;

const int INF = 999999999;

int solve(int s,int g,int c){
	int d[101][11];
	bool flag[101][11];
	for(int i=0;i<101;i++){
		for(int j=0;j<11;j++){
			d[i][j] = INF;
			flag[i][j] = false;
		}
	}
	d[s][c] = 0;
	
	vector<Edge>::iterator eit;
	
	while(1){
		int no,nc;
		bool f = true;
		for(int i=0;i<101;i++){
			for(int j=c;j>=0;j--){
				if(d[i][j] != INF && flag[i][j] == false){
					if(f){
						no = i;
						nc = j;
					}else if(d[no][nc] > d[i][j]){
						no = i;
						nc = j;
					}
					f = false;
				}
			}
		}
		if(f){break;}
		flag[no][nc] = true;
		for(eit = edges.begin();eit != edges.end(); eit++){
			if(eit->from == no){
				if(d[eit->to][nc] > d[eit->from][nc] + eit->cost){
					d[eit->to][nc] = d[eit->from][nc] + eit->cost;
				}
				if(nc > 0){
					if(d[eit->to][nc - 1] > d[eit->from][nc] + eit->cost / 2){
						d[eit->to][nc - 1] = d[eit->from][nc] + eit->cost / 2;
					}
				}
			}
		}
	}
	int ans = INF;
	for(int i=0;i<c;i++){
		if(d[g][i] < ans){
			ans = d[g][i];
		}
	}
	return ans;
}

int main(){
	int c,n,m,s,d;
	int a,b,f;
	while(1){
		scanf("%d %d %d %d %d",&c,&n,&m,&s,&d);
		if(c == 0 && n == 0 && m == 0 && s == 0 && d == 0){
			break;
		}
		edges.clear();
		for(int i=0;i<m;i++){
			scanf("%d %d %d",&a,&b,&f);
			Edge e;
			e.from = a;
			e.to = b;
			e.cost = f;
			edges.push_back(e);
			e.Swap();
			edges.push_back(e);
		}
		printf("%d\n",solve(s,d,c));
	}
	return 0;
}

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

const int INF = 100000000;
const int C_MAX = 10;
const int N_MAX = 100;

//街の情報 <残りの割引券, 街の番号>
typedef pair<int, int> T;
//地点情報 <最小コスト, <残りの割引券, 街の番号>>
typedef pair<int, T> P;

//c: チケットの枚数(1 <= c <= 10)
//n: バスをつなぐ町の数(2 <= n <= 100)
//m: バスの路線数(1 <= m <= 500)
//s: 出発地点
//d: 目的地
int c, n, m, s, d;
//cost[町の番号][残りの割引券] = 費用
int cost[N_MAX][C_MAX + 1];

//すでにコストの最適値が決定済みかどうか
bool flag[N_MAX][C_MAX + 1];

//町間の料金
int line[N_MAX][N_MAX];

typedef priority_queue<P, vector<P>, greater<P> > Q;
Q dp_q;

int dijkstra(){
	while(!dp_q.empty()){
		P p = dp_q.top(); dp_q.pop();
		T &t = p.second;

		//p.first: 最小コスト
		//t.first: 残りの割引券
		//t.second: 町番号

		//既に最適値なのかどうかを調べる
		if(flag[t.second][t.first])	continue;
		flag[t.second][t.first] = true;

		if(t.second == d){
			return p.first;
		}

		//すべての頂点に対して割引券を使ったか使ってないかを決める
		for(int i = 0; i < n; ++i){
			//割引券を使わなかった場合のiのコスト
			//(t.secondでの最小コスト + t.second -> iの料金)が割引券がt.firstの時の町iのコストより小さい
			//時にc[i][t.first]を更新
			if(cost[i][t.first] > p.first + line[t.second][i]){
				cost[i][t.first] = p.first + line[t.second][i];
				dp_q.push(P(p.first + line[t.second][i], T(t.first, i)));
			}

			//割引券を使った場合のiのコスト
			//(t.secondでの最小コスト + t.second -> iの料金の半分)が
			//割引券がt.first - 1の時の町iのコストより小さい時に
			//c[i][t.first - 1]を更新
			if(t.first > 0 && cost[i][t.first - 1] > p.first + line[t.second][i] / 2){
				cost[i][t.first-1] = p.first + line[t.second][i] / 2;
				dp_q.push(P(p.first + line[t.second][i] / 2, T(t.first - 1, i)));
			}
		}
	}

	return INF;
}

void solve(){
	for(int i = 0; i < N_MAX; ++i){
		for(int j = 0; j < C_MAX; ++j){
			cost[i][j] = INF;
			flag[i][j] = false;
		}
	}

	dp_q = Q();
	dp_q.push(P(0, T(c, s)));

	cout << dijkstra() << endl;
}

int main(void){
	int a, b, f;

	while(true){
		cin >> c >> n >> m >> s >> d;
		if(!c && !n && !m && !s && !d)	break;
		s--; d--;
		
		for(int i = 0; i < N_MAX; ++i){
			for(int j = 0; j < N_MAX; ++j){
				line[i][j] = INF;
			}
		}

		for(int i = 0; i < m; ++i){
			cin >> a >> b >> f;
			a--;	b--;
			line[a][b] = f;
			line[b][a] = f;
		}

		solve();
	}
  return 0;
}