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 <queue>
#include <vector>
#include <functional>
#include <algorithm>
#define MAX_V 100
#define INF 10000000
using namespace std;

struct edge{
  int to,cost;
};

typedef pair<int,int> P; //firstは最短経路,secondは頂点番号

vector<edge> G[MAX_V];
int vertexes,edges;
int d[MAX_V][MAX_V];

void dijkstra(int start,int tickets){
  priority_queue<P,vector<P>,greater<P> > que;
  for(int i=0;i<vertexes;i++) d[tickets][i] = INF;
  d[tickets][start] = 0;
  que.push(P(0,start));

  while(!que.empty()){
    P p = que.top();
    que.pop();
    int v = p.second; 
    if(d[tickets][v] < p.first) continue; //異なる2経路のコストで高い方は無視
    for(int i=0;i<G[v].size();i++){
      edge e = G[v][i];
      int minimum = (tickets==0 ? d[tickets][v] + e.cost: min(d[tickets][v] + e.cost, d[tickets-1][v] + e.cost/2));
      if(d[tickets][e.to] > minimum){
        d[tickets][e.to] = minimum;
        que.push( P(d[tickets][e.to],e.to) );
      }
    }
  }
}

int main(){
  int discounts,start,destination;
  while(cin >> discounts >> vertexes >> edges >> start >> destination){
    if(!discounts && !vertexes && !edges && !start && !destination) break;
    for(int i=0;i<edges;i++){
      int a,b,fee;
      cin >> a >> b >> fee;
      a--; b--;

      edge E;
      E.to = b;
      E.cost = fee;
      G[a].push_back(E);

      E.to = a;
      G[b].push_back(E);
    }
    start--; destination--;
    for(int i=0;i<=discounts;i++) dijkstra(start,i);
    cout << d[discounts][destination] << endl;
    for(int i=0;i<MAX_V;i++) G[i].clear();
  }
  return 0;
}

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

using namespace std;

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)

struct edge {
    int to, cost;
};

struct state {
    int v, d, rest;
    bool operator<(const state& s)const{
        return d>s.d;
    }
};

int dijk(int s, int g, const vector<vector<edge>>& G, int c){

    vector<int> d(G.size(), 1000000000);
    d[s] = 0;

    priority_queue<state> q;
    q.push({ s, 0, c });
    
    vector<vector<bool>> vis(G.size(),vector<bool>(c+1,false));

    while (q.size()){
        state sta = q.top();
        q.pop();

        int v = sta.v;

        int rest = sta.rest;

        if (vis[v][rest]) continue;
        vis[v][rest] = true;

        if (d[v] > sta.d) continue;

        rep(i, G[v].size()){

            int to = G[v][i].to;
            int cost = G[v][i].cost;


            if (d[to] > sta.d + cost){

                d[to] = sta.d + cost;
                q.push({ to, sta.d + cost, rest});
            }

            if (d[to] > sta.d + cost / 2 && rest != 0){

                d[to] = sta.d + cost / 2;
                q.push({ to, sta.d + cost / 2, rest - 1 });
            }
        }
    }
    return d[g];
}

int main(){
    while (1){
        int c, V, E, s, d;
        cin >> c >> V >> E >> s >> d;
        if (c == 0) break;
        vector<vector<edge>> G(V);
        s--; d--;
        rep(i, E){
            int a, b, c;
            cin >> a >> b >> c;
            a--; b--;
            G[a].push_back({ b, c });
            G[b].push_back({ a, c });
        }
        cout << dijk(s, d, G, c) << endl;
    }
}

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

using namespace std;

struct Edge{
	int s,d,w,n;
	Edge(){}
	Edge(int s,int d,int w,int n=0):s(s),d(d),w(w),n(n){}
	bool operator<(const Edge& e)const{return w<e.w;}
	bool operator>(const Edge& e)const{return w>e.w;}
};

const int INFTY=1<<29;

int main()
{
	for(int c,n,m,s,d;cin>>c>>n>>m>>s>>d,c|n|m|s|d;){
		s--; d--;
		vector<vector<Edge> > g(n);
		for(int i=0;i<m;i++){
			int s,d,w; cin>>s>>d>>w;
			s--; d--;
			g[s].push_back(Edge(s,d,w));
			g[d].push_back(Edge(d,s,w));
		}
		priority_queue<Edge,vector<Edge>,greater<Edge> > pq;
		vector<vector<int> > cost(n,vector<int>(c+1,INFTY));
		pq.push(Edge(-1,s,0,0));
		while(pq.size()){
			Edge cur=pq.top(); pq.pop();
			if(cost[cur.d][cur.n]!=INFTY)
				continue;
			cost[cur.d][cur.n]=cur.w;
			for(int i=0;i<g[cur.d].size();i++){
				Edge& e=g[cur.d][i];
				pq.push(Edge(e.s,e.d,cur.w+e.w,cur.n));
				if(cur.n<c)
					pq.push(Edge(e.s,e.d,cur.w+e.w/2,cur.n+1));
			}
		}
		
		cout<<*min_element(cost[d].begin(),cost[d].end())<<endl;
	}
	
	return 0;
}