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 <algorithm>
using namespace std;
#define MAX 101
#define INF 9999999

int c,n,m,s,d,a,b,f,cost[MAX+1][MAX+1],dd[MAX+1][MAX+1];
bool used[MAX+1][MAX+1];
int x,y;

int dijkstra(int s,int d,int c){
    fill(dd[0],dd[MAX],INF);
    fill(used[0],used[MAX],false);
    
    dd[c][s-1] = 0;
    while(true){
        int Mini = -1 , Minj;
        
        for(int i = y ; i <= x ; i++){
            for(int j = 0 ; j <= c ; j++){
                if((dd[j][i] < dd[Minj][Mini] || Mini == -1)&&!used[j][i]){
                    Mini = i;
                    Minj = j;
                }
            }
        }
        
        if(Mini == -1) break;
        used[Minj][Mini] = true;
        
        for(int i = y ; i <= x ; i++){
            dd[Minj][i] = min(dd[Minj][i],dd[Minj][Mini]+cost[Mini][i]);
            if(Minj > 0) dd[Minj-1][i] = min(dd[Minj-1][i],dd[Minj][Mini]+cost[Mini][i]/2);
        }
    }
    
    int res = INF;
    for(int i = 0 ; i <= c ; i++){
        res = min(res,dd[i][d-1]);
    }
    
    return res;
}

int main(){
    x = 0; y = INF;
    while(cin >> c >> n >> m >> s >> d, c+n+m+s+d){
        fill(cost[0],cost[MAX],INF);
        for(int i = 0 ; i < m ; i++){
            cin >> a >> b >> f;
            cost[a-1][b-1] = f;
            cost[b-1][a-1] = f;
            x = max(x,max(a-1,b-1));
            y = min(y,min(a-1,b-1));
        }
        
        cout << dijkstra(s,d,c) << endl;
    }
    
    return 0;
}

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

using namespace std;

struct Node
{
	int num, cost, ticket;
	Node(int n, int c, int t)
	{
		num = n;cost = c; ticket = t;
	}

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

const int MAX_NODE_ID = 512;
const int MAX_TICKET = 16;
bool visited[MAX_NODE_ID][MAX_TICKET];

int c, n, m, s, d;

#define MAX 50000
int map[101][101];

int main()
{
	while (true)
	{
		cin >> c >> n >> m >> s >> d;
		if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break;
		for (int i = 0; i <= n; i++)
			for (int j = 0; j <= n; j++)
				map[i][j] = MAX;
		for (int i = 0; i < m; i++)
		{
			int a, b, f;
			cin >> a >> b >> f;
			map[a][b] = map[b][a] = f;
		}
		for (int i = 0; i < MAX_NODE_ID; i++)
			for (int j = 0; j < MAX_TICKET; j++)
				visited[i][j] = false;
		priority_queue<Node> que;
		que.push(Node(s, 0, c));
		while (!que.empty())
		{
			Node nod = que.top();
			que.pop();
			if (nod.num == d)
			{
				cout <<	nod.cost << endl;
				break;
			}
			else
			{
				for (int i = 1; i <= n; i++)
				{
					if (map[nod.num][i] < MAX && !visited[nod.num][nod.ticket])
					{
						que.push(Node(i, nod.cost + map[nod.num][i], nod.ticket));
						if (nod.ticket > 0)
						{
							que.push(Node(i, nod.cost + map[nod.num][i] / 2, nod.ticket - 1));
						}
					}
				}
			}
			visited[nod.num][nod.ticket] = true;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)

const int INF = 1 << 24;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};

typedef unsigned long long ull;

struct Edge{
    int to, cost;
};

int C, N, M, S, D;
std::vector<Edge> G[110];
int d[110][20], used[110][20];

void dijkstra(){
    REP(i, 110){
        REP(j, 20){
            d[i][j] = INF;
            used[i][j] = 0;
        }
    }

    d[S][0] = 0;

    while(1){
        int u = -1, v = -1;
        REP(i, N){
            REP(j, C){
                if(!used[i][j] && (u == -1 || d[u][v] > d[i][j])){u = i; v = j;}
            }
        }

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

        REP(i, G[u].size()){
            Edge &e = G[u][i];
            d[e.to][v+1] = std::min(d[e.to][v+1], d[u][v] + e.cost / 2);
            d[e.to][v] = std::min(d[e.to][v], d[u][v] + e.cost);
        }
    }
}

int main(){
    while(std::cin >> C >> N >> M >> S >> D, C){
        S--; D--;

        REP(i, 110){
            G[i].clear();
        }

        REP(i, M){
            int from, to, cost;
            std::cin >> from >> to >> cost;
            from--; to--;
        
            G[from].push_back({to, cost});
            G[to].push_back({from, cost});
        }

        dijkstra();
        std::cout << d[D][C] << std::endl;
    }
}

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

		std::cout << dist[c][d-1] << std::endl;
	}
}