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

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

using namespace std;

class Edge{
public:
    int to;
    int cost;
    Edge(int to,int cost):to(to),cost(cost){}
};

class Node{
public:
    int distance;
    int num;
    int ticket;
    Node(){}
    Node(int distance, int num,int ticket):distance(distance),num(num),ticket(ticket){}
    bool operator<(const Node &b)const{
        return distance > b.distance;
    }
};

vector<Edge> edge[101];
bool flg[101][11];

int c,n,m,s,d;

int solve(){

    priority_queue<Node> Q;
    Q.push(Node(0,s,0));
    int ans = 1000000;
    while(!Q.empty()){
        Node node = Q.top();Q.pop();
        if(flg[node.num][node.ticket]){
            continue;
        }
        flg[node.num][node.ticket] = true;
        //printf("num->%d cost->%d ticket->%d\n",node.num,node.distance,node.ticket);
        if(node.num == d){
            ans = min(ans,node.distance);
        }

        rep(i,edge[node.num].size()){
            Edge e = edge[node.num][i];
            int cost = node.distance + e.cost;
            Q.push(Node(cost,e.to,node.ticket));
            if(node.ticket < c){
                int cost = node.distance + e.cost/2;
                Q.push(Node(cost,e.to,node.ticket+1));
            }
        }
    }
    return solve();
}



void init(){
    rep(i,101){
        edge[i].clear();
        rep(k,11){
            flg[i][k] = 0;
        }
    }
}

int main(){

    while(cin >> c >> n >> m >> s >> d,!m==0){
        init();
        rep(i,m){
            int a,b,c;
            cin >> a >> b >> c;
            edge[a].push_back(Edge(b,c));
            edge[b].push_back(Edge(a,c));
        }
        cout << solve() << endl;
    }
}

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

using namespace std;

#define REP(i, n) for(int i=0; i<n; i++)

struct edge{
    int to, cost;
};

typedef pair<int, pair<int, int> > pii; // distance, edge, ticket

int INF = 10e8;
int c, n, m, s, d;

int dis[101][11];

int main(){
    while(1){
        cin >> c >> n >> m >> s >> d;

        if(n==0) break;

        vector<edge> g[101];
        REP(i, m){
            int a, b, f;
            cin >> a >> b >> f;
            edge ae;
            ae.to = b;
            ae.cost = f;
            g[a].push_back(ae);
            edge be;
            be.to = a;
            be.cost = f;
            g[b].push_back(be);
        }

        priority_queue<pii, vector<pii>, greater<pii> > que;
        REP(i, n+1){
            REP(j, c+1){
                dis[i][j] = INF;
            }
        }
        dis[s][c] = 0;
        pii init = make_pair(0, make_pair(s, c));
        que.push(init);

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

            int v = p.second.first;
            int t = p.second.second;
            if(dis[v][t]<p.first) continue;
            REP(i, g[v].size()){
                edge e = g[v][i];
                if(dis[e.to][t] > dis[v][t] + e.cost){
                    dis[e.to][t] = dis[v][t] + e.cost;
                    que.push(pii(make_pair(dis[e.to][t], make_pair(e.to, t))));
                }
                if(t>0 && dis[e.to][t-1] > dis[v][t] + e.cost / 2){
                    dis[e.to][t-1] = dis[v][t] + e.cost / 2;
                    que.push(pii(make_pair(dis[e.to][t-1], make_pair(e.to, t-1))));
                }
            }
        }
        /*
        REP(i, n+1){
            REP(j, c+1){
                cout << dis[i][j] << " ";
            }
            cout << endl;
        }
        */
        cout << dis[d][0] << endl;
    }
}

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

using namespace std;

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

struct state {
    int pos, cost;
    state (){}
    state (int pos_, int cost_): pos(pos_), cost(cost_) {}

    bool operator < (const state& o) const {
        return cost > o.cost;
    }
};

constexpr int INF = 1 << 29;

int C, M, N, S, D;

const int W = 100;
vector<edge> graph[1100];

int min_cost[1100];

bool solve() {
    cin >> C >> N >> M >> S >> D;
    if (C+N+M+S+D == 0) { return false; }
    --S; --D;

    for (int j = 0; j < M; ++j) {
        int a, b, f; cin >> a >> b >> f;
        --a; --b;
        for (int j = 0; j < C; ++j) {
            graph[a+j*W].emplace_back(b+j*W, f);
            graph[b+j*W].emplace_back(a+j*W, f);

            graph[a+j*W].emplace_back(b+(j+1)*W, f/2);
            graph[b+j*W].emplace_back(a+(j+1)*W, f/2);
        }
        graph[a+C*W].emplace_back(b+C*W, f);
        graph[b+C*W].emplace_back(a+C*W, f);
    }

    fill(min_cost, min_cost+1000, INF);
    min_cost[S] = 0;

    priority_queue<state, vector<state>> que;
    que.emplace(S, 0);

    while (!que.empty()) {
        state st = que.top(); que.pop();
        int pos = st.pos;
        int cost = st.cost;
        if (cost > min_cost[st.pos]) { continue; }

        for (edge& e : graph[pos]) {
            int next_cost = cost + e.cost;
            if (next_cost < min_cost[e.to]) {
                min_cost[e.to] = next_cost;
                que.emplace(e.to, next_cost);
            }
        }
    }

    int ans = INF;
    for (int j = 0; j <= C; ++j) {
        ans = min(ans, min_cost[D+j*W]);
    }
    cout << ans << endl;

    return true;
}

int main() {
    for (; solve(); ) ;
    return 0;
}