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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T>
int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

const double EPS = 1e-8;
typedef long long ll;
typedef pair<int, int> pint;

typedef pair<int, pint> P;

int dp[101][11];

int main()
{
	int c, n, m, s, d;
	while (scanf("%d%d%d%d%d", &c, &n, &m, &s, &d), c|n|m|s|d)
	{
		vector<pint> edge[101];
		for (int i = 0; i < m; ++i)
		{
			int a, b, f;
			scanf("%d%d%d", &a, &b, &f);
			edge[a].push_back(pint(b, f));
			edge[b].push_back(pint(a, f));
		}

		const int INF = 2 << 25;
		for (int i = 1; i <= n; ++i)
			for (int j = 0; j <= c; ++j)
				dp[i][j] = INF;

		priority_queue<P, vector<P>, greater<P> > q;
		q.push(P(0, pint(s, c)));
		while (!q.empty())
		{
			P e = q.top();
			q.pop();
			int cost = e.first;
			int from = e.second.first;
			int ticket = e.second.second;

			if (cost >= dp[from][ticket])
				continue;
			dp[from][ticket] = cost;

			foreach (it, edge[from])
			{
				int to = it->first;
				int f = it->second;
				q.push(P(cost + f, pint(to, ticket)));
				if (ticket)
					q.push(P(cost + f/2, pint(to, ticket-1)));
			}
		}

		printf("%d\n", *min_element(dp[d], dp[d]+c+1));
	}

	return 0;
}

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