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)
		}
		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)
		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 <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

#define loop(i, n) for (int i = 0; i < n; i++)
#define INF 1010001000

typedef pair<int, int> i_i;
typedef struct data{int pos, tick, cost;}data;

int dp(vector<vector<i_i > > &g);

int c, n, m, s, d;

int main(void)
{
    while (cin >> c >> n >> m >> s >> d, c) {
        n++;
        vector<vector<i_i > > g(n);
        loop(i, m) {
            int a, b, f;
            cin >> a >> b >> f;
            g[a].push_back(i_i(f, b));
            g[b].push_back(i_i(f, a));
        }
        cout << dp(g) << endl;
    }
    return 0;
}

int dp(vector<vector<i_i > > &g)
{
    vector<vector<int> > table(c+1, vector<int>(n, INF));
    queue<data> a;
    data dt = {s, c, 0};
    a.push(dt);
    while (!a.empty()) {
        data tmp = a.front(); a.pop();
        for (i_i i : g[tmp.pos]) {
            if (table[tmp.tick][i.second] > tmp.cost + i.first) {
                table[tmp.tick][i.second] = tmp.cost + i.first;
                dt.pos = i.second; dt.tick = tmp.tick; 
                dt.cost = tmp.cost + i.first;
                a.push(dt);
            }
            if (tmp.tick && 
                    table[tmp.tick-1][i.second] > tmp.cost + i.first) {
                table[tmp.tick-1][i.second] = tmp.cost + i.first / 2;
                dt.pos = i.second; dt.tick = tmp.tick-1; 
                dt.cost = tmp.cost + i.first / 2;
                a.push(dt);
            }
        }
    }
    int ret = INF;
    for (int i = 0; i < c+1; i++) {
        ret = min(ret, table[i][d]);
    }
    return ret;
}

C++: #include <iostream>
#include <algorithm>

const int MAX_N = 100, MAX_C = 11, INF = 1 << 24;
int dist[MAX_C][MAX_N], used[MAX_C][MAX_N], cost[MAX_N][MAX_N];
int c, n, m, s, d;

void dijkstra(int s){
	for(int i=0;i<MAX_C;i++){
		std::fill(dist[i], dist[i]+MAX_N, INF);
		std::fill(used[i], used[i]+MAX_N, 0);
	}

	dist[0][s] = 0;

	while(true){
		int u = -1, v;
		for(int i=0;i<=c;i++){
			for(int j=0;j<n;j++){
				if(!used[i][j] && (u == -1 || dist[i][j] < dist[u][v]))
					u = i, v = j;
			}
		}

		if(u == -1)break;
		used[u][v] = 1;


		for(int i=0;i<n;i++){
			dist[u][i] = std::min(dist[u][i], dist[u][v] + cost[v][i]);
			if(u+1 < n)
				dist[u+1][i] = std::min(dist[u+1][i], dist[u][v] + cost[v][i] / 2);
		}
	}
}

int main(){
	while(std::cin >> c >> n >> m >> s >> d, c){

		for(int i=0;i<MAX_C;i++){
			std::fill(cost[i], cost[i]+MAX_C, INF);
		}

		for(int i=m;i--;){
			int a, b, f;
			std::cin >> a >> b >> f;
			cost[a-1][b-1] = f;
			cost[b-1][a-1] = f;
		}

		dijkstra(s-1);

		int res = INF;
		for(int i=0;i<=c;i++){
			res = std::min(res, dist[i][d-1]);
		}

		std::cout << res << std::endl;
	}
}

C++: #include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int c, n, m, s, d, a, b, f, dp[11][100]; vector<pair<int, int> > G[100];
int main() {
	while(scanf("%d%d%d%d%d", &c, &n, &m, &s, &d), s--, d--, c) {
		for(int i = 0; i < n; i++) G[i].clear();
		for(int i = 0; i <= c; i++) {
			for(int j = 0; j < n; j++) dp[i][j] = 1000000000;
		}
		dp[0][s] = 0;
		for(int i = 0; i < m; i++) {
			scanf("%d%d%d", &a, &b, &f);
			G[a - 1].push_back(make_pair(b - 1, f));
			G[b - 1].push_back(make_pair(a - 1, f));
		}
		priority_queue<vector<int> > que; que.push({0, s, 0});
		while(!que.empty()) {
			vector<int> v1 = que.top(); que.pop();
			for(int i = 0; i < G[v1[1]].size(); i++) {
				int u = G[v1[1]][i].first, w = G[v1[1]][i].second;
				if(dp[v1[2]][u] > dp[v1[2]][v1[1]] + w) {
					dp[v1[2]][u] = dp[v1[2]][v1[1]] + w;
					que.push({-dp[v1[2]][u], u, v1[2]});
				}
				if(v1[2] < c && dp[v1[2] + 1][u] > dp[v1[2]][v1[1]] + w / 2) {
					dp[v1[2] + 1][u] = dp[v1[2]][v1[1]] + w / 2;
					que.push({-dp[v1[2] + 1][u], u, v1[2] + 1});
				}
			}
		}
		int ret = 1000000000;
		for(int i = 0; i <= c; i++) ret = min(ret, dp[i][d]);
		printf("%d\n", ret);
	}
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

#define reep(i,n,m) for(int i=(n);i<(m);i++)
#define rep(i,n) reep(i,0,n)

using namespace std;

class Edge{
public:
    int to;
    int cost;
    Edge(int to,int cost):to(to),cost(cost){}
};

class Node{
public:
    int used;
    int p;
    int cost;
    Node(int u,int n,int c):used(u),p(n),cost(c){}

    bool operator<(const Node &a)const{
        return cost>a.cost;
    }
};

int main(){
    int c,n,m,s,d;
    while(cin>>c>>n>>m>>s>>d,c+n+m+s+d){

        vector<Edge> edge[101];
        rep(i,m){
            int a,b,f;
            cin >> a >> b >> f;
            edge[a].push_back(Edge(b,f));
            edge[b].push_back(Edge(a,f));
        }

        priority_queue<Node> Q;
        Q.push(Node(0,s,0));

        bool used[101][11] = {0};

        while(!Q.empty()){
            Node node = Q.top();Q.pop();
            if(used[node.p][node.used]){
                continue;
            }
            used[node.p][node.used] = true;

            int p = node.p;

            if(p==d){
                cout << node.cost << endl;
                break;
            }

            rep(i,edge[p].size()){
                Edge e = edge[p][i];
                int next = e.to;
                int cost = node.cost + e.cost;
                Q.push(Node(node.used,next,cost));
                if(node.used<c){
                    cost = node.cost + e.cost/2;
                    Q.push(Node(node.used+1,next,cost));
                }
            }
        }
    }
    return 0;
}