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 <queue>
#include <array>
#include <vector>
#include <functional>

using namespace std;
#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)
#define eacha(e,c) for(auto &e:c)

struct edge {
    int to,cost;
};
struct state {
    int v,d;
};

vector<vector<edge>> G;
array<array<int,11>,101> dp;
auto compfn=[](state& s,state& t){
    return s.d>t.d;
};
priority_queue<state,
               vector<state>,
               function<bool(state&,state&)>> q(compfn);
int c,n,m,s,d;

int main(){
    while(cin>>c>>n>>m>>s>>d, c|n|m|s|d){
        vector<vector<edge>>(n+1).swap(G);
        rep(i,m){
            int f,t,c; cin>>f>>t>>c;
            G[f].push_back({t,c});
            G[t].push_back({f,c});
        }
        rep(i,c+1){
            dp[i].fill(1<<30);
            dp[i][s]=0;
            priority_queue<state,
                           vector<state>,
                           function<bool(state&,state&)>>(compfn).swap(q);
            q.push({s,0});
            while(q.size()){
                state sta=q.top(); q.pop();
                if(dp[i][sta.v]<sta.d) continue;
                eacha(e,G[sta.v]){
                    int t;
                    if(i==0) t=dp[0][sta.v]+e.cost;
                    else t=min(dp[i][sta.v]+e.cost,
                               dp[i-1][sta.v]+e.cost/2);
                    if(dp[i][e.to]>t){
                        dp[i][e.to]=t;
                        q.push({e.to,t});
                    }
                }
            }
        }
        cout<<dp[c][d]<<endl;
    }
}

C++: #include <bits/stdc++.h>
//#include <math.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
//typedef pair<int,int> P;
//typedef pair<int,P> PP;

//-----------------------------------------------------------------------------

#define INIT INF
typedef int Weight;
typedef pair<Weight,int> P;

typedef pair<P,int> PP;

int c,n,m,s,g;

struct Edge {
	int to;
	Weight cost;
	Edge(int to,Weight cost):to(to),cost(cost) {}
};

typedef vector< vector<Edge> > Graph;

bool operator<(const Edge &e,const Edge &f) {
	return e.cost!=f.cost?e.cost>f.cost:e.to<f.to;
}

void dijkstra(const Graph &G,int s,vector< vector<Weight> > &dist) {
	int n=(int)G.size();
	dist.assign(n,vector<Weight>(c+1,INIT));
	dist[s][0]=0;
	priority_queue< PP,vector<PP>,greater<PP> > Q;
	Q.push(PP(P(0,s),0));
	while(!Q.empty()) {
		PP p=Q.top();Q.pop();
		int v=p.first.second,times=p.second;
		if(dist[v][times]<p.first.first) continue;
		for(auto e:G[v]) {
			if(dist[e.to][times]>dist[v][times]+e.cost) {
				dist[e.to][times]=dist[v][times]+e.cost;
				Q.push(PP(P(dist[e.to][times],e.to),times));
			}
			if(times+1<=c&&dist[e.to][times+1]>dist[v][times]+e.cost/2) {
				dist[e.to][times+1]=dist[v][times]+e.cost/2;
				Q.push(PP(P(dist[e.to][times+1],e.to),times+1));
			}
		}
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(true) {
	cin>>c>>n>>m>>s>>g;
	if(c==0&&n==0&&m==0&&s==0&&g==0) break;
	s--,g--;
	Graph G(n);
	vector< vector<Weight> > dist(n,vector<Weight>(c+1));
	REP(i,m) {
		int a,b;Weight c;
		cin>>a>>b>>c;
		a--,b--;
		G[a].emplace_back(b,c);
		G[b].emplace_back(a,c);
	}
	dijkstra(G,s,dist);
	Weight ans=INIT;
	REP(i,c+1) ans=min(ans,dist[g][i]);
	cout<<ans<<endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define llong long long 
typedef pair<int, int> P;

typedef struct data{
	int now;
	int time;
	int dep;

	bool operator > (const data& right) const{
		return dep == right.dep ? time > right.time : dep > right.dep;
	}

}data;

int c, n, m, s, d;
vector<P> edges[101];
int cost[101][11];
priority_queue<data, vector<data>, greater<data> > q;

void solve(){
	data D;
	D.now = s;
	D.time = 0;
	D.dep = 0;
	q.push(D);

	while(!q.empty()){
		data nd = q.top(); q.pop();
		int nowp = nd.now;
		int nowtime = nd.time;
		int nowdep = nd.dep;

		if(cost[nowp][nowdep] < nowtime) continue;

		for(int i = 0; i < edges[nowp].size(); i++){
			P edge = edges[nowp][i];
			if(cost[edge.first][nowdep] > cost[nowp][nowdep] + edge.second){
				data in;
				in.now = edge.first;
				in.time = cost[nowp][nowdep] + edge.second;
				in.dep = nd.dep;
				cost[in.now][in.dep] = in.time;
				q.push(in);
			}

			if(nowdep < c){
				if(cost[edge.first][nowdep + 1] > cost[nowp][nowdep] + edge.second / 2){
					data in;
					in.now = edge.first;
					in.time = nowtime + (edge.second / 2);
					in.dep = nowdep + 1;
					cost[in.now][in.dep] = in.time;
					q.push(in);
				}			
			}

		}
	}

	return;
}

int main(){
	while(cin >> c >> n >> m >> s >> d, n){
		fill_n(*cost, 101 * 11, 99999999);
		for(int i = 0; i <= c; i++){
			cost[s][i] = 0;
		}
		for(int i = 0; i < m; i++){
			int n, to, cos;
			cin >> n >> to >> cos;
			edges[n].push_back(P(to, cos));
			edges[to].push_back(P(n, cos));
		}

		solve();

		cout << cost[d][c] << endl;
		// for(int i = 1; i <= n; i++){
		// 	cout << cost[i][2] << endl;
		// }

		for(int i = 0; i < 101; i++){
			edges[i].clear();
		}
	}

	return 0;
}