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 <algorithm>
#include <vector>
using namespace std;

class edge{
	public:
		int to,cost;
		edge();
		edge(int _to,int _cost){
			to = _to;
			cost = _cost;
		}
};
class state{
	public:
		int id,cost,ticket;
		state();
		state(int _id,int _cost,int _ticket){
			id = _id;
			cost = _cost;
			ticket = _ticket;
		}
		bool operator<(const state st)const{
			return cost > st.cost;
		}
};
const int INF = 1000000;

int main()
{
	int c,n,m,s,g;
	while(cin >> c >> n >> m >> s >> g){
		if(!c&&!n&&!m&&!s&&!g)
			break;
		vector<edge> t[101];
		int d[101][11];
		for(int i=0;i<m;i++){
			int a,b,cost;
			cin >> a >> b >> cost;
			a--;b--;
			t[a].push_back(edge(b,cost));
			t[b].push_back(edge(a,cost));
		}
		s--;g--;

		int ans = INF;
		for(int i=0;i<11;i++){
			fill(d[i],d[i]+100,INF);
		}
		d[s][c] = 0;
		priority_queue<state> que;
		que.push(state(s,0,c));
		while(!que.empty()){
			state st = que.top();
			que.pop();
			int v = st.id;
			if(st.id == g)
				ans = min(ans,st.cost);
			for(unsigned i=0;i<t[v].size();i++){
				edge e = t[v][i];
				
				if(d[e.to][st.ticket] > d[v][st.ticket] + e.cost){
					d[e.to][st.ticket] = d[v][st.ticket] + e.cost;
					que.push(state(e.to,d[e.to][st.ticket],st.ticket));
				}
				if(st.ticket>0){
					if(d[e.to][st.ticket-1] > d[v][st.ticket] + (e.cost)/2){
						d[e.to][st.ticket-1] = d[v][st.ticket] + (e.cost)/2;
						que.push(state(e.to,d[e.to][st.ticket-1],st.ticket-1));
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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, int p ) {
	if ( now == d ) {
		return 0;
	}
	if ( dp[ canUseT ][ now ] >= 0 ) {
		return dp[ canUseT ][ now ];
	}
	int res = INF;
	for ( int i = 0; i < v[now].size(); i++ ) {
		if ( v[ now ][ i ].first == p ) {
			continue;
		}
		if ( canUseT ) {
			res = min( res, solve( canUseT - 1, v[ now ][ i ].first, now ) + v[ now ][ i ].second / 2 );
		}
		res = min( res, solve( canUseT, v[ now ][ i ].first, now ) + 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, s );
		printf( "%d\n", ans );
		v.clear();
	}
	return 0;
}

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

#define INF 1010000000
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)
#define sint(i) scanf("%d",&i);
#define sintt(i,j) scanf("%d%d",&i,&j);
#define sinttt(i,j,k) scanf("%d%d%d",&i,&j,&k);

using namespace std;

class S{
	public:
	int p;
	int v;
	int n;
	
	S(int p,int v,int n):p(p),v(v),n(n){}
	
	bool operator<(const S&a)const{
		return v>a.v;
		return v==a.v ? v>a.v : n <a.n;
	}
};

int main(){
	while(1){
		int c,n,m,s,g;
		scanf("%d%d%d%d%d",&c,&n,&m,&s,&g);
		if(c==0&&n==0&&m==0&&s==0&&g==0)break;
		
		int root[101][101];
		rep(i,101)rep(j,101)root[i][j]=INF;
		
		rep(i,m){
			int a,b,d;
			sinttt(a,b,d);
			root[a][b]=d;
			root[b][a]=d;
		}
		
		priority_queue<S> qui;
		qui.push(S(s,0,0));
		int ans=0;
		
		int visit[101][11];
		rep(i,101)rep(j,11)visit[i][j]=0;
		
		while(1){
			if(qui.empty()==1){
				ans=-1;
				break;
			}
			
			S u=qui.top();qui.pop();
			
			if(u.p==g){
				ans=u.v;
				break;
			}
			
			//printf("%d %d %d\n",u.p,u.v,u.n);
			visit[u.p][u.n]=1;
			
			reps(i,n){
				if(root[u.p][i]!=INF){
					if(visit[i][u.n]==0){
						qui.push(S(i,u.v+root[u.p][i],u.n));
					}
					if(visit[i][u.n+1]==0 && visit[u.p][u.n+1]==0 && u.n+1 <= c){
						qui.push(S(i,u.v+root[u.p][i]/2,u.n+1));
					}
				}
			}
		}
		printf("%d\n",ans);
	}
}