Go: package main

import (
	"container/heap"
	"fmt"
)

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 <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS  = 1e-9;

int C, N, M, S, D;
vector<pii> g[100];
int d[11][100];

int main() {
	while (cin >> C >> N >> M >> S >> D, C || N || M || S || D) {
		S--; D--;
		REP(i, N) g[i].clear();
		
		REP(i, M) {
			int a, b, f;
			scanf("%d %d %d", &a, &b, &f);
			a--; b--;
			g[a].push_back(pii(b, f));
			g[b].push_back(pii(a, f));
		}
		
		fill(d[0], d[C + 1], INF);
		
		priority_queue<pii, vector<pii>, greater<pii> > pq[11];
		
		pq[0].push(pii(0, S));
		d[0][S] = 0;
		
		REP(i, C + 1) {
			while (!pq[i].empty()) {
				pii now = pq[i].top(); pq[i].pop();
				int u = now.se, cost = now.fi;
				REP(j, g[u].size()) {
					int v = g[u][j].fi, c = g[u][j].se;
					if (i < C && chmin(d[i + 1][v], cost + c / 2)) pq[i + 1].push(pii(cost + c / 2, v));
					if (chmin(d[i][v], cost + c)) pq[i].push(pii(cost + c, v));
				}
			}
		}
		
		int ans = INF;
		REP(i, C + 1) chmin(ans, d[i][D]);
		cout << ans << endl;
	}
	return 0;
}

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