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<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int memo[11][100];

int main(){
  int c,n,m,s,d;
  while(scanf("%d%d%d%d%d",&c,&n,&m,&s,&d),c+n+m+s+d){
    int load[100][100] = {{ 0 }};
    priority_queue<pair<int,pair<int,int> > > pq;
    memset(memo, -1, sizeof(memo));
    s--; d--;
    REP(i,m){
      int a,b,f;
      scanf("%d%d%d",&a,&b,&f);
      load[a-1][b-1] = load[b-1][a-1] = f;
    }
    pq.push(mp(0,mp(c,s)));
    while(pq.size()){
      int pos  = pq.top().s.s;
      int dist = -pq.top().f;
      int chk  = pq.top().s.f;
      pq.pop();

      //printf("pq.size=%d, pos=%d, dist=%d, chk=%d\n",pq.size(),pos,dist,chk);
      if(pos == d){
        printf("%d\n",dist);
        break;
      }

      if(memo[chk][pos] != -1)
        continue;

      memo[chk][pos] = dist;

      REP(i,n){
        if(load[pos][i] != 0){
          int cost1 = dist + load[pos][i];
          int cost2 = dist + load[pos][i]/2;
          if(memo[chk][i] == -1)
            pq.push(mp(-cost1,mp(chk,i)));
          if(chk > 0 && memo[chk-1][i] == -1)
            pq.push(mp(-cost2,mp(chk-1,i)));
        }
      }
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

constexpr int INF = 1e9;

struct edge {
    int to, cost;
};

using edges = vector<edge>;
using graph = vector<edges>;

int main() {
    int C, N, M, S, D;
    while(cin >> C >> N >> M >> S >> D, N) {
        S--; D--;
        graph g(N);
        for(int i=0; i<M; ++i) {
            int a, b, f;
            cin >> a >> b >> f;
            a--; b--;
            g[a].push_back((edge){b, f});
            g[b].push_back((edge){a, f});
        }

        int res = INF;
        using state = tuple<int, int, int>;
        vector<vector<int>> d(N, vector<int>(C+1, INF));
        d[S][C] = 0;
        priority_queue<state, vector<state>, greater<state>> que;
        que.push(make_tuple(0, S, C));
        while(!que.empty()) {
            int cur, v, c;
            tie(cur, v, c) = que.top();
            que.pop();
            if(d[v][c] < cur) {
                continue;
            }
            if(v == D) {
                res = min(res, d[v][c]);
            }
            for(auto& e : g[v]) {
                if(c > 0 && d[e.to][c-1] > d[v][c] + e.cost / 2) {
                    d[e.to][c-1] = d[v][c] + e.cost / 2;
                    que.push(make_tuple(d[e.to][c-1], e.to, c-1));
                }
                if(d[e.to][c] > d[v][c] + e.cost) {
                    d[e.to][c] = d[v][c] + e.cost;
                    que.push(make_tuple(d[e.to][c], e.to, c));
                }
            }
        }
        cout << res << endl;
    }
}

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