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 <iostream>
#include <vector>
#include <queue>
#include <functional>

constexpr int MAX_N=100,INF=(int)1e9;

struct edge {
	int to,cost;
	edge()=default;
	edge(int _t,int _c) : to(_t),cost(_c) {}
};

struct Node{
	int v,num,cost;
	Node()=default;
	Node(int _v,int _n,int _c) : v(_v),num(_n),cost(_c) {}

	bool operator<(const Node& rhs) const{
		return cost==rhs.cost ? v < rhs.v : cost < rhs.cost; 
	}
	bool operator>(const Node& rhs) const{
		return cost==rhs.cost ? v > rhs.v : cost > rhs.cost;
	}
};

int c,n,m,s,g;

std::vector<edge> G[MAX_N];

int d[11][MAX_N];

int dijkstra() {

	for(int i=0;i<=c;++i){
		for(int j=0;j<n;++j)
			d[i][j]=INF;
	}

	std::priority_queue<Node,std::vector<Node>,std::greater<Node>> que;

	d[c][s]=0;
	que.emplace(s,c,0);

	while(!que.empty()){

		Node node=que.top();
		que.pop();

		int v=node.v;

		if(v==g)
			return d[node.num][g];

		if(node.cost>d[node.num][v])
			continue;

		for(int i=0;i<G[v].size();++i){
			edge e=G[v][i];
			if(node.num==0)
				goto a;
			else{
				if(d[node.num-1][e.to]>d[node.num][v]+e.cost/2){
					d[node.num-1][e.to]=d[node.num][v]+e.cost/2;
					que.emplace(e.to,node.num-1,d[node.num-1][e.to]);
				}
				a:;
				if(d[node.num][e.to]>d[node.num][v]+e.cost){
					d[node.num][e.to]=d[node.num][v]+e.cost;
					que.emplace(e.to,node.num,d[node.num][e.to]);
				}
			}
		}
	}
	return -1;
}

int main() {
	while(1){
		std::cin>>c>>n>>m>>s>>g;
		if(c+n+m+s+g==0)
			return 0;
		--s;
		--g;
		int a,b,f;
		for(int i=0;i<m;++i){
			std::cin>>a>>b>>f;
			--a;
			--b;
			G[a].emplace_back(b,f);
			G[b].emplace_back(a,f);
		}
		int ans=dijkstra();
		std::cout<<ans<<std::endl;
		for(int i=0;i<n;++i)
			G[i].clear();
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;

class State{
public:
  int v, c, t;
  State(int _v, int  _c, int _t)
    :v(_v), c(_c), t(_t) {}
  bool operator < (const State& st) const{
    return this->c > st.c;
  }
};


int main(){

  const int INF = 1 << 27;
 
  int ticket, n, m, start, goal;
  while(cin >> ticket >> n >> m >> start >> goal){
    if((ticket|n|m|start|goal) == 0) break;
    --start;
    --goal;

    int s, dd, score;
    vector<vector<vector<int> > >cost(n, vector<vector<int> >(n, vector<int>(ticket +1, INF*2)));
    for(int i=0; i<m; i++){
      cin >> s >> dd >> score;
      --s;
      --dd;
      cost[s][dd][0] = cost[dd][s][0] = score;
      for(int j=1; j<ticket; j++) cost[s][dd][j] = cost[dd][s][j] = score;      
      //cout << cost[s][dd][0] << endl;
    }

    priority_queue<State> req;
    vector<vector<int> > d(n, vector<int>(ticket+1, INF));
    for(int i=0; i<=ticket; i++){
      d[start][i] = 0;
 
    }
    req.push(State(start, 0, ticket));

    while(! req.empty()){
      State st = req.top();
      req.pop();

      //cout << "v:" << st.v+1 <<" c:" << st.c << " t:" << st.t << endl;

      if(st.v == goal){
	   cout << st.c << endl;
	   break;
      }

      for(int i=0; i<n; i++){
	//use ticket
	if(st.t>0 && d[st.v][st.t] + cost[st.v][i][st.t-1]/2 < d[i][st.t-1]){
	  d[i][st.t-1] = st.c + cost[st.v][i][st.t-1]/2;
	  req.push(State(i, d[i][st.t-1], st.t-1));
	}
	//not use ticket
	if(d[st.v][st.t] + cost[st.v][i][st.t] < d[i][st.t]){
	  d[i][st.t] = st.c + cost[st.v][i][st.t];
	  req.push(State(i, d[i][st.t], st.t));
	}
      }
	
      
    }
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
#define MAX_N 100

typedef pair<int, int> pi;
typedef pair<int, pi> P;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

struct edge{ int to, cost;};

vector<vector<edge> >G(MAX_N);
int c, n, m;
int cost[MAX_N][MAX_N];
int d[MAX_N];

void dijkstra(int s){
  priority_queue<P, vector<P>, greater<P> >q;
  rep(i, n)d[i] = INF;
  d[s] = 0;
  q.push(P(0, pi(s, c)));

  while(!q.empty()){
    P p = q.top(); q.pop();
    //    printf("%d %d %d\n", p.F, p.S.F, p.S.S);
    int v = p.S.F;
    //    if(d[v] < p.first) continue;
    rep(i, G[v].size()){
      edge e = G[v][i];
      if(d[e.to] > d[v] + e.cost){
	d[e.to] = d[v] + e.cost;
	q.push(P(d[e.to], pi(e.to, p.S.S)));
      }
      if(p.S.S && d[e.to] > d[v] + e.cost/2){
	d[e.to] = d[v] + e.cost/2;
	//	d[v][1]--;
	q.push(P(d[e.to], pi(e.to, p.S.S-1)));
      }
    }
  }      
}

int main(){
  int s, g;
  while(scanf("%d%d%d%d%d", &c, &n, &m, &s, &g) && c+n+m+s+g){
    s--; g--;
    G.clear();
    G.resize(n);
    int a, b, f;
    rep(i, m){
      scanf("%d%d%d", &a, &b, &f);a--;b--;
      edge e; e.to = b; e.cost = f;
      G[a].PB(e);
      e.to = a;
      G[b].PB(e);
    }
    dijkstra(s);
    printf("%d\n", d[g]);
  }
  return 0;
}