Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

var pass [][]int

func main() {
	for scanner.Scan() {
		pass = make([][]int, 200)
		for i := range pass {
			pass[i] = make([]int, 200)
		}
		buf := strings.Split(scanner.Text(), " ")
		c, _ := strconv.Atoi(buf[0])
		n, _ := strconv.Atoi(buf[1])
		m, _ := strconv.Atoi(buf[2])
		s, _ := strconv.Atoi(buf[3])
		d, _ := strconv.Atoi(buf[4])
		if c == 0 && n == 0 && m == 0 && s == 0 && d == 0 {
			break
		}
		s--
		d--
		for i := 0; i < 200; i++ {
			for j := 0; j < 200; j++ {
				pass[i][j] = INF
			}
		}
		for i := 0; i < 200; i++ {
			pass[i][i] = 0
		}
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			a, _ := strconv.Atoi(buf_[0])
			b, _ := strconv.Atoi(buf_[1])
			f, _ := strconv.Atoi(buf_[2])
			a--
			b--
			pass[a][b] = f
			pass[b][a] = f
		}
		fmt.Println(dijkstra(s, d, c, n))
	}
}

func dijkstra(s, g, c, n int) float64 {
	d := make([][]float64, 200)
	used := make([][]bool, 200)
	for i := 0; i < 200; i++ {
		d[i] = make([]float64, 20)
		used[i] = make([]bool, 20)
	}
	for i := range d {
		for j := range d[i] {
			d[i][j] = INF
		}
	}
	d[s][c] = 0
	for {
		v := -1
		w := -1
		for u := 0; u < n; u++ {
			for i := 0; i < c+1; i++ {
				if !used[u][i] && (v == -1 || d[u][i] < d[v][w]) {
					v = u
					w = i
				}
			}
		}
		if v == -1 {
			break
		}
		used[v][w] = true
		for u := 0; u < n; u++ {
			d[u][w] = math.Min(d[u][w], d[v][w]+float64(pass[v][u]))
			if w-1 >= 0 && pass[v][u] != INF {
				d[u][w-1] = math.Min(d[u][w-1], d[v][w]+(float64(pass[v][u]/2.0)))
			}
		}
	}
	ret := float64(INF)
	for i := 0; i < c+1; i++ {
		ret = math.Min(ret, d[g][i])
	}
	return ret
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>
#include<queue>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;
typedef pair<ll,ll> LLP;
int dx[4] = {1,0,0,-1};
int dy[4] = {0,1,-1,0};
#define INF 1000000007
#define MOD 1000000007
#define ARRAY_MAX 105
#define TICKET 15

int c,n,m,s,d;
vector<P> G[ARRAY_MAX];//to,cost
int cost[TICKET][ARRAY_MAX];//チケットの残り枚数、最短距離
bool used[TICKET][ARRAY_MAX];


void solve(){

    cost[c][s] = 0;//出発点
    priority_queue<pair<int,P>,vector<pair<int,P> >,greater<pair<int,P> > > que;
    que.push(pair<int,P>(0,P(s,c)));//コスト、現在位置、残りのチケット枚数
    while(!que.empty()){

        pair<int,P> tmp = que.top();
        que.pop();
        int now_position = tmp.second.first;//現在位置
        int now_ticket = tmp.second.second;//残りのチケット枚数
        int now_cost = tmp.first;//次の町までのコスト

        if(cost[now_ticket][now_position] < now_cost){
            continue;
        }


        for(int i = 0;i < G[now_position].size();i++){
            //チケットを使わない場合
            if(cost[now_ticket][G[now_position][i].first] > cost[now_ticket][now_position] + G[now_position][i].second){
                cost[now_ticket][G[now_position][i].first] = cost[now_ticket][now_position] + G[now_position][i].second;
                que.push(pair<int,P>(cost[now_ticket][G[now_position][i].first],P(G[now_position][i].first,now_ticket)));
            }
            //チケットをc枚まで使う場合
            if(now_ticket > 0){
                if(cost[now_ticket-1][G[now_position][i].first] > cost[now_ticket][now_position] + G[now_position][i].second/2){
                    cost[now_ticket-1][G[now_position][i].first] = cost[now_ticket][now_position] + G[now_position][i].second/2;
                    que.push(pair<int,P>(cost[now_ticket-1][G[now_position][i].first],P(G[now_position][i].first,now_ticket-1)));
                }
            }
        }
    }
}

int main(){

    while(cin >> c >> n >> m >> s >> d,c|n|m|s|d){

        s--;
        d--;
        //cout << s << " " << d << endl;
        REP(i,ARRAY_MAX){//初期化
            G[i].clear();
        }

        REP(i,TICKET){//最短距離の初期化
            REP(j,ARRAY_MAX){
                cost[i][j] = INF;
                used[i][j] = false;
            }
        }

        REP(i,m){
            int a,b,f;
            cin >> a >> b >> f;
            a--;
            b--;
            G[a].push_back({b,f});
            G[b].push_back({a,f});
            //マップの作成
        }

        solve();

        int mini = INF;
        for(int i = 0;i <= c;i++){
            mini = min(mini,cost[i][d]);
        }
        cout << mini << endl;
    }


    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <complex>
#include <queue>
#include <map>
using namespace std;

#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

#define EPS (1e-10)
#define EQ(a,b) (abs((a)-(b)) < EPS)

typedef long long ll;
struct edge {int to, cost;};
typedef pair<int, int> P;
typedef complex<double> C;

const int INF = 99999999;
const int MAX_V = 110;
const int MAX_E = 510;

int cost[MAX_V][MAX_V];
int d[MAX_V], d2[MAX_V];
bool used[MAX_V];
int pre[MAX_V], pre2[MAX_V];
int V, E;
int c, s, g;

void init_cost() {
    REP(i, V) REP(j, V) cost[i][j] = INF;
}

//s: start
void dijkstra(int s, int *d, int *pre) {
    REP(i, V) d[i] = INF;
    REP(i, V) used[i] = false;
    REP(i, V) pre[i] = -1;
    d[s] = 0;

    while (true) {
        int v = -1;
        REP(u, V) if (!used[u] && (v == -1 | d[u] < d[v])) v = u;

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

        REP(u, V) {
            if (d[u] > d[v] + cost[v][u]) {
                d[u] = d[v] + cost[v][u];
                pre[u] = v;
            }
        }
    }
}

//t: terminal
vector<int> get_path(int t, int *pre) {
    vector<int> path;
    for (; t != -1; t = pre[t]) path.pb(t);
    reverse(ALL(path));
    return path;
}

void solve() {
    dijkstra(s, d, pre);
    dijkstra(g, d2, pre2);

    int ans = INF;

    REP(k, V) {
        vector<int> cost_vec;

        vector<int> path = get_path(k, pre);
        REP(i, path.size() - 1) {
            cost_vec.pb(cost[path[i]][path[i+1]]);
        }

        path = get_path(k, pre2);
        REP(i, path.size() - 1) {
             cost_vec.pb(cost[path[i]][path[i+1]]);
        }

        sort(ALL(cost_vec));
        reverse(ALL(cost_vec));

        // REP(i, cost_vec.size()) printf("%d ", cost_vec[i]);
        // printf("\n");

        int sm = 0;
        REP(i, cost_vec.size()) {
            if (i < c)  sm += cost_vec[i] / 2;
            else        sm += cost_vec[i];
        }

        ans = min(ans, sm);
    }

    printf("%d\n", ans);

}

int main() {
    while (true) {
        scanf("%d%d%d%d%d", &c, &V, &E, &s, &g);
        if (!c) return 0;
        s--; g--;
        init_cost();
        REP(i, E) {
            int f, t, c;
            scanf("%d%d%d", &f, &t, &c);
            f--; t--;
            cost[f][t] = c;
            cost[t][f] = c;
        }
        solve();
    }
}