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<stdio.h>
#include<string.h>
#include<utility>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 1000000000
int C,N,M,S,D,g1[101][101],g2[101][101],s,t,c,d[11][101];
typedef struct P{int p,c,t;}P;
struct Order
{
	bool operator ()(P const& a, P const& b) const
	{
		return a.c>b.c;
	}
};

P tmp,tmp2;
priority_queue<P,vector<P>,Order>Q;
int main()
{
	for(;scanf("%d%d%d%d%d",&C,&N,&M,&S,&D),C;)
	{
		for(int i=0;i<=N;i++)
			for(int j=0;j<=N;j++)
				g1[i][j]=g2[i][j]=INF;
		for(int i=0;i<=C;i++)
			for(int j=0;j<=N;j++)
				d[i][j]=INF;
		for(int i=0;i<M;i++)
		{
			scanf("%d%d%d",&s,&t,&c);
			if(g1[s][t]>c)
			{
				g1[s][t]=g1[t][s]=c;
				g2[s][t]=g2[t][s]=c/2;
			}
		}
		d[C][S]=0;
		tmp.c=0;tmp.p=S;tmp.t=C;
		Q.push(tmp);
		for(;!Q.empty();)
		{
			tmp=Q.top(),Q.pop();
			for(int i=1;i<=N;i++)
			{
				int nc=tmp.c+g1[tmp.p][i];
				if(d[tmp.t][i]>nc)
				{
					tmp2.c=d[tmp.t][i]=nc;
					tmp2.p=i;
					tmp2.t=tmp.t;
					Q.push(tmp2);
				}
				nc=tmp.c+g2[tmp.p][i];
				if(tmp.t>0&&d[tmp.t-1][i]>nc)
				{
					tmp2.c=d[tmp.t-1][i]=min(d[tmp.t][i],nc);
					tmp2.p=i;
					tmp2.t=tmp.t-1;
					Q.push(tmp2);
				}
			}
		}
		printf("%d\n",d[0][D]);
	}
}

C++: #include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(void) {
	while(true) {
		ll c, n, m, s, d;
		cin>>c>>n>>m>>s>>d;
		if(c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break;
		--s;
		--d;
		vector<vector<P> > p(n);
		vector<vector<ll> > dir(n, vector<ll>(c + 1, INF * INF));
		REP(i, m) {
			int a, b, f;
			cin>>a>>b>>f;
			--a;
			--b;
			p[a].pb(P(b, f));
			p[b].pb(P(a, f));
		}
		priority_queue<pair<P, ll> > que;
		que.push(pair<P, ll>(P(0, s), c));
		dir[s][c] = 0;
		while(!que.empty()) {
			pair<P, ll> np = que.top();
			que.pop();
			if(dir[np.first.second][np.second] != np.first.first) continue;
			REP(i, p[np.first.second].size()) {
				if(dir[p[np.first.second][i].first][np.second] > dir[np.first.second][np.second] + p[np.first.second][i].second) {
					dir[p[np.first.second][i].first][np.second] = dir[np.first.second][np.second] + p[np.first.second][i].second;
					que.push(pair<P, ll>(P(dir[p[np.first.second][i].first][np.second], p[np.first.second][i].first), np.second));
				}
				if(np.second > 0 && dir[p[np.first.second][i].first][np.second - 1] > dir[np.first.second][np.second] + p[np.first.second][i].second / 2) {
					dir[p[np.first.second][i].first][np.second - 1] = dir[np.first.second][np.second] + p[np.first.second][i].second / 2;
					que.push(pair<P, ll>(P(dir[p[np.first.second][i].first][np.second - 1], p[np.first.second][i].first), np.second - 1));
				}
			}
		}
		ll res = INF * INF;
		REP(i, c + 1) {
			res = min(res, dir[d][i]);
		}
		cout<<res<<endl;
	}
}

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

#define N 100
#define C 50
#define INF 1000000007
#define rep(i, n) for(int i=0; i<n; ++i)

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+1) 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)));
			}
		}
	}
	return dist[t][c];
}


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;
}