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 <algorithm>
#include <complex>
#include <queue>
#include <map>
using namespace std;

#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

#define EPS (1e-10)
#define EQ(a,b) (abs((a)-(b)) < EPS)

typedef long long ll;
struct edge {int to, cost;};
typedef pair<int, int> P;
typedef complex<double> C;

const int INF = 99999999;
const int MAX_V = 110;
const int MAX_E = 510;

int cost[MAX_V][MAX_V];
int d[MAX_V], d2[MAX_V];
bool used[MAX_V];
int pre[MAX_V], pre2[MAX_V];
int V, E;
int c, s, g;

void init_cost() {
    REP(i, V) REP(j, V) cost[i][j] = INF;
}

//s: start
void dijkstra(int s, int *d, int *pre) {
    REP(i, V) d[i] = INF;
    REP(i, V) used[i] = false;
    REP(i, V) pre[i] = -1;
    d[s] = 0;

    while (true) {
        int v = -1;
        REP(u, V) if (!used[u] && (v == -1 | d[u] < d[v])) v = u;

        if (v == -1) break;
        used[v] = true;

        REP(u, V) {
            if (d[u] > d[v] + cost[v][u]) {
                d[u] = d[v] + cost[v][u];
                pre[u] = v;
            }
        }
    }
}

//t: terminal
vector<int> get_path(int t, int *pre) {
    vector<int> path;
    for (; t != -1; t = pre[t]) path.pb(t);
    reverse(ALL(path));
    return path;
}

void solve() {
    dijkstra(s, d, pre);
    dijkstra(g, d2, pre2);

    int ans = INF;

    REP(k, V) {
        vector<int> cost_vec;

        vector<int> path = get_path(k, pre);
        REP(i, path.size() - 1) {
            cost_vec.pb(cost[path[i]][path[i+1]]);
        }

        path = get_path(k, pre2);
        REP(i, path.size() - 1) {
             cost_vec.pb(cost[path[i]][path[i+1]]);
        }

        sort(ALL(cost_vec));
        reverse(ALL(cost_vec));

        // REP(i, cost_vec.size()) printf("%d ", cost_vec[i]);
        // printf("\n");

        int sm = 0;
        REP(i, cost_vec.size()) {
            if (i < c)  sm += cost_vec[i] / 2;
            else        sm += cost_vec[i];
        }

        ans = min(ans, sm);
    }

    printf("%d\n", ans);

}

int main() {
    while (true) {
        scanf("%d%d%d%d%d", &c, &V, &E, &s, &g);
        if (!c) return 0;
        s--; g--;
        init_cost();
        REP(i, E) {
            int f, t, c;
            scanf("%d%d%d", &f, &t, &c);
            f--; t--;
            cost[f][t] = c;
            cost[t][f] = c;
        }
        solve();
    }
}

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

typedef pair<int, int> P;
typedef pair<P, int> State;

const int N = 100, INF = 1e8;

int edge[N][N];

int dijkstra(int n, int s, int d, int c){
	priority_queue<State, vector<State>, greater<State> > q;
	int visited[N][11] = {}, res;
	for(int i(0);i < N;++i){
		for(int j(0);j <= 10;++j){
			visited[i][j] = INF;
		}
	}
	q.push(State(P(0, s), c));
	while(!q.empty()){
		int c = q.top().first.first;
		int curr = q.top().first.second;
		int dc = q.top().second;
		q.pop();
        if(c > visited[curr][dc]) continue;
		for(int i(0);i < n;++i){
			if(edge[curr][i] != INF){
				int nc = c + edge[curr][i];
				int next = i;
				if(visited[next][dc] > nc){
                    visited[next][dc] = nc;
					q.push(State(P(nc, next), dc));
				}
				nc = c + edge[curr][next] / 2;
				if(dc > 0 && visited[next][dc - 1] > nc){
                    visited[next][dc - 1] = nc;
					q.push(State(P(nc, next), dc - 1));
				}
			}
		}
	}
	res = INF;
	for(int i(0);i < n;++i){
		res = min(res, visited[d][i]);
	}
    return res;
}

int main(){
	int c, n, m, s, d, a, b, f;
	while(cin >> c >> n >> m >> s >> d, c && n && m && s && d){
		for(int i(0);i < N;++i){
			for(int j(i);j < N;++j){
				edge[i][j] = edge[j][i] = INF;
			}
		}
		--s;
		--d;
		for(int i(0);i < m;++i){
			cin >> a >> b >> f;
			--a;
			--b;
			edge[a][b] = edge[b][a] = f;
		}
        cout << dijkstra(n, s, d, c) << endl;
	}
    return 0;
}