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)
		}
		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)
		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)
					}
				}
			}
		}
	}
}

C++: #include <iostream>
#include <queue>
#define INF (1e9)
#define N 101
#define C 11
using namespace std;
void dijkstra(int);
int M[N][N],d[N][C],c,n;
typedef struct data{
  int cost,pos,ticket;
  bool operator<(const data & right)const{
    return cost > right.cost;
  }
} data;
int main(){
  int m,s,id,a,b,f;
  while(1){
    cin >> c >> n >> m >> s >> id;
    if(c==0&&n==0&&m==0&&s==0&&id==0) break;
    for(int i=0;i<=n;i++)
      for(int j=0;j<=n;j++)
	M[i][j]=INF;
    for(int i=0;i<m;i++){
      cin >> a >> b >> f;
      M[a][b]=M[b][a]=f;
    }
    dijkstra(s);
    cout << d[id][c] << endl;
  }
  return 0;
}
void dijkstra(int s){
  priority_queue<data> q;
  for(int i=1;i<=n;i++)
    for(int j=0;j<=c;j++)
      d[i][j]=INF;
  d[s][0]=0;
  q.push((data){0,s,0});
  while(!q.empty()){
    data t=q.top();
    q.pop();
    int cost=t.cost,pos=t.pos,ticket=t.ticket;
    if(d[pos][ticket]>cost) continue;
    for(int i=1;i<=n;i++){
      if(d[i][ticket]>d[pos][ticket]+M[pos][i]){
	d[i][ticket]=d[pos][ticket]+M[pos][i];
	q.push((data){d[i][ticket],i,ticket});
      }
      if(ticket==c) continue;
      if(d[i][ticket+1]>d[pos][ticket]+M[pos][i]/2){
	d[i][ticket+1]=d[pos][ticket]+M[pos][i]/2;
	q.push((data){d[i][ticket+1],i,ticket+1});
      }
    }
  }
}

C++: #include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <map>

using namespace std;

struct node
{
	int ticket, si;
	int cost;
	bool det;
};

struct comparator : public binary_function<node *, node *, bool>
{
	bool operator() (node *x, node *y) const { return x->cost > y->cost; }
};

typedef pair<int, int> edge_t;
typedef list<edge_t> edgelist_t;

static node nodes[11][101];
static map<int, edgelist_t > edges;

int main()
{
	int C,N,M,S,D;
	while(cin>>C>>N>>M>>S>>D, C&&N&&S&&M&&D)
	{
		for (int c = 0; c <= C; c++)
		{
			for (int i = 1; i <= N; i++)
			{
				nodes[c][i].ticket = c;
				nodes[c][i].si = i;
				nodes[c][i].cost = 1 << 16;
				nodes[c][i].det = false;
			}
		}

		edges.clear();
		for (int i = 0; i < M; i++)
		{
			int a,b,f;
			cin>>a>>b>>f;
			edges[a].push_back(make_pair(b, f));
			edges[b].push_back(make_pair(a, f));
		}

		// Dijkstra
		priority_queue<node *, vector<node *>, comparator> queue;
		nodes[C][S].cost = 0;
		queue.push(&nodes[C][S]);

		int min_cost = 1 << 16;
		while (!queue.empty())
		{
			node *n = queue.top();
			queue.pop();
			if (n->det) continue;
			n->det = true;

			if (n->si == D) min_cost = min(min_cost, n->cost);

			edgelist_t &l = edges[n->si];
			edgelist_t::const_iterator it;
			for (it = l.begin(); it != l.end(); ++it)
			{
				node *n2 = &nodes[n->ticket][(*it).first];
				int cost = (*it).second;

				if (!n2->det)
				{
					n2->cost = min(n2->cost, n->cost + cost);
					queue.push(n2);
				}
				if (n->ticket > 0)
				{
					n2 = &nodes[n->ticket - 1][(*it).first];
					cost = (*it).second / 2;
					if (!n2->det)
					{
						n2->cost = min(n2->cost, n->cost + cost);
						queue.push(n2);
					}
				}
			}
		}
		cout << min_cost << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<climits>

using namespace std;

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

struct Edge{
  int to,cost;
  Edge(int _to, int _cost):to(_to), cost(_cost){};
};

int minCost[101][11];
vector<Edge>edge[101];
static const int INF = INT_MAX;

int c,n,m,s,d;

int dijkstra(void){
  priority_queue<P, vector<P>,greater<P> >que;
  que.push(make_pair(0,make_pair(s,c))); 

  for(int i=0;i<101;i++)
    for(int j=0;j<11;j++)
      minCost[i][j]=INF;

  while(!que.empty()){
    P p=que.top();
    que.pop();

    for(int i=0;i<edge[p.second.first].size();i++){
      Edge e=edge[p.second.first][i];
      if(p.first+e.cost<minCost[e.to][p.second.second]){
	minCost[e.to][p.second.second]=p.first+e.cost;
	que.push(make_pair(p.first+e.cost,make_pair(e.to,p.second.second)));
      }
    }

    if(p.second.second==0)continue;

    for(int j=0;j<edge[p.second.first].size();j++){
      Edge e=edge[p.second.first][j];
      if(p.first+e.cost/2<minCost[e.to][p.second.second-1]){
	minCost[e.to][p.second.second-1]=p.first+e.cost/2;
	que.push(make_pair(p.first+e.cost/2,make_pair(e.to,p.second.second-1)));
      }
    }
  }
  int ans=INT_MAX;
  for(int i=0;i<11;i++)
    ans=min(minCost[d][i],ans);

  return ans;
}

int main(void){
  int a,b,f;

  while(cin >> c >> n >> m >> s >> d,c|n|m|s|d){
    
    for(int i=0;i<=n;i++)edge[i].clear();

    for(int i=0;i<m;i++){
      cin >> a >> b >>f;
      edge[a].push_back(Edge(b,f));
      edge[b].push_back(Edge(a,f));
    }
    cout << dijkstra() << endl;
  }

  return 0;
}