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]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 <algorithm>
#include <numeric>
using namespace std;
#define NONE -1

// ÅZoHTõ - Ï
vector < vector <int> >	edges;
vector < vector <int> >	adj;

vector <int>	routes;
vector <int>	halfs;
int	kotae;
int	goal;
int coupon;

// ÅZoHTõ - Ö
void search_routes( int now )
{
	if ( kotae != NONE )
	{
		int half = accumulate( halfs.begin(), halfs.end(), 0 );
		if ( half >= kotae )
			return;
	}

	routes.push_back( now );
	if ( now == goal )
	{
		vector <int> r;
		for ( vector <int>::iterator it = routes.begin(); it != routes.end()-1; it++ )
		{
			r.push_back( edges[*(it+1)][*it] );
		}
		sort( r.rbegin(), r.rend() );
		for ( int i = 0; i < min( coupon, (int)r.size() ); i++ )
		{
			r[i] /= 2;
		}
		int sum = accumulate( r.begin(), r.end(), 0 );
		if ( kotae == NONE )
			kotae = sum;
		else
			kotae = min( sum, kotae );
	}
	else
	{
		for ( vector <int>::iterator it = adj[now].begin(); it != adj[now].end(); it++ )
		{
			if ( find( routes.begin(), routes.end(), *it ) == routes.end() )
			{
				halfs.push_back( edges[now][*it]/2 );
				search_routes( *it );
				halfs.pop_back();
			}
		}
	}
	routes.pop_back();
}

int main( void )
{
	// c: øÌ 1`10
	// n: oXªÂÈ®¬Ì 2`100
	// m: oXÌHü 1`500
	// s: start
	// d: goal
	int c, n, m, s, d;
	while ( cin >> c >> n >> m >> s >> d && c && n && m && s && d )
	{
		edges	=	vector < vector <int> >( 501, vector <int>(501, NONE) );
		adj		=	vector < vector <int> >( 501, vector <int>() );

		s--;
		d--;

		// ú»
		for ( int i = 0; i < m; i++ )
		{
			// f: a-bÔÌRXg
			int a, b, f;
			cin >> a >> b >> f;

			a--;
			b--;

			edges[a][b] = f;
			edges[b][a] = f;
			adj[a].push_back( b );
			adj[b].push_back( a );
		}

		routes	=	vector <int>(0);
		halfs	=	vector <int>(0);
		kotae	=	NONE;
		goal	=	d;
		coupon	=	c;

		search_routes( s );
		cout << kotae << endl;

	}

	return 0;
}

C++: #include <iostream>
#include <queue>
#include <climits>
#include <vector>
using namespace std;
typedef pair<pair<int,int>,int> NODE;
#define cost first.first
#define ticket first.second 
#define pos second
#define ppp(a,b,c) make_pair(make_pair(a,b),c)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF (1<<21)

int info[100][100];
int n;

int dijk(int a,int b,int c){
	priority_queue<NODE,vector<NODE>,greater<NODE> > que;
	int memo[100][11];
	rep(i,100)rep(j,11)memo[i][j] = INF;
	//rep(j,11)memo[a][j] = 0;
	
	que.push(ppp(0,c,a));
	while( !que.empty() ){
		NODE q = que.top();que.pop();
		
		if(memo[q.pos][q.ticket]<=q.cost)continue;
		
		if(q.pos == b)return q.cost;
		
		memo[q.pos][q.ticket] = q.cost;
		
		for(int i=0;i<n;i++){
			que.push(ppp(info[q.pos][i]+q.cost ,q.ticket,i));
			if(q.ticket)que.push(ppp(info[q.pos][i]/2+q.cost ,q.ticket-1,i));

		}
	}
	
	/*ツ個ゥツづつつゥツづァツづ按つゥツづつス*/
	return -1;
}
int main(){
	int c,m,s,d,a,b,f;
	while(cin >> c >> n >> m >> s >> d ,c ){
		s--;d--;
		rep(i,100)rep(j,100)info[i][j]=INF*2;
		
		for(int i=0;i<m;i++){
			cin >> a >> b >> f;
			a--;b--;
			info[a][b] = info[b][a] = f;
		}
		cout << dijk(s,d,c) << endl;
	}
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int c,n,m,s,d;
int rosen[101][101] = {-1};
int saiki(int p,vector<int> st,vector<bool> u){
    int ans = INF;
    if(p == d){
	sort(RALL(st));
	REP(i,c){
	    st[i] /= 2;
	}
	int sum = 0;
	REP(i,st.size()){
	    sum += st[i];
	}
	return sum;
    }
    FOR(i,1,n+1){
	if(!u[i] && rosen[p][i] > 0){
	    vector<int> temp = st;
	    vector<bool> utemp = u;
	    temp.PB(rosen[p][i]);
	    utemp[i] = true;
	    ans = min(ans,saiki(i,temp,utemp));
	}
    }
    return ans;
}

int main(){
    while(true){
	cin >> c >> n >> m >> s >> d;
	if(c==0&&n==0&&m==0&&s==0&&d==0) break;
	memset(rosen,-1,sizeof(rosen));
	REP(i,m){
	    int a,b,f;
	    cin >> a >> b >> f;
	    rosen[a][b] = f;
	    rosen[b][a] = f;
	}
	vector<bool>  used(n+1,false);
	vector<int> nanika;
	used[s] = true;
	cout << saiki(s, nanika ,used) << endl;
    }
    return 0;
}