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<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
#define inf 1e9
typedef pair<mp,ll> mmp;

int main(){
    while(1){
	ll c,n,m,s,d;
	cin>>c>>n>>m>>s>>d;
	if(c==0)break;
	s--,d--;
	vector<vector<mp> > g(n);
	for(ll i=0;i<m;i++){
		ll a,b,f;
		cin>>a>>b>>f;
		a--;
		b--;
		g[a].push_back(mp(f,b) );
		g[b].push_back(mp(f,a) );
	}
	priority_queue<mmp,vector<mmp>,greater<mmp> > q;
	q.push(mmp(mp(0,0),s) );
	vector<vector<bool> > used(n,vector<bool>(c+1,false) );
	while(!q.empty() ){
		mmp now = q.top();
		q.pop();
		ll ncost = now.first.first;
		ll cc = now.first.second;
		ll nn = now.second;
		//cout<<nn<<' '<<cc<<endl;
		if(nn==d){
		    	//cout<<"ok"<<endl;
			cout<<ncost<<endl;
			break;
		}
		if(used[nn][cc]) continue;
		used[nn][cc] = true;
		for(ll i=0;i<g[nn].size();i++){
			ll next = g[nn][i].second;
			ll nec = g[nn][i].first;
			if(used[next][cc])continue;
			q.push(mmp(mp(ncost+nec,cc),next) );
			if(cc < c){
			    if(used[next][cc+1])continue;
				q.push(mmp(mp(ncost+nec/2,cc+1),next) );
			}
		}
	}


    }
	return 0;
}

C++: // AOJ 0212 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0212)
#include<cstdio>
#include<algorithm>
#include<limits>
#include<queue>
#include<vector>
#define rep(i,a) for(int i=0;i<(a);++i)

typedef std::pair<int, int> P;

const int MAX_N = 100, MAX_C = 10, INF = std::numeric_limits<int>::max()>>2;

struct St
{
	int v, dist, tick;

	St( int v, int dist, int tick )
	:	v(v), dist(dist), tick(tick)
	{}

	bool operator< ( const St &s ) const
	{ return dist > s.dist; }
};

int c, n, m, s, d;
std::vector<P> G[MAX_N];
int dp[MAX_N][MAX_C];

int main()
{
	while( scanf( "%d%d%d%d%d", &c, &n, &m, &s, &d ), c|n|m|s|d )
	{
		rep( i, n )
			G[i].clear();

		--s; --d;

		rep( i, m )
		{
			int a, b, f;
			scanf( "%d%d%d", &a, &b, &f );
			--a; --b;
			G[a].push_back( P( b, f ) );
			G[b].push_back( P( a, f ) );
		}

		std::priority_queue<St> pque;
		pque.push( St( s, 0, c ) );
		
		rep( i, n ) rep( j, c+1 )
			dp[i][j] = INF;
		dp[s][c] = 0;
		
		while( !pque.empty() )
		{
			St st = pque.top(); pque.pop();
			int v = st.v, dist = st.dist, t = st.tick;

			if( dp[v][t] < dist )
				continue;

			rep( i, G[v].size() )
			{
				P &e = G[v][i];

				if( dp[e.first][t] > dp[v][t]+e.second )
				{
					dp[e.first][t] = dp[v][t]+e.second;
					pque.push( St( e.first, dp[v][t]+e.second, t ) );
				}

				if( t && dp[e.first][t-1] > dp[v][t]+e.second/2 )
				{
					dp[e.first][t-1] = dp[v][t]+e.second/2;
					pque.push( St( e.first, dp[v][t]+e.second/2, t-1 ) );
				}
			}
		}

		printf( "%d\n", *std::min_element( dp[d], dp[d]+c+1 ) );
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <utility>
#include <tuple>
#include <queue>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#ifdef _DEBUG_
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl
#else
#define dump(val)
#endif

using namespace std;

typedef long long int ll;
typedef tuple<int, int> P;
typedef tuple<ll, int, int> T;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}

template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

template<typename T>
void chmin(T &a, T b) {
    a = min(a, b);
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int c, n, m, s, t;
    while (cin >> c >> n >> m >> s >> t, c + n + m + s + t) {
        s--;
        t--;
        vector<vector<P>> G(n);
        REP(i, 0, m) {
            int a, b, f;
            cin >> a >> b >> f;
            a--;
            b--;
            G[a].push_back(make_tuple(b, f));
            G[b].push_back(make_tuple(a, f));
        }
        const ll inf = 1LL << 60;
        auto dist = make_v(n, c + 1, inf);
        priority_queue<T, vector<T>, greater<T>> pq;
        // cost, ticket, vertex
        pq.emplace(0LL, c, s);
        REP(i, 0, c + 1) {
            dist[s][i] = 0;
        }
        while (pq.size()) {
            ll cost;
            int ticket, vertex;
            tie(cost, ticket, vertex) = pq.top();
            pq.pop();
            if (dist[vertex][ticket] < cost) continue;
            dist[vertex][ticket] = cost;
            for (auto p : G[vertex]) {
                int to, f;
                tie(to, f) = p;
                // use ticket
                if (ticket > 0 && dist[to][ticket - 1] > cost + f / 2) {
                    dist[to][ticket - 1] = cost + f / 2;
                    pq.emplace(dist[to][ticket - 1], ticket - 1, to);
                }
                // do not use ticket
                if (dist[to][ticket] > cost + f) {
                    dist[to][ticket] = cost + f;
                    pq.emplace(dist[to][ticket], ticket, to);
                }
            }
        }
        ll ans = inf;
        REP (i, 0, c + 1) {
            chmin(ans, dist[t][i]);
        }
        cout << ans << endl;
    }

    return 0;
}