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++: #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});
                    }
                }
            }
        }
    }
}