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 <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
typedef long long ll;
using namespace std;

int mindis[101], pre[101];
bool used[101];
int eCost[101][101];

int getPathSum(int c, int p, int g){
	vector<int> edge;
	edge.push_back(eCost[p][g]);
		
	int to = p;
	for(int from = pre[to]; from != 0; from = pre[from]){
		edge.push_back(eCost[from][to]);
		to = from;
	}

	sort(edge.begin(), edge.end());

	int idx = edge.size()-1;
	for(int i = 0; i < c; i++){
		edge[idx--] /= 2;
		if(idx < 0) break;
	}
	
	int ans = 0;
	for(int i = 0; i < edge.size(); i++){
		ans += edge[i];
	}

	return ans;
}

int main(){
	int c, n, m, s, d;
	while(cin >> c >> n >> m >> s >> d){
		if(c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break;

		fill(mindis, mindis+101, 1<<30);
		fill(pre, pre+101, 0);
		fill(used, used+101, false);
		for(int i = 0; i < 101; i++) fill(eCost[i], eCost[i]+101, 1<<30);

		for(int i = 0; i < m; i++){
			int a, b, cost;
			cin >> a >> b >> cost;
			eCost[a][b] = cost;
			eCost[b][a] = cost;
		}

		mindis[s] = 0;
		vector<int> goals;

		//最短経路(ダイクストラ)
		while(true){
			int v = -1;
			for(int i = 1; i <= n; i++){
				if(!used[i] && (v == -1 || mindis[v] > mindis[i])) v = i;
			}

			if(v == d) break;
			used[v] = true;

			for(int i = 1; i <= n; i++){
				if(mindis[i] > mindis[v]+eCost[v][i]){
					if(i == d){
						//次がゴールならここでチケットを適用した総和を出して最終的な結果にする
						//それをgoalsに格納していく
						goals.push_back(getPathSum(c, v, i));
					}else{
						mindis[i] = mindis[v]+eCost[v][i];
						pre[i] = v;
					}
				}
			}
		}

		int minc = 100000000;
		for(int i = 0; i < goals.size(); i++){
			if(minc > goals[i]) minc = goals[i];
		}

		cout << minc << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <functional>
#include <map>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef pair<P, int> State;

const int N = 100, INF = 1e8;

int edge[N][N];

int dijkstra(int n, int s, int d, int c){
	priority_queue<State, vector<State>, greater<State> > q;
	vector<int> visited(n, INF);
	q.push(make_pair(make_pair(0, s), c));
	while(!q.empty()){
		int c = q.top().first.first;
		int curr = q.top().first.second;
		int dc = q.top().second;
		q.pop();
		visited[curr]  = c;
		if(curr == d) return c;
        if(c > visited[curr]) continue;
		for(int i(0);i < n;++i){
			if(edge[curr][i] != INF){
				int nc = c + edge[curr][i];
				int next = i;
                q.push(make_pair(make_pair(nc, next), dc));
				nc = c + edge[curr][i] / 2;
				if(dc > 0){
					q.push(make_pair(make_pair(nc, next), dc - 1));
				}
			}
		}
	}
	return -1;
}

int main(){
	int c, n, m, s, d, a, b, f;
	while(cin >> c >> n >> m >> s >> d, c && n && m && s && d){
		for(int i(0);i < N;++i){
			for(int j(0);j < N;++j){
				edge[i][j] = INF;
			}
		}
		--s;
		--d;
		for(int i(0);i < m;++i){
			cin >> a >> b >> f;
			--a;
			--b;
			edge[a][b] = edge[b][a] = f;
		}
        cout << dijkstra(n, s, d, c) << endl;
	}
}

C++: #include <cstdio>
#include <queue>
#include <algorithm>

#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

const int inf = 2e7;

typedef pair<int, int> P;

struct E{
	E(int t, int c) :
		t(t), c(c)
	{}

	int t, c;
};

int c, n, m, s, t;
vector<E> g[1100];
int d[1100];

int index(int i, int j){
	return i * (c + 1) + j;
}

int main(){
	while(1){
		scanf("%d%d%d%d%d", &c, &n, &m, &s, &t);
		--s;
		--t;
		if(c == 0){
			break;
		}
		rep(i, m){
			int a, b, k;
			scanf("%d%d%d", &a, &b, &k);
			--a;
			--b;
			rep(j, c + 1){
				g[index(a, j)].push_back(E(index(b, j), k));
				g[index(b, j)].push_back(E(index(a, j), k));
			}
			rep(j, c){
				g[index(a, j + 1)].push_back(E(index(b, j), k / 2));
				g[index(b, j + 1)].push_back(E(index(a, j), k / 2));
			}
		}

		fill_n(d, n * (c + 1), inf);

		priority_queue<P, vector<P>, greater<P> > q;
		d[index(s, c)] = 0;
		q.push(P(0, index(s, c)));
		while(!q.empty()){
			P v = q.top();
			q.pop();
			if(v.first > d[v.second]){
				continue;
			}
			rep(i, g[v.second].size()){
				E e = g[v.second][i];
				if(d[e.t] > v.first + e.c){
					d[e.t] = v.first + e.c;
					q.push(P(d[e.t], e.t));
				}
			}
		}

		printf("%d\n", *min_element(d + index(t, 0), d + index(t + 1, 0)));

		rep(i, n * (c + 1)){
			g[i].clear();
		}
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

const int inf = 1000001;

class St{
	public:
	int c, p, cost;
	St() {}
	St(int _c, int _p, int _cost): c(_c), p(_p), cost(_cost) {}
	
	bool operator>(const St& st) const{
		return cost > st.cost;
	}
};

int exdij(int a[101][101], int c, int n, int s, int t)
{
	int r = inf, mcost[101][11];
	bool vis[101][11];
	priority_queue<St, vector<St>, greater<St> > que;
	
	que.push(St(c, s, 0));
	fill(&vis[0][0], &vis[100][10], true);
	fill(&mcost[0][0], &mcost[100][10], inf);
	while(!que.empty()){
		St st = que.top();
		que.pop();
		if(st.p == t){
			r = st.cost;
			break;
		}
		if(!vis[st.p][st.c]){
			continue;
		}
		vis[st.p][st.c] = false;
		
		for(int i = 1; i <= n; i++){
			if(mcost[i][st.c] > st.cost+a[st.p][i]){
				mcost[i][st.c] = st.cost+a[st.p][i];
				que.push(St(st.c, i, mcost[i][st.c]));
			}
			if(st.c && mcost[i][st.c-1] > st.cost+a[st.p][i]/2){
				mcost[i][st.c-1] = st.cost+a[st.p][i]/2;
				que.push(St(st.c-1, i, mcost[i][st.c-1]));
			}
		}
	}
	
	return r;
}

int main()
{
	int c, n, m, s, d, t, u, f, a[101][101];
	
	while(cin>>c>>n>>m>>s>>d && (c+n+m+s+d)){
		fill(&a[0][0], &a[n][n], inf);
		for(int i = 0; i <= n; i++){
			a[i][i] = 0;
		}
		while(m--){
			cin >> t >> u >> f;
			a[t][u] = a[u][t] = f;
		}
		
		cout << exdij(a, c, n, s, d) << endl;
	}
	
	return 0;
}