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]struct{})
		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}] = struct{}{}
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				if _, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok {
					heap.Push(&pq, next)
				}
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					if _, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok {
						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[11], 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(i, g[u].size()) {
					int v = g[u][i].fi, c = g[u][i].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;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using ll = long long;
using P = std::tuple<int,int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

int C, N, M, S, D;

vector<P> G[200];
int d[200][20];
priority_queue<P, std::vector<P>, std::greater<P>> pq;

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    while(true){
        std::cin >> C >> N >> M >> S >> D;

        if(C == 0){return 0;}
        
        for(int i=0;i<N+1;++i){
            G[i].clear();
        }
    
        for(int i=0;i<M;++i){
            int A, B, F;
            std::cin >> A >> B >> F;

            G[A].emplace_back(B, F, -1);
            G[B].emplace_back(A, F, -1);        
        }

        fill(&d[0][0], &d[0][0] + 200 * 20, 1001001001);
        d[S][0] = 0;
        pq.emplace(0, S, 0);

        while(!pq.empty()){
            int c, u, uu;
            tie(c, u, uu) = pq.top();
            pq.pop();

            if(d[u][uu] < c){continue;}
        
            for(P& p : G[u]){
            
                int v, c2;
                tie(v, c2, ignore) = p;

                // std::cout << u << " -> " << v << ", " << uu << ", " << d[v][uu+1] << ", " << d[u][uu] << ", " << (c2 / 2) << std::endl;
            
                if(d[v][uu] > d[u][uu] + c2){
                    // std::cout << "?" << u << " -> " << v << ", " << uu << ", " << d[v][uu+1] << ", " << d[u][uu] << ", " << (c2 / 2) << std::endl;
                    d[v][uu] = d[u][uu] + c2;
                    pq.emplace(d[v][uu], v, uu);
                }
                if(uu + 1 <= C && d[v][uu+1] > d[u][uu] + c2 / 2){
                    // std::cout << "!" << u << " -> " << v << ", " << uu << ", " << d[v][uu+1] << ", " << d[u][uu] << ", " << (c2 / 2) << std::endl;
                    d[v][uu+1] = d[u][uu] + c2 / 2;
                    pq.emplace(d[v][uu+1], v, uu+1);
                }
            }
        }

        int mn = 1001001001;
        for(int i=0;i<=C;++i){
            mn = min(mn, d[D][i]);
        }

        std::cout << mn << std::endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<queue>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
typedef pair<int,int> pii;
typedef pair<int,pii> pip;

class Dijkstra{
private:
    struct edge {int to,cost;};
    vector<vector<edge> > g;
    int n;

public:
    Dijkstra(int size){
        g.resize(size);
        n = size;
    }
    vector<vector<int> > dist;
    void add(int a, int b, int c){
        edge e1 = {b,c}, e2 = {a,c};
        g[a].push_back(e1);
        g[b].push_back(e2);
    }
    int calc(int s,int t,int ma){
        priority_queue<pip,vector<pip>,greater<pip> > q;
        int n = g.size();
        dist = vector<vector<int> > (n, vector<int>(ma+1,1e9));
        dist[s][0] = 0;
        q.push(pip(0,pii(s,0)));
        while(!q.empty()){
            pip p = q.top();
            q.pop();
            int c = p.first;
            pii node = p.second;
            if(dist[node.first][node.second] < c)continue;
            rep(i,g[node.first].size()){
                edge e = g[node.first][i];
                if(dist[e.to][node.second] > dist[node.first][node.second] + e.cost){
                    dist[e.to][node.second] = dist[node.first][node.second] + e.cost;
                    q.push(pip(dist[e.to][node.second],pii(e.to,node.second)));
                }
                if(node.second < ma){
                    if(dist[e.to][node.second+1] > dist[node.first][node.second] + e.cost/2){
                        dist[e.to][node.second+1] = dist[node.first][node.second] + e.cost/2;
                        q.push(pip(dist[e.to][node.second+1],pii(e.to,node.second+1)));
                    }
                }
            }
        }
        int ret = 1e9;
        rep(i,ma+1)ret = min(ret, dist[t][i]);
        return ret;
    }
};

int main(){
    int c,n,m,s,d;
    while(cin >> c >> n >> m >> s >> d, n){
        Dijkstra dij(n);
        rep(i,m){
            int a,b,f;
            cin >> a >> b >> f;
            dij.add(a-1,b-1,f);
        }
        cout << dij.calc(s-1,d-1,c) << endl;
    }
}