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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

using namespace std;

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


struct Info{
    Info(){
        current_town = 0;
        index = 0;
    }
    bool visited[102];
    int current_town,index,costs[102],visited_town[101];

};

void func(Info info){

    int sum = 0,limit;

    if(info.current_town == goal || (info.index >= min_past_num-2 && minimum != 2000000000))
    {

    	limit = min(num_ticket,info.index);

    	for(int i = 0; i < limit; i++){
    		sum += info.costs[i]/2;
    	}

    	for(int i = limit; i < info.index; i++){
    		sum += info.costs[i];
    	}

		if(info.current_town == goal){
			if(minimum > sum){
				minimum = sum;
				min_past_num = info.index;
			}
			return;
		}else{
			if(sum >= minimum){
				return;
			}
		}

    }

    int calc;
    for(int i = 1; i <= num_town; i++){
        if(costTable[info.current_town][i] != -1 && info.visited[i] == false){
            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;

            if(costTable[info.current_town][i] > info.costs[0]){
            	new_info.costs[0] = costTable[info.current_town][i];
            	for(int k = 1; k <= info.index; k++){
            		new_info.costs[k] = info.costs[k-1];
            	}
            }else if(costTable[info.current_town][i] <= info.costs[info.index-1]){
            	for(int k = 0; k < info.index; k++){
            		new_info.costs[k] = info.costs[k];
            	}
            	new_info.costs[info.index] = costTable[info.current_town][i];
            }else{
            	for(calc = 0; info.costs[calc] >= costTable[info.current_town][i];calc++)new_info.costs[calc] = info.costs[calc];
				new_info.costs[calc] = costTable[info.current_town][i];
				calc++;
				for(;calc-1 < info.index;calc++)new_info.costs[calc] = info.costs[calc-1];
            }
            new_info.index = info.index+1;
            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;
        min_past_num = 101;

        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;
        for(int i = 1; i <= num_town;i++)info.visited[i] = false;

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

        func(info);

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


    return 0;
}

C++: include<bits/stdc++.h>
#define F first
#define S second
#define INF 1<<28
using namespace std;
 
int c, n, s, d;
typedef pair < int, int > P;
typedef pair < int, P > PP;
vector < P > vec[111];
 
int solve(){
 
  int dist[111][11]; for(int i=0;i<111;i++)for(int j=0;j<11;j++) dist[i][j] = INF;
  priority_queue < PP, vector<PP>, greater<PP> > que;
 
  dist[s][c] = 0;
  que.push( PP(0, P(s, c)) ); //Now_cost, Now_node, Now_ticket
 
  while(!que.empty()){
    PP p = que.top(); que.pop();
    int cost = p.F, now = p.S.F, tic = p.S.S;
 
    //if(dist[now][tic] < cost) continue;
    for(int i=0;i<vec[now].size();i++){
      P to = vec[now][i];
      if(dist[to.S][tic] > dist[now][tic] + to.F){
	dist[to.S][tic] = dist[now][tic] + to.F;
	que.push( PP(dist[to.S][tic], P(to.S, tic)) );
      }
      if(tic && dist[to.S][tic-1] > dist[now][tic] + to.F/2){
	dist[to.S][tic-1] = dist[now][tic] + to.F/2;
	que.push( PP(dist[to.S][tic-1], P(to.S, tic-1)) );
      }
    }
  }
   
  int ans = INF;
  for(int i=0;i<=c;i++){ ans = min(ans, dist[d][i]); } //cout << ans << endl; }
  return dist[d][0];
}
 
int main(){
 
  int m, a, b, p;
 
   
  while(cin >> c >> n >> m >> s >> d){
    if((c|n|m|s|d) == 0) break;
    for(int i=0;i<111;i++) vec[i].clear();
    for(;m--;){
      cin >> a >> b >> p;
      vec[a].push_back(P(p, b)); //[now] P(cost, next);
      vec[b].push_back(P(p, a));
    }
     
    cout << solve() << endl;
  }
   
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

const int INF = 1 << 30;

struct edge {int to, cost; };

int V;
int d[100][100];

vector<edge> G[100];
priority_queue<P, vector<P>, greater<P> > que;

void dijkstra(int s,int a){
  for(int i = 0; i < V; i++) d[a][i] = INF;
  que.push(P(0, s));
  d[a][s] = 0;

  while(!que.empty()){
    P p = que.top(); que.pop();
    int v = p.second;
    if(d[a][v] < p.first) continue;
    for(int i = 0; i < G[v].size(); i++){
      edge e = G[v][i];
      int Min = (a == 0 ? d[a][v] + e.cost : min(d[a][v] + e.cost, d[a-1][v] + e.cost/2));
      if(d[a][e.to] > Min){
        d[a][e.to] = Min;
        que.push(P(d[a][e.to], e.to));
      }
    }
  }
}

int main(){
  int c,m,s,e,From,To,Cost;
  edge E;
  while(scanf("%d%d%d%d%d",&c,&V,&m,&s,&e)!=EOF,c|V|m|s--|e--){
    for(int i = 0; i < m; i++){
      scanf("%d%d%d",&From,&To,&Cost);

      From--,To--;
      E.to = From,E.cost = Cost;
      G[To].push_back(E);
      E.to = To;
      G[From].push_back(E);
    }

    for(int i = 0; i <= c; i++) dijkstra(s, i);

    printf("%d\n",d[c][e]);

    for(int i = 0; i < 100; i++) G[i].clear();
  }
}