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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define INF (1<<20)
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> PP;
struct edge{ int to,cost;};
int C,N,M,S,D;
vector<edge> G[111];
int minc[111][11];
void solve(){
  memset(minc,-1,sizeof(minc));
  priority_queue< PP,vector<PP>,greater<PP> > q;
  q.push( PP(0,P(0,S)) );
  while(!q.empty()){
    PP p = q.top(); q.pop();
    int v=p.second.second;
    int m=p.second.first;
    if(minc[v][m]!=-1) continue;
    minc[v][m]=p.first;
    for(int i=0;i<G[v].size();i++){
      edge e=G[v][i];
      if(minc[e.to][m]==-1){
	q.push( PP(e.cost+p.first,P(m,e.to)) );
      }
      if(minc[e.to][m+1]==-1 && m<C){
	q.push( PP(e.cost/2+p.first,P(m+1,e.to)) );
      }
    }
  }
}

main(){
  while(1){
    scanf("%d %d %d %d %d",&C,&N,&M,&S,&D);
    if(C==0 && N==0 && M==0 && S==0 && D==0) break;
    for(int i=0;i<M;i++){
      int a,b,f;
      scanf("%d %d %d",&a,&b,&f);
      edge e;
      e.to=b;e.cost=f;
      G[a].push_back(e);
      e.to=a;
      G[b].push_back(e);
    }
    solve();
    int res=INF;
    for(int i=0;i<=C;i++){
      if(minc[D][i]==-1) continue;
      res=min(res,minc[D][i]);
    }
    printf("%d\n",res);
    for(int i=1;i<=N;i++)
      G[i].clear();
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;//cost, v, ticket
struct edge{
  int to, cost;
};

int main(int argc, char *argv[]){
  int c, n, m, s, d;
  while(std::cin >> c >> n >> m >> s >> d, n){
    s--;d--;
    vector<vector<edge> > g(n);
    for (int i = 0; i < m; i++) {
      edge e;
      int from;
      std::cin >> from >> e.to >> e.cost;
      e.to--;from--;
      g[from].push_back(e);
      g[e.to].push_back((edge){from, e.cost});
    }
    vector<vector<int> > dp(c + 1, vector<int>(n, 1e9));
    dp[0][s] = 0;
    priority_queue<piii, vector<piii>, greater<piii> > que;
    que.push(piii(0, pii(s, 0)));
    while(!que.empty()){
      int cost   = que.top().first;
      int v      = que.top().second.first;
      int ticket = que.top().second.second;
      que.pop();
      for (int i = 0; i < g[v].size(); i++) {
        edge e = g[v][i];
        if(dp[ticket][e.to] > dp[ticket][v] + e.cost){
          dp[ticket][e.to] = dp[ticket][v] + e.cost;
          que.push(piii(dp[ticket][e.to], pii(e.to, ticket)));
        }
        if(ticket + 1 <= c &&
           dp[ticket + 1][e.to] > dp[ticket][v] + e.cost/2){
          dp[ticket + 1][e.to] = dp[ticket][v] + e.cost/2;
          que.push(piii(dp[ticket + 1][e.to],
                        pii(e.to, ticket + 1)));
        }
      }
    }
    int ans = 1e9;
    for (int i = 0; i <= c; i++) {
      ans = min(ans, dp[i][d]);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <climits>

using namespace std;

#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n) 

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

struct Edge
{
	int src, dst;
	int weight;
	Edge(int src, int dst, int weight) : src(src), dst(dst), weight(weight) {}

	bool operator< (const Edge& e)const
	{
		return weight != e.weight ? weight > e.weight :
			src != e.src ? src < e.src : dst < e.dst;
	}
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void Dijkstra(const Graph& g, int s, vi& dist, vi& prev)
{
	int n = g.size();
	dist.assign(n, INT_MAX);
	dist[s] = 0;
	prev.assign(n, -1);
	
	priority_queue<Edge> Q;
	Q.push(Edge(-2, s, 0));

	while(!Q.empty()){
		Edge e = Q.top();
		Q.pop();
		if(prev[e.dst] != -1)
			continue;
		prev[e.dst] = e.src;

		rep(i, g[e.dst].size()){
			int pw = e.weight + g[e.dst][i].weight;
			if(dist[g[e.dst][i].dst] > pw){
				dist[g[e.dst][i].dst] = pw;
				Q.push(Edge(g[e.dst][i].src, g[e.dst][i].dst, pw));
			}
		}
	}
}


vi buildPath(const vi& prev, int t)
{
	vi path;
	for(int u = t; u >= 0; u = prev[u])
		path.push_back(u);
	reverse(path.begin(), path.end());
	return path;
}

int main()
{
	int c, n, m, s, d;
	while(scanf("%d%d%d%d%d", &c, &n, &m, &s, &d), c){
		--s; --d;
		Graph g(n*(c+1));
		rep(i, m){
			int a, b, f;
			scanf("%d%d%d", &a, &b, &f);
			--a; --b;

			rep(j, c+1){
				g[j*n+a].push_back(Edge(j*n+a, j*n+b, f));
				g[j*n+b].push_back(Edge(j*n+b, j*n+a, f));
				if(j){
					g[(j-1)*n+a].push_back(Edge((j-1)*n+a, j*n+b, f/2));
					g[(j-1)*n+b].push_back(Edge((j-1)*n+b, j*n+a, f/2));
				}
			}
		}

		vi dist, prev;
		Dijkstra(g, s, dist, prev);

		int ans = INT_MAX;
		rep(j, c+1){
			ans = min(ans, dist[j*n+d]);
		}
		printf("%d\n", ans);
	}

	return 0;
}