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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

int minimum,num_ticket,num_town,m,start,goal,costTable[101][101];

class PQ{
public:
	PQ(){
		array = new int[100];
		n = 0;
	}
	void push(int arg_info){
		array[n] = arg_info;
		int tmp = n;
		while(tmp != 0 && array[tmp] < array[(tmp-1)/2]){
			swap(array[tmp], array[(tmp-1)/2]);
			tmp = (tmp-1)/2;
		}
		n++;
	}

	bool empty(){
		return n == 0;
	}

	int get(int i){
		return array[i];
	}

	int pop(){
		int ret = array[0];
		n--;
		swap(array[0],array[n]);
		int tmp = 0;
		while(2*tmp+1 <= n-1){
			if(2*tmp+2 <= n-1){
				if(array[2*tmp+2] < array[tmp] && array[2*tmp+2] <= array[2*tmp+1]){
					swap(array[2*tmp+2],array[tmp]);
					tmp = 2*tmp+2;
				}else if(array[2*tmp+1] < array[tmp] && array[2*tmp+1] <= array[2*tmp+2]){
					swap(array[2*tmp+1],array[tmp]);
					tmp = 2*tmp+1;
				}else{
					break;
				}
			}else{
				if(array[tmp] > array[2*tmp+1]){
					swap(array[tmp],array[2*tmp+1]);
					tmp = 2*tmp+1;
				}else{
					break;
				}
			}
		}
		return ret;
	}

private:
	int* array;
	int n;
};


struct Info{
	Info(){
		for(int i = 1; i <= num_town; i++){
			visited[i] = false;
		}
		current_town = 0;
		index = 0;
	}
	bool visited[101];
	int current_town,index;
	PQ Q;
};

void func(Info info){
	if(info.current_town == goal){

		int sum = 0;

		if(num_ticket >= info.index){
			while(!info.Q.empty()){
				sum += info.Q.pop();
			}
			sum /= 2;
			minimum = min(minimum,sum);
		}else{

			int count = 0,tmp;
			while(!info.Q.empty()){
				tmp = info.Q.pop();
				if(count < num_ticket){
					sum += tmp/2;
				}else{
					sum += tmp;
				}
				count++;
			}
			minimum = min(minimum,sum);
		}
		return;
	}

	for(int i = 1; i <= num_town; i++){
		if(info.visited[i] == false && costTable[info.current_town][i] != -1){

			Info new_info;

			for(int k = 1; k <= num_town; k++){
				new_info.visited[k] = info.visited[k];
			}
			new_info.visited[i] = true;
			new_info.current_town = i;
			new_info.index = info.index+1;

			for(int k = 0; k < info.index; k++){
				new_info.Q.push(info.Q.get(k));
			}

			new_info.Q.push(costTable[info.current_town][i]);
			func(new_info);
		}
	}
}

int main(){

	int from,to,fee;

	while(true){
		scanf("%d %d %d %d %d",&num_ticket,&num_town,&m,&start,&goal);
		if(num_ticket == 0 && num_town == 0 && m == 0 && start == 0 && goal == 0)break;

		minimum = 2000000000;

		for(int i = 1; i <= num_town; i++){
			for(int k = 1; k <= num_town; k++)costTable[i][k] = -1;
		}

		for(int i = 0; i < m; i++){
			scanf("%d %d %d",&from,&to,&fee);
			costTable[from][to] = fee;
			costTable[to][from] = fee;
		}

		Info info;

		info.current_town = start;
		info.visited[start] = true;

		func(info);

		printf("%d\n",minimum);
	}


	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;

struct node{
  int cost;
  int c;
  int now;
  node(int C,int cc,int n):cost(C),c(cc),now(n){}
  bool operator >(const node &e) const{
    return cost > e.cost;
  }
};

int main()
{
  int c,n,m,s,d;

  for(;cin>>c>>n>>m>>s>>d,c;)
    {
      int edge[n][n];
      bool close[c+1][n];
      memset(edge,-1,sizeof(edge));
      memset(close,false,sizeof(close));      
      for(int i=0;i<m;i++)
	{
	  int a,b,f;
	  cin>>a>>b>>f;
	  a--;
	  b--;
	  edge[a][b]=f;
	  edge[b][a]=f;
	}
      s--;
      d--;
      priority_queue<node,vector<node>,greater<node> >qu;
      qu.push(node(0,c,s));
      while(!qu.empty())
	{
	  node tmp = qu.top();
	  //	  cout<<tmp.now<<" "<<tmp.cost<<" "<<tmp.c<<endl;
	  qu.pop();
	  if(close[tmp.c][tmp.now])
	    continue;
	  close[tmp.c][tmp.now]=true;
	  if(tmp.now==d)
	    {
	      cout<<tmp.cost<<endl;
	      break;
	    }
	  for(int i=0;i<n;i++)
	    {
	      if(edge[tmp.now][i]!=-1)
		{
		  if(!close[tmp.c][i])
		    qu.push(node(edge[tmp.now][i]+tmp.cost,tmp.c,i));
		  if(tmp.c>0 && !close[tmp.c-1][i])
		    qu.push(node(edge[tmp.now][i]/2+tmp.cost,tmp.c-1,i));
		}
	    }
	}
    }

}

C++: #include <iostream>
#define INF 1000000000
using namespace std;


struct edge{
	int from, to, cost;
};

int ans[100000];
int counter=0;
bool flag=true;

void solve(){
	int d[101][11];//地点と割引回数
	edge route[500];//路線
	int c,n,m,s,t;//割引回数,町数,路線数,出発駅,到着駅
	cin >> c>>n>>m>>s>>t;
	if(c==0) {flag=false; return;}
	
	for(int i=0;i<m;i++){
		cin >>route[i].from>>route[i].to>>route[i].cost;
	}
	
	for(int i=1;i<=n;i++){
		for(int j=0;j<c+1;j++){//割引回数は割引無しも必要
			d[i][j]=INF;
		}
	}
	
	d[s][0]=0;
	bool update=true;
	while(update){
		update=false;
		for(int i=0;i<m;i++){
			edge e=route[i];//見やすく
			for(int j=0;j<=c;j++){
				//まずは割引無し
				if(d[e.from][j]!=INF && d[e.to][j]>d[e.from][j]+e.cost){
					d[e.to][j]=d[e.from][j]+e.cost; update=true;
				}
				if(d[e.to][j]!=INF && d[e.from][j]>d[e.to][j]+e.cost){
					d[e.from][j]=d[e.to][j]+e.cost; update=true;
				}
				
			}
			
			for(int j=0;j<c;j++){
				if(d[e.from][j]!=INF && d[e.to][j+1]>d[e.from][j]+e.cost/2){
					d[e.to][j+1]=d[e.from][j]+e.cost/2; update=true;
				}
				if(d[e.to][j]!=INF && d[e.from][j+1]>d[e.to][j]+e.cost/2){
					d[e.from][j+1]=d[e.to][j]+e.cost/2; update=true;
				}
			}
		}
	}
	int k=INF;
	for(int i=0;i<=c;i++){
		if(k>d[t][i]) k=d[t][i];
	}
	ans[counter]=k;
	counter++;
}

int main(){
	while(flag){
		solve();
	}
	for(int i=0;i<counter;i++){
		cout << ans[i]<<endl;
	}
}