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 <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> >  Q;
	int memo[100][11];
	rep(i,100)rep(j,11)memo[i][j] = INF;
	rep(j,11)memo[a][j] = 0;
	
	Q.push(ppp(0,c,a));
	while( !Q.empty() ){
		NODE q = Q.top();Q.pop();
		for(int i=0;i<n;i++){
			if( info[q.pos][i]+q.cost < memo[i][q.ticket]){
				 memo[i][q.ticket] = info[q.pos][i]+q.cost;
				Q.push(ppp(info[q.pos][i]+q.cost ,q.ticket,i));
			}
			if( q.ticket && info[q.pos][i]/2+q.cost < memo[i][q.ticket-1] ){
					Q.push( ppp( info[q.pos][i]/2+q.cost , q.ticket-1, i));
				 memo[i][q.ticket-1] = info[q.pos][i]/2+q.cost;
			}
		}
	}
	int mi = (1<<21);
	rep(j,11)mi = min(memo[b][j],mi);
	return mi;
}
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>
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;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
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, ans;
vector<vector<PII> > v;
int dp[ 11 ][ 100 ];

int solve( int canUseT, int now, set<int> p ) {
	if ( now == d ) {
		return 0;
	}
	if ( dp[ canUseT ][ now ] >= 0 ) {
		return dp[ canUseT ][ now ];
	}
	p.insert( now );
	int res = INF;
	for ( int i = 0; i < v[now].size(); i++ ) {
		if ( p.count( v[ now ][ i ].first ) ) {
			continue;
		}
		if ( canUseT ) {
			res = min( res, solve( canUseT - 1, v[ now ][ i ].first, p ) + v[ now ][ i ].second / 2 );
		}
		res = min( res, solve( canUseT, v[ now ][ i ].first, p ) + v[ now ][ i ].second );
	}
	return dp[ canUseT ][ now ] = res;
}

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

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX 100000
#define MAX_NODE_ID 101
#define MAX_TICKET 11
class Node{
public:
	int id;
	int cost;
	int ticket;
	Node(int i,int c,int t){
		id=i;
		cost=c;
		ticket=t;
	}
	bool operator < (const Node &o) const{
		return cost > o.cost;
	}
};
typedef priority_queue<Node> PQ;
int table[MAX_NODE_ID][MAX_NODE_ID];
bool visited[MAX_NODE_ID][MAX_TICKET];
int main(){

	for(int c,n,m,s,d;cin>>c>>n>>m>>s>>d&&(c||n||m||s||d);){
		for(int i=1;i<MAX_NODE_ID;i++)for(int j=1;j<MAX_NODE_ID;j++)table[i][j]=MAX;
		for(int i=1,a,b,f;i<m&&cin>>a>>b>>f;i++){
			table[a][b]=f;
			table[b][a]=f;
//			cout<<"table["<<a<<"]["<<b<<"]"<<table[a][b]<<endl;
//			cout<<"table["<<b<<"]["<<a<<"]"<<table[b][a]<<endl;
		}
		PQ pq;

		pq.push(Node(s,0,c));
		while(!pq.empty()){
			int current_node=pq.top().id;
			int current_cost=pq.top().cost;
			int current_ticket=pq.top().ticket;
//			cout<<"current:"<<current_node<<endl;
			if(current_node==d)break;
			if(visited[current_node][current_ticket]);
			else{
				for(int i=1;i<=n;i++){
					if(current_node==i)continue;
					if(table[current_node][i]==MAX)continue;
	//				cout<<"["<<current_node<<"]["<<i<<"]"<<"cost:"<<table[current_node][i]<<endl;

					pq.push(Node(i,current_cost+table[current_node][i],current_ticket));
					if(current_ticket>0){
						pq.push(Node(i,current_cost+(table[current_node][i]/2),current_ticket-1));
					}
					visited[current_node][i]=true;
				}
			}
			pq.pop();
		}
		cout<<pq.top().cost<<endl;
	}
	return 0;
}