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<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+8
#define rep(i,j) for(int i = 0; i < (j); i++)
#define reps(i,j,k) for(int i = j; i < k; i++)
typedef long long ll;
typedef unsigned long long ull;

struct edge{
	int to,cost,dis;
};

bool operator < (const edge &a,const edge &b){
    return a.cost > b.cost;
}

int main(){
	int c,n,m,s,g;
	while(scanf("%d%d%d%d%d",&c,&n,&m,&s,&g),n){
		int ans = 0;
		s--,g--;
		vector<edge> G[128];
		int d[128][32]={{}};
		rep(i,m){
			int a,b,cst;
			scanf("%d%d%d",&a,&b,&cst);
			a--,b--;
			edge in;
			in = {b,cst,-1};
			G[a].push_back(in);
			in = {a,cst,-1};
			G[b].push_back(in);
		}
		priority_queue<edge> PQ;
		edge start = {s,0,c};
		PQ.push(start);
		while(!PQ.empty()){
			edge pq = PQ.top();
			PQ.pop();
			if(d[pq.to][pq.dis]){
				d[pq.to][pq.dis]++;
				continue;
			}
			d[pq.to][pq.dis] = true;
			if(pq.to == g){
				printf("%d\n",pq.cost);
				break;
			}
			rep(u,G[pq.to].size()){
				edge next = {G[pq.to][u].to,pq.cost+G[pq.to][u].cost,pq.dis};
				PQ.push(next);
				if(pq.dis){
					next = {G[pq.to][u].to,pq.cost+G[pq.to][u].cost/2,pq.dis-1};
					PQ.push(next);
				}
			}
		}
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#define INF 10000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;

static const double eps = 1e-8;

int path[101][101];
int used[101][11];

void bfs(int s,int d,int n,int c){
	priority_queue<PP,vector<PP>,greater<PP> > que;
	que.push(PP(0,P(s,c)));
	used[s][c] = 0;

	while(!que.empty()){
		int cost = que.top().first;
		int src = que.top().second.first;
		int ticket = que.top().second.second;
		que.pop();

		for(int dst=1;dst<=n;dst++){
			if(used[dst][ticket] > cost + path[src][dst]){
				used[dst][ticket] = cost + path[src][dst];
				que.push(PP(cost + path[src][dst],P(dst,ticket)));
			}

			if(ticket > 0 && used[dst][ticket-1] > cost + path[src][dst]/2){
				used[dst][ticket-1] = cost + path[src][dst]/2;
				que.push(PP(cost + path[src][dst]/2,P(dst,ticket-1)));
			}
		}
	}
}

int main(){
	//c:number of tickets
	//n:number of towns
	//m:number of buses
	//s:source
	//d:distination
	int c,n,m,s,d;
	while(~scanf("%d %d %d %d %d",&c,&n,&m,&s,&d)){
		if(c==n && n==m && m==s && s==d && d==0) break;
		memset(path,0x3,sizeof(path));
		memset(used,0x3,sizeof(used));

		for(int i=0;i<m;i++){
			int a,b,f;
			scanf("%d %d %d",&a,&b,&f);
			path[a][b] = path[b][a] = min(path[a][b],f);
		}

		bfs(s,d,n,c);
		printf("%d\n",used[d][0]);
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <climits>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector< VI > VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

typedef pair<int, pair<int, int>> STATE;

const int INF = INT_MAX / 4;

int main(void) {
	int c, n, m, s, d;

	while (cin >> c >> n >> m >> s >> d, c) {
		s--; d--;

		VVI es(n, VI(n, INF));
		for (int i = 0; i < m; i++) {
			int a, b, f;
			cin >> a >> b >> f;
			es[a - 1][b - 1] = f;
			es[b - 1][a - 1] = f;
		}

		VVI distance(n, VI(c + 1, INF));	// node, ticket
		distance[s][c] = 0;
		priority_queue<STATE> q; // cost, node, ticket
		q.push(STATE(0, make_pair(s, c)));

		while (q.size()) {
			int cost = q.top().first;
			int node = q.top().second.first;
			int ticket = q.top().second.second;
			q.pop();

			if (cost > distance[node][ticket]) {
				continue;
			}

			for (int i = 0; i < n; i++) if (es[node][i] != INF) {
				int next_cost;

				// not use ticket
				next_cost = distance[node][ticket] + es[node][i];
				if (distance[i][ticket] > next_cost) {
					distance[i][ticket] = next_cost;
					q.push(STATE(next_cost, make_pair(i, ticket)));
				}

				// ticket - 1
				if (ticket) {
					next_cost = distance[node][ticket] + es[node][i] / 2;
					if (distance[i][ticket - 1] > next_cost) {
						distance[i][ticket - 1] = next_cost;
						q.push(STATE(next_cost, make_pair(i, ticket - 1)));
					}
				}
			}
		}

		cout << *min_element(distance[d].begin(), distance[d].end()) << endl;
	}

	return 0;
}