Go: package main

import (
	"container/heap"
	"fmt"
	"runtime"
)

type Node struct {
	NowPosition          int
	DiscountTicketTCount int
	Amount               int
}

type PriorityQueueNode []*Node

func (pq PriorityQueueNode) Len() int {
	return len(pq)
}

func (pq PriorityQueueNode) Less(i, j int) bool {
	return pq[i].Amount < pq[j].Amount
}

func (pq PriorityQueueNode) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *PriorityQueueNode) Push(node interface{}) {
	*pq = append(*pq, node.(*Node))
}

func (pq *PriorityQueueNode) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	*pq = old[0 : n-1]
	return item
}

type Point struct {
	NextPoint int
	Fee       int
}

type Memo struct {
	NowPoint             int
	DiscountTicketTCount int
}

func main() {
	var c, n, m, s, d int
	var a, b, f int

	for {
		fmt.Scan(&c, &n, &m, &s, &d)
		if c == 0 && n == 0 && m == 0 && s == 0 && d == 0 {
			break
		}
		s--
		d--
		pointMap := make([][]Point, n)
		for i := 0; i < n; i++ {
			pointMap[i] = make([]Point, 0, n)
		}
		for i := 0; i < m; i++ {
			fmt.Scan(&a, &b, &f)
			a--
			b--
			pointMap[a] = append(pointMap[a], Point{NextPoint: b, Fee: f})
			pointMap[b] = append(pointMap[b], Point{NextPoint: a, Fee: f})
		}
		pq := make(PriorityQueueNode, 0, m)
		start := Node{NowPosition: s, DiscountTicketTCount: c, Amount: 0}
		heap.Push(&pq, &start)
		memo := make(map[Memo]int)
		for len(pq) != 0 {
			now := heap.Pop(&pq).(*Node)
			if now.NowPosition == d {
				fmt.Println(now.Amount)
				break
			}
			memo[Memo{NowPoint: now.NowPosition, DiscountTicketTCount: now.DiscountTicketTCount}] = now.Amount
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				if memoAmount, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok || (ok && memoAmount > next.Amount) {
					memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}] = next.Amount
					heap.Push(&pq, next)
				}
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					if memoAmount, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok || (ok && memoAmount > next.Amount) {
						memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}] = next.Amount
						heap.Push(&pq, next)
					}
				}
			}
		}
		runtime.GC()
	}
}

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;
}

C++: #include <cstdio>
#include <utility>
#include <queue>
#include <algorithm>
#define INF (1<<27)
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int> _P; //point chicket
typedef pair<int,_P> P; //cost _P
struct Edge {
	int to,cost;
	Edge(int _to,int _cost) {to=_to,cost=_cost;}
};
int dist[11][100];
vector<Edge> edges[100];
int main() {
	int c,n,m,s,d;
	while(scanf("%d %d %d %d %d",&c,&n,&m,&s,&d),c) {
		rep(i,n) edges[i].clear();
		rep(i,c+1) rep(j,n) dist[i][j]=INF;
		rep(i,m) {
			int a,b,f;
			scanf("%d %d %d",&a,&b,&f);
			edges[a-1].push_back(Edge(b-1,f));
			edges[b-1].push_back(Edge(a-1,f));
		}
		priority_queue<P,vector<P>,greater<P> > Q;
		Q.push(P(0,_P(s-1,c)));
		dist[c][s-1]=0;
		while(!Q.empty()) {
			P p=Q.top(); Q.pop();
			_P _p=p.second;
			if(dist[_p.second][_p.first]<p.first) continue;
			rep(i,edges[_p.first].size()) {
				if(dist[_p.second][edges[_p.first][i].to]>(p.first+edges[_p.first][i].cost)) {
					dist[_p.second][edges[_p.first][i].to]=(p.first+edges[_p.first][i].cost);
					Q.push(P(p.first+edges[_p.first][i].cost,_P(edges[_p.first][i].to,_p.second)));
				}
				if(_p.second>0&&(dist[_p.second-1][edges[_p.first][i].to]>(p.first+edges[_p.first][i].cost/2))) {
					dist[_p.second-1][edges[_p.first][i].to]=(p.first+edges[_p.first][i].cost/2);
					Q.push(P(p.first+edges[_p.first][i].cost/2,_P(edges[_p.first][i].to,_p.second-1)));
				}
			}
		}
		int sum=1<<30;
		rep(i,c+1)if(sum>dist[i][d-1])sum=dist[i][d-1];
		printf("%d\n",sum);
	}
}