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
}

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)
		for len(pq) != 0 {
			now := heap.Pop(&pq).(*Node)
			if now.NowPosition == d {
				fmt.Println(now.Amount)
				break
			}
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				heap.Push(&pq, next)
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					heap.Push(&pq, next)
				}
			}
		}
	}
}

C++: #define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <unordered_map> 
#include <unordered_set>
#include <functional>

using namespace std;

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

long long int INF = 1e18;
long long int MOD = 1e9 + 7;

#define MAX_V 200000

struct edge{
	int to;
	long long int cost;
    edge(int to_, long long int cost_){
        to = to_;
        cost = cost_;
    }
};

vector<edge> G[MAX_V];
long long int d[MAX_V];

// O(ElogV)

void shortest_path(int s, int V){
	
	priority_queue<P, vector<P>, greater<P> > que;
	
	for(int i = 0; i < V; i++){
		d[i] = INF;
	}
	d[s] = 0;
	
	que.push(P(0, s));
	
	while(!que.empty()){
		
		P p = que.top();
		que.pop();
		
		int v = p.second;
		
		if(d[v] < p.first){
			continue;
		}
		
		for(int i = 0; i < G[v].size(); i++){
			edge e = G[v][i];
			if(d[e.to] > e.cost + d[v]){
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int main(){
	while(true){
		int c, n, m, s, g;
		cin >> c >> n >> m >> s >> g;
		if(c == 0){
			break;
		}
		s -= 1;
		g -= 1;
		for(int i = 0; i < m; i++){
			int a, b, f;
			cin >> a >> b >> f;
			a -= 1;
			b -= 1;
			G[a].emplace_back(b, f);
			G[b].emplace_back(a, f);
			for(int j = 0; j < c; j++){
				G[a + n * j].emplace_back(b + n * (j + 1), f / 2);
				G[b + n * j].emplace_back(a + n * (j + 1), f / 2);
			}
			for(int j = 0; j <= c; j++){
				G[a + n * j].emplace_back(b + n * j, f);
				G[b + n * j].emplace_back(a + n * j, f);
			}
		}
		shortest_path(s, n * (c + 1));
		long long int ans = INF;
		for(int i = 0; i <= c; i++){
			ans = min(ans, d[g + n * i]);
		}
		cout << ans << endl;
		for(int i = 0; i < n * (c + 1); i++){
			G[i].clear();
		}
	}
	return 0;
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define INF 1e8

struct edge {
    int to, cost;
};

struct P {
    int from, cost, ticket;

    bool operator >(const P& p) const {
        return cost > p.cost;
    }
};

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

int ticket_n, node_n, edge_n, start, goal;

int main() {
    while (cin >> ticket_n >> node_n >> edge_n >> start >> goal, ticket_n|node_n|edge_n|start|goal) {
        vector<vector<edge>> E(node_n+1);
        REP(i, edge_n) {
            int from, to, cost;
            cin >> from >> to >> cost;
            E[from].push_back({to, cost});
            E[to].push_back({from, cost});
        }

        vvi G(edge_n+1, vi(ticket_n+1, INF));
        priority_queue<P, vector<P>, greater<P>> q;
        q.push({start, 0, ticket_n});
        while (!q.empty()) {
            P p = q.top(); q.pop();

            if (p.from == goal) {
                cout << p.cost << endl;
                break;
            }

            int from = p.from, cost = p.cost, ticket = p.ticket;
            for (edge e : E[from]) {
                // use ticket
                if (G[e.to][ticket] > cost + e.cost) {
                    G[e.to][ticket] = cost + e.cost;
                    q.push({e.to, cost + e.cost, ticket});
                }
                // don't use ticket
                if (ticket-1 >= 0) {
                    if (G[e.to][ticket-1] > cost + e.cost/2) {
                        G[e.to][ticket-1] = cost + e.cost/2;
                        q.push({e.to, cost + e.cost/2, ticket-1});
                    }
                }
            }
        }
    }
}