Go: package main

import (
	"container/heap"
	"fmt"
)

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)
					}
				}
			}
		}
	}
}

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]; 
  priority_queue < PP, vector<PP>, greater<PP> > que;
  
  for(int i=0;i<111;i++) for(int j=0;j<11;j++) dist[i][j] = INF;
  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;
    
    for(int i=0;i<vec[now].size();i++){
      P to = vec[now][i];
      if(dist[to.S][tic] > cost + to.F){
	dist[to.S][tic] = cost + to.F;
	que.push( PP(dist[to.S][tic], P(to.S, tic)) );
      }
      if(tic && dist[to.S][tic-1] > cost + to.F/2){
	dist[to.S][tic-1] = cost + to.F/2;
	que.push( PP(dist[to.S][tic-1], P(to.S, tic-1)) );
      }
    }
  }
  
  return dist[d][c];
}

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 <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, 0, -1, 0};
int dy[]={0, -1, 0, 1};

int dijkstra(int s, int g, int ticketNum, vector<vector<P>> &G){
    vector<vector<int>> dist(110, vector<int>(15, 1e9));
    queue<P> que;
    dist[s][0] = 0;
    que.push(P(s, 0));

    while(!que.empty()){
        int cv, cticket;
        tie(cv, cticket) = que.front();
        que.pop();

        for(auto x : G[cv]){
            int nv = x.first;
            int ncost = x.second;
            if(dist[cv][cticket] + ncost < dist[nv][cticket]){
                dist[nv][cticket] = dist[cv][cticket] + ncost;
                que.push(P(nv, cticket));
            }
            if(cticket < ticketNum && dist[cv][cticket] + ncost / 2 < dist[nv][cticket+1]){
                dist[nv][cticket+1] = dist[cv][cticket] + ncost / 2;
                que.push(P(nv, cticket+1));
            }
        }
    }

    int ans=1e9;
    for(int i=0; i<=ticketNum; i++){
        chmin(ans, dist[g][i]);
    }

    return ans;
}

int main(){
    while(1){
        int c, n, m, s, d; cin>>c>>n>>m>>s>>d;
        s--, d--;
        if(c == 0) return 0;

        vector<vector<P>> G(110);
        for(int i=0; i<m; i++){
            int a, b, f; cin>>a>>b>>f;
            a--, b--;
            G[a].eb(P(b, f));
            G[b].eb(P(a, f));
        }

        cout << dijkstra(s, d, c, G) << endl;
    }
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>

using namespace std;

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

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

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

		for(int i = 1; i <= info.index-1; i++){
			for(int k = info.index-1; k >= i; k--){
				if(info.cost[k] > info.cost[k-1])swap(info.cost[k],info.cost[k-1]);
			}
		}
		int sum = 0;

		if(num_ticket >= info.index){
			for(int i = 0; i < info.index; i++){
				sum += info.cost[i];
			}
			sum /= 2;
			minimum = min(minimum,sum);
		}else{
			for(int i = 0; i < num_ticket; i++){
				sum += info.cost[i]/2;
			}
			for(int i = num_ticket; i < info.index; i++){
				sum += info.cost[i];
			}

			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 = 0; k < 100; k++){
				new_info.visited[k+1] = info.visited[k+1];
				new_info.cost[k] = info.cost[k];
			}
			new_info.visited[i] = true;
			new_info.current_town = i;
			new_info.index = info.index;
			new_info.cost[new_info.index++] = 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;
}