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 FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int c,n,m,s,d;
int rosen[101][101] = {-1};

int main(){
    while(true){
	scanf("%d%d%d%d%d",&c,&n,&m,&s,&d);
	if(c==0||n==0||m==0||s==0||d==0) break;
	memset(rosen,-1,sizeof(rosen));
	REP(i,m){
	    int a,b,f;
	    cin >> a >> b >> f;
	    rosen[a][b] = f;
	    rosen[b][a] = f;
	}
	int ans = 100000000;
	vector<bool>  used(n+1,false);
	vector<int> nanika;
	used[s] = true;
	queue<int> q;
        queue<string> st;
	vector<bool> u(n+1,false);
	u[s] = true;
	q.push(s);
	st.push("");
	while(!q.empty()){
	    int p = q.front();
            string ss = st.front();
	    q.pop();
	    st.pop();
	    if(p == d){
		vector<int> v(0);
		int x = -1;
		int pre = s;
		REP(i,ss.size()){
		    if(ss[i]==' '){
			int var = stoi(ss.substr(x+1,i-1-x));
			v.PB(rosen[pre][var]);
			pre = var;
			x = i;
		    }
		}
		int sum = 0;
		int tc = min((int)(v.size()),c);
		sort(RALL(v));
		REP(i,tc){
		    v[i] /= 2;
		}
		REP(i,v.size()){
		    sum += v[i];
		}
		ans = min(ans,sum);
		continue;
	    }
	    FOR(i,1,n+1){
		if(rosen[p][i]>0 && !u[i]){
		    if(i!=d)
			u[i] = true;
		    q.push(i);
		    string temps = ss;
		    temps += to_string(i) + " ";
		    st.push(temps);
		}
	    }
	}
	cout << ans << endl;
    }
    return 0;
}

C++: //1.includes
#include<iostream>
#include<algorithm>
#include<queue>
#include<tuple>
#include<functional>
#include<vector>
using namespace std;

//2.defines
#define INF 1000000000000000000LL//10^18
#define MAX_N 1000
#define MAX_C 100
#define BLACK 2
#define GRAY 1
#define WHITE 0

//3.variables
priority_queue<tuple<long long, long long, long long>, vector<tuple<long long, long long, long long>>, greater<tuple<long long, long long, long long>>>Q;
long long dist[MAX_N][MAX_C];
long long color[MAX_N][MAX_C];
vector<pair<long long, long long>>X[MAX_N];
long long C, N, M, S, D, A, B, G;

//4.functions
void _memset() {
	for (int i = 0; i < MAX_N; i++) {
		for (int j = 0; j < MAX_C; j++) {
			dist[i][j] = 0;
			color[i][j] = WHITE;
		}
	}
	for (int i = 0; i < MAX_N; i++) {
		X[i].clear();
	}
	while (!Q.empty()) {
		Q.pop();
	}
}

long long dijkstra(long long s, long long t, long long u) {
	for (int i = 0; i < MAX_N; i++) {
		for (int j = 0; j < MAX_C; j++) {
			dist[i][j] = INF;
			color[i][j] = WHITE;
		}
	}
	Q.push(make_tuple(0, 0, s));
	dist[s][0] = 0;
	color[s][0] = GRAY;
	while (!Q.empty()) {
		tuple<long long, long long, long long> tup = Q.top();
		Q.pop();
		long long a1 = get<0>(tup);
		long long a2 = get<1>(tup);
		long long a3 = get<2>(tup);
		color[a3][a2] = BLACK;
		for (int i = 0; i < X[a3].size(); i++) {
			long long a4 = X[a3][i].first;
			long long a5 = X[a3][i].second;
			bool ok = true;
			if (color[a4][a2] == BLACK) { ok = false; }
			if (ok == true && a1 + a5 < dist[a4][a2]) {
				dist[a4][a2] = a1 + a5;
				color[a4][a2] = GRAY;
				Q.push(make_tuple(dist[a4][a2], a2, a4));
			}
			ok = true;
			if (color[a4][a2 + 1] == BLACK) { ok = false; }
			if (a2 + 1 > u) { ok = false; }
			if (ok == true && a1 + a5 / 2 < dist[a4][a2 + 1]) {
				dist[a4][a2 + 1] = a1 + a5 / 2;
				color[a4][a2 + 1] = GRAY;
				Q.push(make_tuple(dist[a4][a2 + 1], a2 + 1, a4));
			}
		}
	}
	long long minx = INF;
	for (int i = 0; i <= u; i++) {
		minx = min(minx, dist[t][i]);
	}
	return minx;
}

int main() {
	while (true) {
		_memset();
		cin >> C >> N >> M >> S >> D;
		if (C == 0 && N == 0 && M == 0 && S == 0 && D == 0) {
			break; //C=number of ticket
		}
		for (int i = 0; i < M; i++) {
			cin >> A >> B >> G;
			X[A].push_back(make_pair(B, G));
			X[B].push_back(make_pair(A, G));
		}
		cout << dijkstra(S, D, C) << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
	int c,n,m,s,d; while(cin>>c>>n>>m>>s>>d,c){
		s--,d--;
		int a,b,f;
		int ad[n][n],cost[c+1][n];
		rep(i,n)rep(j,n)ad[i][j]=0;
		rep(i,c+1)rep(j,n)cost[i][j]=1<<20;
		rep(i,m)cin>>a>>b>>f,a--,b--,ad[a][b]=ad[b][a]=f;
		queue<pair<int,int> > F; F.push(make_pair(s,c));
		cost[c][s]=0;
		while(!F.empty()){
			int cnode=F.front().first,cc=F.front().second; F.pop();
			rep(i,n)if(ad[cnode][i]){
				if(cost[cc][i]>cost[cc][cnode]+ad[cnode][i])
				cost[cc][i]=cost[cc][cnode]+ad[cnode][i],
				F.push(make_pair(i,cc));
				if(cc>0&&cost[cc-1][i]>cost[cc][cnode]+ad[cnode][i]/2)
				cost[cc-1][i]=cost[cc][cnode]+ad[cnode][i]/2,
				F.push(make_pair(i,cc-1));
			}
		}
		int mi;rep(i,c+1)if(cost[i][d]!=1<<20){mi=i;break;}
		cout<<cost[mi][d]<<endl;
	}
	return 0;
}