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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<eps)
struct edge
{
	int to,cost;
	edge(int to,int cost):to(to),cost(cost){}
};
int c,n,m,s,d;
int id(int index,int rem)
{
	return ((c+1)*index+rem);
}
vector<edge> g[2000];
int dist[2000];
void dijkstra()
{
	for(int i=0;i<2000;i++)dist[i]=INF;
	dist[id(s,c)]=0;
	priority_queue<P,vector<P>,greater<P> > q;
	q.push(P(0,id(s,c)));
	while(!q.empty())
	{
		P a=q.top();
		q.pop();
		int v=a.sec;
		if(dist[v]<a.fi)continue;
		for(int i=0;i<g[v].size();i++)
		{
			edge e=g[v][i];
			if(dist[e.to]>dist[v]+e.cost)
			{
				dist[e.to]=dist[v]+e.cost;
				q.push(P(dist[e.to],e.to));
			}
		}
	}
	return;
}
int main()
{
	while(1)
	{
		scanf("%d %d %d %d %d",&c,&n,&m,&s,&d);
		if(c==0&&n==0&&m==0&&s==0&&d==0)break;
		s--;d--;
		for(int i=0;i<2000;i++)g[i].clear();
		for(int i=0;i<m;i++)
		{
			int A,B,C;
			scanf("%d %d %d",&A,&B,&C);
			A--;B--;
			for(int j=0;j<=c;j++)
			{
				g[id(A,j)].pb(edge(id(B,j),C));
				g[id(B,j)].pb(edge(id(A,j),C));
			}
			for(int j=1;j<=c;j++)
			{
				g[id(A,j)].pb(edge(id(B,j-1),C/2));
				g[id(B,j)].pb(edge(id(A,j-1),C/2));
			}
		}
		dijkstra();
		int ans=INF;
		for(int i=0;i<=c;i++)ans=min(ans,dist[id(d,i)]);
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include <iostream>
#include <array>

using namespace std;
#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)
#define eacha(e,c) for(auto &e:c)

struct edge {
    int from,to,cost;
};

array<edge,500> E;
array<array<int,11>,101> dp;
int c,n,m,s,d;

int main(){
    while(cin>>c>>n>>m>>s>>d, c|n|m|s|d){
        rep(i,m) cin>>E[i].from>>E[i].to>>E[i].cost;
        rep(i,c+1){
            dp[i].fill(10000000);
            dp[i][s]=0;
            bool end;
            while(1){
                end=1;
                rep(j,m){
                    edge& e=E[j];
                    rep(dir,2){
                        int nd=dp[i][e.from]+e.cost;
                        if(i!=0) nd=min(dp[i-1][e.from]+e.cost/2,nd);
                        if(nd<dp[i][e.to]) dp[i][e.to]=nd,end=false;
                        swap(e.to,e.from);
                    }
                }
                if(end) break;
            }
        }
        cout<<dp[c][d]<<endl;
    }
}

C++: #include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

struct Node
{
	int num, cost, ticket;
	Node(int n, int c, int t)
	{
		num = n;cost = c; ticket = t;
	}

	bool operator < (const Node &o) const
	{
		return cost > o.cost;
	}
};

const int MAX_NODE_ID = 1024;
const int MAX_TICKET = 1024;
bool visited[MAX_NODE_ID][MAX_TICKET];

int c, n, m, s, d;

#define MAX 50000
int map[101][101];

int main()
{
	while (true)
	{
		cin >> c >> n >> m >> s >> d;
		if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				map[i][j] = MAX;
		for (int i = 0; i < m; i++)
		{
			int a, b, f;
			cin >> a >> b >> f;
			map[a][b] = map[b][a] = f;
		}
		for (int i = 0; i < MAX_NODE_ID; i++)
			for (int j = 0; j < MAX_TICKET; j++)
				visited[i][j] = false;
		priority_queue<Node> que;
		int minimum = MAX;
		que.push(Node(s, 0, c));
		while (!que.empty())
		{
			Node nod = que.top();
			que.pop();
			if (nod.num == d)
			{
				//S[
				minimum = min<int>(minimum, nod.cost);
			}
			else
			{
				for (int i = 1; i <= n; i++)
				{
					if (map[nod.num][i] < MAX && !visited[nod.num][nod.ticket])
					{
						que.push(Node(i, nod.cost + map[nod.num][i], nod.ticket));
						if (nod.ticket > 0)
						{
							que.push(Node(i, nod.cost + map[nod.num][i] / 2, nod.ticket - 1));
						}
					}
				}
			}
			visited[nod.num][nod.ticket] = true;
		}
		cout <<	minimum << endl;
	}
	return 0;
}