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<vector>
#include<queue>
#include<set>
#include<climits>
using namespace std;

struct Data{
  int id;
  int cost;
  int ticket;
};
bool operator<(Data a, Data b){
  return a.cost!=b.cost?a.cost>b.cost:(a.ticket!=b.ticket?a.ticket<b.ticket:a.id>b.id);
}
struct Data2{
  int id;
  int ticket;
};
bool operator<(Data2 a, Data2 b){
  return a.id!=b.id?a.id>b.id:a.ticket<b.ticket;
}

#define NONE INT_MAX

int Dijkstra(vector< vector<int> >A,int start,int goal,int ticket)
{
  vector<int>cost(A.size(),NONE);
  priority_queue<Data>q;
  set<Data2>used;

  Data node={start,0,ticket};
  q.push(node);
  cost[start]=0;

  while(q.size()){
    node=q.top();q.pop();
    if(node.id==goal)return node.cost;
    Data2 t={node.id,node.ticket};
    if(used.count(t))continue;
    used.insert(t);
    for(int i=0;i<A[node.id].size();i++){
      if(A[node.id][i]==NONE)continue;
      if(node.ticket){
	if(cost[i]<=cost[node.id]+(A[node.id][i])/2)continue;
	Data next={i,cost[node.id]+(A[node.id][i])/2,node.ticket-1};
	cost[i]=next.cost;
	q.push(next);
      }else{
	if(cost[i]<=cost[node.id]+A[node.id][i])continue;
	Data next={i,cost[node.id]+A[node.id][i],node.ticket};
	cost[i]=next.cost;
	q.push(next);
      }
    }
  }
  return -1;
}

int main()
{
  int c,n,m,s,d;
  int a,b,f;
  while(cin>>c>>n>>m>>s>>d,c|n|m|s|d){
    s--;d--;
    vector< vector<int> >A(n,vector<int>(n,NONE));
    for(int i=0;i<m;i++){
      cin>>a>>b>>f;
      a--;b--;
      A[a][b]=A[b][a]=f;
    }
    cout<<Dijkstra(A,s,d,c)<<endl;
  }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int MAX_N = 110;
struct edge
{
    int to, cost, card;
    bool operator<(const edge &a) const { return cost > a.cost; }
};
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int c, n, m, s, d;
    while (cin >> c >> n >> m >> s >> d, c | n | m)
    {
        s--, d--;
        vector<edge> G[MAX_N];
        for (int i = 0; i < m; i++)
        {
            int a, b, f;
            cin >> a >> b >> f;
            a--, b--;
            G[a].push_back({b, f, 0});
            G[b].push_back({a, f, 0});
            G[a].push_back({b, f / 2, 1});
            G[b].push_back({a, f / 2, 1});
        }
        priority_queue<edge> que;
        vector<vector<int>> dist(n, vector<int>(15, INF));
        dist[s][c] = 0;
        que.push({s, 0, c});
        while(que.size())
        {
            edge p = que.top(); que.pop();
            for(int u = 0; u < G[p.to].size(); u++)
            {
                edge e = G[p.to][u];
                int num = p. card - e.card;
                if(num < 0) continue;
                if(e.cost + dist[p.to][p.card] < dist[e.to][num])
                {
                    dist[e.to][num] = dist[p.to][p.card] + e.cost;
                    que.push({e.to, dist[e.to][num], num});
                }
            }
        }
        int ans = INF;
        for(int i = 0; i < 15; i++) ans = min(ans, dist[d][i]);
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX 100000
#define MAX_NODE_ID 100
#define MAX_TICKET 10
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=0;i<MAX_NODE_ID;i++)for(int j=0;j<MAX_NODE_ID;j++)table[i][j]=MAX;
		for(int i=0,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;
		Node start(s,0,c);
		pq.push(start);
		while(!pq.empty()){
//			cout<<"current:"<<current_node<<endl;
			if(pq.top().id==d)break;
			if(visited[pq.top().id][pq.top().cost]);
			else{
				for(int i=1;i<=n;i++){
					if(pq.top().id==i)continue;
					if(table[pq.top().id][i]==MAX)continue;
	//				cout<<"["<<current_node<<"]["<<i<<"]"<<"cost:"<<table[current_node][i]<<endl;
					Node node1(i,pq.top().cost+table[pq.top().id][i],pq.top().ticket);
					pq.push(node1);
					if(pq.top().ticket>0){
						Node node2(i,pq.top().cost+(table[pq.top().id][i]/2),pq.top().ticket-1);
						pq.push(node2);
					}
					visited[pq.top().id][i]=true;
				}
			}
			pq.pop();
		}
		cout<<pq.top().cost<<endl;
	}


	return 0;
}