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 <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(A)		(A).begin(),(A).end()
#define DUMP(A)    cout<<#A<<"="<<(A)<< endl
#define SIZE(A)    (int)((A).size())
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> PP;

int main(){
    int c,n,m,s,d;
    for(;;){
        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--;
        int path[100][100];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                path[i][j]=-1;
            }
        }
        int fr,to,co;
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&fr,&to,&co);
            fr--;to--;
            path[fr][to]=path[to][fr]=co;
        }
        int dist[100][11];
        for(int i=0;i<n;i++){
            for(int j=0;j<=c;j++){
                dist[i][j]=-1;
            }
        }
        priority_queue<PP> pque;
        pque.push(PP(0,P(0,s)));
        while(!pque.empty()){
            PP now=pque.top();
            pque.pop();
            int ndi=now.first,ncar=now.second.first,nat=now.second.second;
            if(dist[nat][ncar]!=-1 && dist[nat][ncar]<=ndi) continue;
            dist[nat][ncar]=ndi;
            for(int i=0;i<n;i++){
                if(path[nat][i]!=-1 && (dist[i][ncar]==-1 || ndi+path[nat][i]<dist[i][ncar])){
                    pque.push(PP(ndi+path[nat][i],P(ncar,i)));
                }
                if(path[nat][i]!=-1 && ncar+1<=c && (dist[i][ncar+1]==-1 || ndi+path[nat][i]/2 < dist[i][ncar+1])){
                    pque.push(PP(ndi+path[nat][i]/2,P(ncar+1,i)));
                }
            }
        }
        int mi=INT_MAX;
        for(int i=0;i<=c;i++){
            if(dist[d][i]!=-1 && dist[d][i]<mi) mi=dist[d][i];
        }
        printf("%d\n",mi);
    }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <cassert>
#include <algorithm>
using namespace std;
typedef pair<int, int> edge;
struct state{
  int cost, coupon, station;
  state(int cost_, int coupon_, int station_):cost(cost_), coupon(coupon_), station(station_){}
  bool operator< (const state &s) const{
    if(cost == s.cost){
      return coupon > s.coupon;
    }
    return cost > s.cost;
  }
};
int djikstra(vector<vector<edge> > graph, int start, int goal, int coupon){
  priority_queue<state> pq;
  pq.push(state(0, coupon, start));
  bool visited[coupon][graph.size()];
  fill_n((bool*)visited, (coupon+1) * graph.size(), false);
  while(!pq.empty()){
    state cur = pq.top(); pq.pop();
    //cout << cur.station << ": " << cur.cost << ": " << cur.coupon << endl;
    if(visited[cur.coupon][cur.station]){
      continue;
    }
    if(cur.station == goal){
      return cur.cost;
    }
    visited[cur.coupon][cur.station] = true;
    for(int i = 0; i < graph[cur.station].size(); ++i){
      int to = graph[cur.station][i].first;
      int c = graph[cur.station][i].second;
      if(cur.coupon > 0){
        pq.push(state(cur.cost+c/2, cur.coupon-1, to));
      }
      pq.push(state(cur.cost+c, cur.coupon, to));
    }
  }
  assert(false);
}
int main(){
  int c, n, m, s, d;
  while(true){
    cin >> c >> n >> m >> s >> d;
    if((c|n|m|s|d) == 0){
      break;
    }
    vector<vector<edge> > graph(n+1, vector<edge>());
    for(int i = 0; i < m; ++i){
      int a, b, f;
      cin >> a >> b >> f;
      graph[a].push_back(make_pair(b, f));
      graph[b].push_back(make_pair(a, f));
    }
    cout << djikstra(graph, s, d, c) << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;//cost, v, ticket
struct edge{
  int to, cost;
};

int main(int argc, char *argv[]){
  int c, n, m, s, d;
  while(std::cin >> c >> n >> m >> s >> d, n){
    s--;d--;
    vector<vector<edge> > g(n);
    for (int i = 0; i < m; i++) {
      edge e;
      int from;
      std::cin >> from >> e.to >> e.cost;
      e.to--;from--;
      g[from].push_back(e);
      g[e.to].push_back((edge){from, e.cost});
    }
    vector<vector<int> > dp(c + 1, vector<int>(n, 1e9));
    dp[0][s] = 0;
    priority_queue<piii, vector<piii>, greater<piii> > que;
    que.push(piii(0, pii(s, 0)));
    while(!que.empty()){
      int cost   = que.top().first;
      int v      = que.top().second.first;
      int ticket = que.top().second.second;
      que.pop();
      for (int i = 0; i < g[v].size(); i++) {
        edge e = g[v][i];
        if(dp[ticket][e.to] > dp[ticket][v] + e.cost){
          dp[ticket][e.to] = dp[ticket][v] + e.cost;
          que.push(piii(dp[ticket][e.to], pii(e.to, ticket)));
        }
        if(ticket + 1 <= c &&
           dp[ticket + 1][e.to] > dp[ticket][v] + e.cost/2){
          dp[ticket + 1][e.to] = dp[ticket][v] + e.cost/2;
          que.push(piii(dp[ticket + 1][e.to],
                        pii(e.to, ticket + 1)));
        }
      }
    }
    std::cout << dp[c][d] << std::endl;
  }
  return 0;
}