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<queue>
#include<cstdio>
#include<algorithm>

#define	mp	make_pair

using namespace std;

typedef	pair<int,int>	pii;

const int inf=1<<29;
int adj[100][100],memo[100][11];
bool visited[100][11];

int main(){
	for(int c,n,m,src,dst;scanf("%d%d%d%d%d",&c,&n,&m,&src,&dst),c;){
		src--,dst--;
		for(int i=0;i<n;i++)for(int j=0;j<n;j++)	adj[i][j]=(i==j?0:inf);
		for(int i=0;i<n;i++)for(int j=0;j<=c;j++)	memo[i][j]=inf,visited[i][j]=false;
		for(int i=0;i<m;i++){
			int a,b,fee;	scanf("%d%d%d",&a,&b,&fee);
			a--,b--;
			adj[a][b]=adj[b][a]=fee;
		}

		memo[src][0]=0;
		priority_queue< pair<int,pii> > pq;	pq.push(mp(0,mp(src,0)));
		while(!pq.empty()){
			pair<int,pii> a=pq.top();	pq.pop();
			int fee=-a.first;
			int u=a.second.first;
			int tic=a.second.second;
			if(u==dst)	break;

			if(visited[u][tic])	continue;
			visited[u][tic]=true;

			for(int v=0;v<n;v++){
				if(!visited[v][tic] && fee+adj[u][v]<memo[v][tic]){
					memo[v][tic]=fee+adj[u][v];
					pq.push(mp(-memo[v][tic],mp(v,tic)));
				}
				if(tic<c && !visited[v][tic+1] && fee+adj[u][v]/2<memo[v][tic+1]){
					memo[v][tic+1]=fee+adj[u][v]/2;
					pq.push(mp(-memo[v][tic+1],mp(v,tic+1)));
				}
			}
		}

		int feemin=inf;
		for(int i=0;i<=c;i++)	feemin=min(feemin,memo[dst][i]);
		printf("%d\n",feemin);
	}

	return 0;
}

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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<28

using namespace std;
typedef pair<int,int> P;


struct edge
{
	int to;
	int cost;

	edge(int to,int cost) : to(to),cost(cost) {}
};

int d[101][11];
vector<edge> G[101];

void dijkstra(int s,int n,int ticket)
{
	priority_queue<P,vector<P>,greater<P> > que;
	rep(i,n) d[i][ticket] = INF;
	d[s][ticket] = 0;
	que.push(P(0,s));

	while(!que.empty())
	{
		P p = que.top();
		que.pop();

		int v = p.second;
		if(d[v][ticket] < p.first) continue;

		rep(i,G[v].size())
		{
			edge e = G[v][i];
			int temp;

			if(ticket == 0)
			{
				temp = d[v][ticket] + e.cost;
			}
			else
			{
				temp =  min(d[v][ticket]+e.cost,d[v][ticket-1]+e.cost/2);
			}

			if(d[e.to][ticket] > temp)
			{
				d[e.to][ticket] = temp;
				que.push(P(d[e.to][ticket],e.to));
			}
		}
	}
}

int main()
{
	int c,n,m,s,t;
	while(cin >> c >> n >> m >> s >> t)
	{
		if(c == 0 && n == 0 && m == 0 && s == 0 && t == 0) break;
		
		s--;
		t--;
		rep(i,100) G[i].clear();

		rep(i,m)
		{
			int a,b,cost;
			cin >> a >> b >> cost;
			a--;
			b--;

			edge e(b,cost);
			G[a].push_back(e);

			edge e2(a,cost);
			G[b].push_back(e2);
		}
		
		rep(i,c+1) dijkstra(s,n,i);

		cout <<  d[t][c] << endl;
	}

	return 0;
}

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

#define reep(i,n,m) for(int i=(n);i<(m);i++)
#define rep(i,n) reep(i,0,n)

using namespace std;

class Edge{
public:
    int to;
    int cost;
    Edge(int to,int cost):to(to),cost(cost){}
};

class Node{
public:
    int distance;
    int num;
    int ticket;
    Node(){}
    Node(int distance, int num,int ticket):distance(distance),num(num),ticket(ticket){}
    bool operator<(const Node &b)const{
        return distance > b.distance;
    }
};

vector<Edge> edge[101];
bool flg[101][11];

int c,n,m,s,d;

int solve(){

    priority_queue<Node> Q;
    Q.push(Node(0,s,0));
    int ans = 1000000;
    while(!Q.empty()){
        Node node = Q.top();Q.pop();
        if(flg[node.num][node.ticket]){
            continue;
        }
        flg[node.num][node.ticket] = true;
        //printf("num->%d cost->%d ticket->%d\n",node.num,node.distance,node.ticket);
        if(node.num == d){
            ans = min(ans,node.distance);
        }

        rep(i,edge[node.num].size()){
            Edge e = edge[node.num][i];
            int cost = node.distance + e.cost;
            Q.push(Node(cost,e.to,node.ticket));
            if(node.ticket < c){
                int cost = node.distance + e.cost/2;
                Q.push(Node(cost,e.to,node.ticket+1));
            }
        }
    }
    return solve();
}



void init(){
    rep(i,101){
        edge[i].clear();
        rep(k,11){
            flg[i][k] = 0;
        }
    }
}

int main(){

    while(cin >> c >> n >> m >> s >> d,!m==0){
        init();
        rep(i,m){
            int a,b,c;
            cin >> a >> b >> c;
            edge[a].push_back(Edge(b,c));
            edge[b].push_back(Edge(a,c));
        }
        cout << solve() << endl;
    }
}