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]struct{})
		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}] = struct{}{}
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				if _, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok {
					heap.Push(&pq, next)
				}
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					if _, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok {
						heap.Push(&pq, next)
					}
				}
			}
		}
		runtime.GC()
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define INF 1000000000;
#define dis first
#define mati second.first
#define sta  second.second
using namespace std;
typedef pair <int ,pair <int,int> > P;
vector <P> mp[102];
int c,n,m,start,goal;

int bit_count(int a) {
  a = (a & 0x55555555)+((a & 0xAAAAAAAA) >> 1 );
  a = (a & 0x33333333)+((a & 0xCCCCCCCC) >> 2 );
  a = (a & 0x0F0F0F0F)+((a & 0xF0F0F0F0) >> 4 );
  a = (a & 0x00FF00FF)+((a & 0xFF00FF00) >> 8 );
  a = (a & 0x0000FFFF)+((a & 0xFFFF0000) >> 16);
  return a;
}

void henkan(int n,int kai) {
  if(kai <= 10) {
    henkan(n/2,kai+1);  
  cout << n % 2 ;
  if(kai == 0) cout << endl;
  }
}

int dijkstra() {
  int flg[1<<10][102] = {},dis[1<<10][102];
  for(int j=0;j<1<<n;j++)
    for(int i=0;i<=n;i++) dis[j][i] = -INF;

  for(int j=1;j<1<<n;j++)flg[j][start] = 1;

  priority_queue <P> Q;
  P t;
  t.dis = 0, t.mati = start, t.sta = 0;
  Q.push(t);

  while(!Q.empty()) {
    t = Q.top(),Q.pop();
    if(flg[t.sta][t.mati] == 1) continue;
    flg[t.sta][t.mati] = 1;
    for(int i=0;i<mp[t.mati].size();i++){
      int ndis = mp[t.mati][i].dis;
      int nmati = mp[t.mati][i].mati;
      int nsta = t.sta | (1<<(nmati-1));
      P tt;
      if(dis[t.sta][nmati] < t.dis-ndis && flg[t.sta][nmati] == 0) {
	tt.dis = t.dis-ndis,tt.mati = nmati,tt.sta = t.sta;
	dis[t.sta][nmati] = t.dis-ndis;
	Q.push(tt);
      }
      if(bit_count(nsta)<= c&&dis[nsta][nmati]<t.dis-ndis/2 && flg[nsta][nmati]==0){
	tt.dis = t.dis - ndis/2 , tt.mati = nmati, tt.sta = nsta;
	dis[nsta][nmati] = t.dis-ndis/2;
	Q.push(tt);
      }
    }
  }

  int ans = -INF;
  for(int j=0;j<1<<n;j++)ans = max(ans,dis[j][goal]);
   
  return -ans;
}


int main() {

  while(1) {
    cin >> c >> n >> m >> start >> goal;
    if(c==0&&n==0&&m==0&&start==0&&goal==0) break;

    for(int i=0;i<=n;i++) mp[i].clear();
    for(int i=0;i<m;i++) {
      int a,b,f;
      cin >> a >> b >> f;
      P t;
      t.dis = f,  t.mati = b , t.sta = 0;
      mp[a].push_back(t);
      t.mati = a;
      mp[b].push_back(t);
    }

    cout << dijkstra() << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
struct Edge{ int to, cost; };
vector<Edge> G[101];
int dist[11][101];
const int INF = (1<<28);
int dijkstra( int s, int a ){
  priority_queue< P, vector<P>, greater<P> > q;
  for( int j=0; j<=100; j++ )
    dist[a][j] = INF;
  dist[a][s] = 0;
  q.push({ 0, s });
  while( !q.empty() ){
    int cost = q.top().first;
    int v = q.top().second;
    q.pop();
    if( dist[a][v] < cost ) continue;
    for( auto it=G[v].begin(); it!=G[v].end(); ++it ){
      int d = dist[a][v] + it->cost;
      if( a > 0 )
        d = min( d, dist[a-1][v] + it->cost/2 );
      if( dist[a][it->to] > d ){
        dist[a][it->to] = d;
        q.push({ d, it->to });
      }
    }
  }
  //for( int i=1; i<=3; i++ )
  //  cout << dist[a][i] << ' '; cout << endl;
}
int main( void ){
  while( 1 ){
    int c,n,m,s,d,a,b,f;
    cin >> c >> n >> m >> s >> d;
    if( c == 0 ) break;
    for( int i=1; i<=n; i++ ) G[i].clear();
    for( int i=0; i<m; i++ ){
      cin >> a >> b >> f;
      G[a].push_back({ b, f });
      G[b].push_back({ a, f });
    }
    for( int i=0; i<=c; i++ )
      dijkstra( s, i );
    cout << dist[c][d] << endl;
  }
}

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, vector<state>, greater<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();
    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;
}