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>
using namespace std;

struct State {
    int no_;
    int cost_;
    int ticket_;
    State(int no, int cost, int ticket) 
        : no_(no), cost_(cost), ticket_(ticket) {} 
    
};

struct Edge {
    int to_;
    int cost_;
    Edge(int to, int cost) 
        : to_(to), cost_(cost) {}
};

int c, n, m, s, d;

auto dijkstra(vector<vector<Edge>> &g, vector<vector<int>> &dm) -> int {
    auto comp = [](auto& lhs, auto& rhs){return lhs.cost_ > rhs.cost_;};
    priority_queue<State, vector<State>, decltype(comp)> que(comp);

    que.push(State(s, 0, c));
    for (auto &e : dm[s]) {
        e = 0;
    }
    while (!que.empty()) {
        State s = que.top();
        que.pop();
        int current_no = s.no_;
        int current_cost = s.cost_;
        int current_ticket = s.ticket_;
        for (auto &e : g[current_no]) {
            int to = e.to_;
            int cost = e.cost_;
            if (current_ticket > 0) {
                // not use ticket
                if (dm[to][current_ticket] > dm[current_no][current_ticket] + cost) {
                    que.push(State(to, dm[current_no][current_ticket] + cost, current_ticket));
                    dm[to][current_ticket] = dm[current_no][current_ticket] + cost;
                }
                // use ticket
                if (dm[to][current_ticket-1] > dm[current_no][current_ticket] + cost/2) {
                    que.push(State(to, dm[current_no][current_ticket] + cost/2, current_ticket-1));
                    dm[to][current_ticket-1] = dm[current_no][current_ticket-1] + cost/2;
                }
            }else {
                if (dm[to][current_ticket] > dm[current_no][current_ticket] + cost) {
                    que.push(State(to, dm[current_no][current_ticket] + cost, current_ticket));
                    dm[to][current_ticket] = dm[current_no][current_ticket] + cost;
                }
            }
        }
    }

    int ans = 1e8;
    for (auto &e : dm[d]) {
        ans = min(ans, e);
    }
    return ans;
}

auto main(int argc, char const *argv[]) -> int {
    constexpr int INF = 1e8;

    while(cin>>c>>n>>m>>s>>d, c||n||m||s||d) {
        vector<vector<Edge>> g(n+1);
        vector<vector<int>> dm(n+1);
        for (auto &e : dm) {
            for (int i = 0; i < c + 1; ++i) {
                e.push_back(INF);
            }
        }

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

        for (int i = 0; i <= n; ++i) {
            for (auto &e : g[i]) {
                cout<<i<<" -> "<<e.to_<<" : "<<e.cost_<<endl;
            }
        }
        cout<<dijkstra(g, dm)<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstring>
#include <queue>
#include <utility>
#define rep2(x,from,to) for(int x=(from);(x)<(to);(x)++)
#define rep(x,to) rep2(x,0,to)
using namespace std;
const int INF = 1<<25;
int main(){
	int c, n, m, s, d, a, b, f;
	
	while(cin >> c >> n >> m >> s >> d && (c || m || n || s || d)) {
		s--, d--;
		int cost[110][110] = {}, ret[11][110];
		rep(i,m) {
			cin >> a >> b >> f;
			a--, b--;
			cost[a][b] = cost[b][a] = f;
		}
		rep(i,c+1) {
			rep(j,n) { 
				ret[i][j] = INF;
			}
		}
		queue<pair<int, int> > q;
		q.push(make_pair(c,s));
		ret[c][s] = 0;
		while(!q.empty()) {
			int node = q.front().second, nc = q.front().first; q.pop();
			rep(i,n) {
				if(cost[node][i]) {
					if(ret[nc][i] > ret[nc][node] + cost[node][i]) {
						ret[nc][i] = ret[nc][node] + cost[node][i];
						q.push(make_pair(nc, i));
					}
					if(nc>0 && ret[nc - 1][i] > ret[nc][node] + cost[node][i] / 2) {
						 ret[nc - 1][i] = ret[nc][node] + cost[node][i] / 2;
						 q.push(make_pair(nc-1, i));
					}
				}
			}	
		}
		int res = INF;
		rep(i,c+1) {
			res = min(res,ret[i][d]);
		}
		cout << res << endl;
	}
}