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]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 <cassert>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <numeric>
#include <algorithm>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
	for (const auto &e : vec) os << e << (&e == &vec.back() ? "" : " ");
	return os;
}

#ifdef _DEBUG
template <class T>
void dump(const char* str, T &&h) { cerr << str << " = " << h << "\n"; };
template <class Head, class... Tail>
void dump(const char* str, Head &&h, Tail &&... t) {
	while (*str != ',') cerr << *str++; cerr << " = " << h << "\n";
	dump(str + (*(str + 1) == ' ' ? 2 : 1), t...);
}
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else 
#define DMP(...) ((void)0)
#endif

template<class T = lint>
struct Edge {
	int to;
	T cost;
	Edge() {}
	Edge(int to, T cost) : to(to), cost(cost) {}
	bool operator>(const Edge &r) const { return this->cost > r.cost; }
};

template<class T>
vector<T> Dijkstra(vector<vector<Edge<T>>> &edges, int st) {

	int V = (int)edges.size();
	const T inf = numeric_limits<T>::max() / 2;
	vector<T> cost(V, inf);
	cost[st] = 0;

	priority_queue<Edge<T>, vector<Edge<T>>, greater<Edge<T>>> p_que;
	p_que.emplace(st, cost[st]);

	while (!p_que.empty()) {

		Edge<T> now(p_que.top().to, p_que.top().cost);
		p_que.pop();

		if (cost[now.to] < now.cost) continue;
		for (const Edge<T> &e : edges[now.to]) {
			T tmp_cost = now.cost + e.cost;
			if (cost[e.to] > tmp_cost) {
				cost[e.to] = tmp_cost;
				p_que.emplace(e.to, cost[e.to]);
			}
		}
	}

	return cost; // min cost to vertex idx from st
}

template<class T>
inline bool chmin(T &a, T b) { return a > b && (a = b, true); }

int main() {

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	while (true) {

		int c, n, m, s, d;
		cin >> c >> n >> m >> s >> d;
		if (c == 0) break;
		s--, d--;

		vector<vector<Edge<int>>> edges(n*(c + 1));
		for (int i = 0; i < m; i++) {
			int u, v, len;
			cin >> u >> v >> len;
			u--, v--;
			for (int j = 0; j <= c; j++) {
				edges[u * (c + 1) + j].emplace_back(v * (c + 1) + j, len);
				edges[v * (c + 1) + j].emplace_back(u * (c + 1) + j, len);
				if (j != c) {
					edges[u * (c + 1) + j].emplace_back(v * (c + 1) + (j + 1), len / 2);
					edges[v * (c + 1) + j].emplace_back(u * (c + 1) + (j + 1), len / 2);
				}
			}
		}

		auto cost = Dijkstra(edges, s * (c + 1));

		int ans = INF;
		for (int i = 0; i <= c; i++) chmin(ans, cost[d*(c + 1) + i]);

		cout << ans << "\n";
	}

	return 0;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int INF = 1e9;

int C, N, M, S, T;

class Cl{
  public:
    int n, c, cost;
    Cl(){}
    Cl(int nn, int cc, int co) { n = nn; c = cc; cost = co; }
    bool operator > (const Cl &tmp) const { return cost > tmp.cost; }
    bool operator < (const Cl &tmp) const { return cost < tmp.cost; }
};

int solve(vector< vector<P> > &v){
  priority_queue<Cl> open;
  open.push(Cl(S, C, 0));
  vector< vector<int> > closed(N, vector<int>(C + 1, INF));
  while(!open.empty()){
    Cl cl = open.top(); open.pop();
    if(closed[cl.n][cl.c] <= cl.cost) continue;
    closed[cl.n][cl.c] = cl.cost;
    REP(i, v[cl.n].size()){
      P nx = v[cl.n][i];
      open.push(Cl(nx.first, cl.c, cl.cost + nx.second));
      if(cl.c > 0) open.push(Cl(nx.first, cl.c - 1, cl.cost + nx.second / 2));
    }
  }
  int res = INF;
  REP(i, C) res = min(res, closed[T][i]);
  return res;
}

int main() {
  while(cin >>C >>N >>M >>S >>T && C && N && M && S && T){
    --T; --S;
    vector< vector<P> > v(N);
    REP(i, M){
      int a, b, f; cin >>a >>b >>f;
      --a; --b;
      v[a].push_back(P(b, f));
      v[b].push_back(P(a, f));
    }
    cout <<solve(v) <<endl;
  }
  return 0;
}

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

struct NODE{
	int x,t,c;
};
bool operator < (const NODE &a,const NODE &b){
	return a.c > b.c;
}
int main(){
	int c,n,m,s,d;
	while(cin >> c >> n >> m >> s >> d && c ){
		s--,d--;
		vector<NODE> g[110];
		for(int i = 0 ; i < m ; i++){
			int a,b,c;
			cin >> a >> b >> c;
			--a,--b;
			g[a].push_back({b,0,c});
			g[a].push_back({b,1,c/2});
			g[b].push_back({a,0,c});
			g[b].push_back({a,1,c/2});
		}
		priority_queue<NODE> Q;
		Q.push({s,c,0});
		int done[110][11] = {};
		while(Q.size()){
			NODE q = Q.top(); Q.pop();
			if( q.t < 0 ) continue;
			if( done[q.x][q.t]++ ) continue;
			if( q.x == d ){
				cout << q.c << endl;
				break;
			}
			for( auto e : g[q.x] ) Q.push({e.x,q.t-e.t,q.c+e.c});
		}
		
		
	}	
}