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]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)
					}
				}
			}
		}
		runtime.GC()
	}
}

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

#define loop(i, n) for (int i = 0; i < n; i++)
#define INF 1010001000

typedef pair<int, int> i_i;
typedef struct data{int pos, tick, cost;}data;

int dp(vector<vector<i_i > > &g);

int c, n, m, s, d;

int main(void)
{
    while (cin >> c >> n >> m >> s >> d, c) {
        n++;
        vector<vector<i_i > > g(n);
        loop(i, m) {
            int a, b, f;
            cin >> a >> b >> f;
            g[a].push_back(i_i(f, b));
            g[b].push_back(i_i(f, a));
        }
        cout << dp(g) << endl;
    }
    return 0;
}

int dp(vector<vector<i_i > > &g)
{
    vector<vector<int> > table(c+1, vector<int>(n, INF));
    queue<data> a;
    data dt = {s, c, 0};
    a.push(dt);
    while (!a.empty()) {
        data tmp = a.front(); a.pop();
        for (i_i i : g[tmp.pos]) {
            if (table[tmp.tick][i.second] > tmp.cost + i.first) {
                table[tmp.tick][i.second] = tmp.cost + i.first;
                dt.pos = i.second; dt.tick = tmp.tick; 
                dt.cost = tmp.cost + i.first;
                a.push(dt);
            }
            if (tmp.tick && 
                    table[tmp.tick-1][i.second] > tmp.cost + i.first / 2) {
                table[tmp.tick-1][i.second] = tmp.cost + i.first / 2;
                dt.pos = i.second; dt.tick = tmp.tick-1; 
                dt.cost = tmp.cost + i.first / 2;
                a.push(dt);
            }
        }
    }
    int ret = INF;
    for (int i = 0; i < c+1; i++) {
        ret = min(ret, table[i][d]);
    }
    return ret;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };



int c, n, m, s, d;
int dp[ 11 ][ 101 ];
static uint _root[ 100 ][ 100 ];

int solve( int now, int can_use, set<int> p ) {
	p.insert( now );
	if ( now == d ) {
		return 0;
	}
	if ( dp[ can_use ][ now ] >= 0 ) {
		return dp[ can_use ][ now ];
	}
	uint res = 1 << 30;
	for ( int i = 0; i < n; ++i ) {
		if ( p.count( i ) || _root[ now ][ i ] >= res ) {
			continue;
		}
		res = min( res, can_use ? min( solve( i, can_use - 1, p ) + _root[ now ][ i ] / 2, solve( i, can_use, p ) + _root[ now ][ i ] ) : solve( i, can_use, p ) + _root[ now ][ i ] );
	}
	return dp[ now ][ can_use ] = res;
}

int main() {
	while ( ~scanf("%d %d %d %d %d", &c, &n, &m, &s, &d) && n && m ) {
		--s;
		--d;
		for ( int i = 0; i < n; ++i ) {
			for ( int j = 0; j < n; j++ ) {
				_root[ i ][ j ] = 1 << 30;
			}
			_root[ i ][ i ] = 0;
		}
		for ( int i = 0; i < m; ++i ) {
			int a, b, f;
			scanf( "%d %d %d", &a, &b, &f );
			--a;
			--b;
			_root[ a ][ b ] = _root[ b ][ a ] = f;
		}
		NCLR( dp );
		int ans = solve( s, c, set<int>() );
		printf( "%d\n", ans );
	}
	return 0;
}

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

using namespace std;

#define INF 1000000000
#define MAX_N 100

class CDij{
public:
	int min_c;
	vector<int> vec;
};

int n;
int c,m;
int s,d;

queue<int> q;

int cost[MAX_N+1][MAX_N+1];

CDij dp[MAX_N+1];//sからiに行くまでの最小運賃

int main(){

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

		if(c+n+m+s+d==0) break;

		for(int i=0;i<=n;i++){
			for(int j=0;j<=n;j++){
				cost[i][j] = -1;
			}
		}

		for(int i=1;i<=n;i++){
			dp[i].min_c = INF;
			for(int j=0;j<dp[i].vec.size();j++){
				dp[i].vec.pop_back();
			}
		}

		for(int i=0;i<m;i++){
			int a,b,p;
			cin >> a >> b >>p;
			cost[a][b] = p;
			cost[b][a] = p;
		}

		q.push(s);
		dp[s].min_c = 0;
		dp[s].vec.push_back(s);

		while(!q.empty()){
			int now = q.front();
			q.pop();

			for(int i=1;i<=n;i++){
				if(cost[now][i] != -1){
					vector<int> root;
					root = dp[now].vec;
					root.push_back(i);

					vector<int> vec;

					int size = root.size()-1;

					for(int j=0;j<size;j++){
						vec.push_back( cost[root[j]][root[j+1]] );
					}
					sort(vec.begin(),vec.end(),greater<int>());

					size = vec.size();
					for(int j=0;j<min(c,size);j++){
						vec[j] /= 2;
					}

					int sum = 0;
					size = vec.size();

					for(int j=0;j<size;j++){
						sum+=vec[j];
					}

					if(sum < dp[i].min_c){
						dp[i].min_c = sum;
						dp[i].vec = root;
						q.push(i);
					}
				}
			}
	
		}

		cout << dp[d].min_c << endl;

	}
	return 0;
}