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

using namespace std;

typedef pair<int, int> P;
const int N = 100, INF = 10e6;
int edge[N][N];

vector<int> dijkstra(int n, int s, int d){
    priority_queue<P> pq;
    vector<int> ret;
    int visited[n], now = d;
    fill(visited, visited + n, 0);
    pq.push(make_pair(1, s));
    visited[s] = 1;
    while(!pq.empty()){
        P t = pq.top();
        pq.pop();
        visited[t.second] = t.first;
        if(t.second == d) break;
        for(int i = 0;i < n;++i){
            if(edge[t.second][i] != INF && !visited[i]){
                pq.push(make_pair(t.first + 1, i));
            }
        }
    }
    while(now != s){
        int m = INF, t = now;
        for(int i = 0;i < n;++i){
            if(visited[i] == visited[now] - 1 && edge[now][i] != INF){
                m = min(m, edge[now][i]);
                t = m == edge[now][i] ? i : t;
            }
        }
        ret.push_back(m);
        now = t;
    }
    return ret;
}

int main(){
    int c, n, m, s, d;
    while(cin >> c >> n >> m >> s >> d, c){
        int a, b, f, res = 0;
        --s; --d;
        for(int i = 0;i < N;++i){
            fill(edge[i], edge[i] + N, INF);
        }
        for(int i = 0;i < m;++i){
            cin >> a >> b >> f;
            --a; --b;
            edge[a][b] = edge[b][a] = f;
        }
        vector<int> r = dijkstra(n, s, d);
        sort(r.begin(), r.end(), greater<int>());
        vector<int>::iterator vit = r.begin();
        for(;vit != r.end();++vit){
            if(c != 0){
                *vit = *vit / 2;
                --c;
            }
            res += *vit;
        }
        cout << res << endl;
    }
    return 0;
}

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

#define N 104
#define C 53
#define INF 1000000007
#define rep(i, n) for(int i=0; i<n; ++i)
#define min(a,b) (a>b?b:a)

struct Edge {
	int to, cost;
};

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

typedef vector<Edge> Graph;

Graph g[N];

int dist[N][C];
int n, c;
int dijkstra(int s, int t) {
	priority_queue<IP, vector<IP>, greater<IP> > q;
	rep(i, n) rep(j, c+2) dist[i][j] = INF;
	dist[s][0] = 0;
	q.push(IP(0, P(s, 0)));
	while(!q.empty()) {
		int cost = q.top().first, u = q.top().second.first;
		int p = q.top().second.second; q.pop();
		if(dist[u][p] < cost) continue;
		rep(i, g[u].size()) {
			Edge &e = g[u][i];
			if(dist[u][p] + e.cost < dist[e.to][p]) {
				dist[e.to][p] = dist[u][p] + e.cost;
				q.push(IP(dist[e.to][p], P(e.to, p)));
			}
			if(p < c && dist[u][p] + (e.cost / 2) < dist[e.to][p+1]) {
				dist[e.to][p+1] = dist[u][p] + (e.cost / 2);
				q.push(IP(dist[e.to][p+1], P(e.to, p+1)));
			}
		}
	}
	int ans = INF;
	rep(i, c+1) ans = min(ans, dist[t][i]);
	return ans;
}


int main() {
	int m, s, d;
	while(cin >> c >> n >> m >> s >> d && c && n && m && s && d) {
		--s; --d;
		rep(i, n) g[i].clear();
		rep(i, m) {
			int a, b, f;
			cin >> a >> b >> f; --a; --b;
			g[a].push_back((Edge){b, f});
			g[b].push_back((Edge){a, f});
		}
		cout << dijkstra(s, d) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <algorithm>
#define INF 9999999
using namespace std;

class State {
public:
  int id;
  int cost;
  int tiket;
  bool operator <(const State &s) const {
    return cost > s.cost;
  }
  State(int id, int cost, int tiket) : id(id), cost(cost), tiket(tiket) {}
};

int main() {
  while(1) {
    int c, n, m, s, d;
    cin >> c >> n >> m >> s >> d;
    if(c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break;
    int data[n+1][n+1];
    fill(&data[0][0], &data[n][n]+1, INF);
    for(int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      data[a][b] = data[b][a] = f;
    }

    priority_queue<State> PQ;
    PQ.push(State(s, 0, c));
    int visited[n+1][c+1];
    int cost[n+1][c+1];
    fill(&visited[0][0], &visited[n][c] + 1, false);
    fill(&cost[0][0], &cost[n][c] + 1, INF);
    cost[s][c] = 0;
    while(!PQ.empty()) {
      State s = PQ.top();
      PQ.pop();
      visited[s.id][s.tiket] = true;
      for(int i = 1; i <= n; i++) {
	if(!visited[i][s.tiket] && data[s.id][i] != INF) {
	  int newValue = cost[s.id][s.tiket] + data[s.id][i];
	  if(newValue < cost[i][s.tiket]) {
	    cost[i][s.tiket] = newValue;
	    PQ.push(State(i, newValue, s.tiket));
	  }
	}
	if(s.tiket >= 1 && !visited[i][s.tiket-1] && data[s.id][i] != INF) {
	  int newValue = cost[s.id][s.tiket] + data[s.id][i]/2;
	  if(newValue < cost[i][s.tiket-1]) {
	    cost[i][s.tiket-1] = newValue;
	    PQ.push(State(i, newValue, s.tiket-1));
	  }
	}
      }
    }
    cout << cost[d][0] << endl;
  }
  return 0;
}