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 <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <queue>
#include <bitset>
#include <string>
using namespace std;

#define REP(i,n) for(int i=0; i<n; i++)
#define RPA(i,s,e) for(int i=s; i<=e; i++)
#define RPD(i,s,e) for(int i=s; i>=e; i--)
#define PB(a) push_back(a)
#define MP(i,s) make_pair(i,s)
#define ALL(a) (a).begin(), (a).end()
#define PRT(a) cout << (a) << endl

typedef pair<int,int> P;

struct edge { int to, cost; };

const int INF = 1000000009;
const int MAX_V = 111;
const int MAX_C = 15;
vector<edge> G[MAX_V];

int c, n, m, s, d;
int p, q, r;
int dp[MAX_C][MAX_V];

void add_edge(int from, int to, int cost) {
	edge e1 = { to, cost };
	edge e2 = { from, cost };
	G[from].PB(e1);
	G[to].PB(e2);
}

void dijkstra(int f, int t, int k) {
	REP(j,n) dp[k][j] = INF;

	priority_queue<P, vector<P>, greater<P> > que;
	que.push(MP(0, f));
	dp[k][f] = 0;

	while(!que.empty()) {
		P p = que.top();
		que.pop();

		int v = p.second;
		REP(i,G[v].size()) {
			edge& e = G[v][i];
			int minval = dp[k][v] + e.cost;
			if(k > 0) minval = min(minval, dp[k-1][v] + e.cost / 2);
			if(dp[k][e.to] > minval) {
				dp[k][e.to] = minval;
				que.push(P(minval, e.to));
			}
		}
	}
}

void solve() {
	RPA(i,0,c) {
		dijkstra(s, d, i);
	}
	PRT(dp[c][d]);
}

void coding() {
	while(scanf("%d%d%d%d%d", &c, &n, &m, &s, &d) != EOF) {
		if(c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break;

		s--; d--;
		REP(i,n) G[i].clear();

		REP(i,m) {
			scanf("%d%d%d", &p, &q, &r);
			p--; q--;
			add_edge(p, q, r);
		}
		solve();
	}
}

//#define _LOCAL_TEST

int main() {
#ifdef _LOCAL_TEST
	freopen("a.in", "r", stdin);
#endif

	coding();

#ifdef _LOCAL_TEST
	cout << endl;
	cout << "This is local test" << endl;
	cout << "Do not forget to comment out _LOCAL_TEST" << endl;
#endif
}

C++: #include <cstdio>
#include <queue>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int,int> _P;
typedef pair<int,_P> P;
int main() {
	int c,n,m,s,d;
	int a,b,f;
	int i;
	int cost1[100][100],cost2[100][11];
	priority_queue<P,vector<P>,greater<P> > que;
	while(scanf("%d%d%d%d%d",&c,&n,&m,&s,&d),c) {
		fill(cost1[0],cost1[0]+100*100,1<<27);
		fill(cost2[0],cost2[0]+100*11,1<<27);
		for(i=0;i<m;i++) {
			scanf("%d%d%d",&a,&b,&f);
			cost1[a-1][b-1]=cost1[b-1][a-1]=min(cost1[a-1][b-1],f);
		}
		while(!que.empty()) que.pop();
		que.push(P(0,_P(s-1,0)));
		cost2[s-1][0]=0;
		while(!que.empty()) {
			P p=que.top();que.pop();
			if(cost2[p.second.first][p.second.second]<p.first) continue;
			for(i=0;i<n;i++) {
				if((p.first+cost1[p.second.first][i])<cost2[i][p.second.second]) {
					cost2[i][p.second.second]=p.first+cost1[p.second.first][i];
					que.push(P(cost2[i][p.second.second],_P(i,p.second.second)));
				}
				if(p.second.second+1<=c&&(p.first+cost1[p.second.first][i]/2)<cost2[i][p.second.second+1]) {
					cost2[i][p.second.second+1]=p.first+cost1[p.second.first][i]/2;
					que.push(P(cost2[i][p.second.second+1],_P(i,p.second.second+1)));
				}
			}
		}
		printf("%d\n",cost2[d-1][c]);
	}
}

C++: #include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int c, n, m, s, d;

int field[101][101];
int visited[11][101];

int main () {
	while (cin >> c >> n >> m >> s >> d, c || n || m || s || d) {
		memset(field, 0x3, sizeof(field));
		memset(visited, 0x3, sizeof(visited));
		for (int i = 0; i < m; i++) {
			int a, b, f;
			cin >> a >> b >> f;
			field[a][b] = field[b][a] = f / 10;
		}
		priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >, greater<pair<int, pair<int, int> > > > q;
		q.push(make_pair(0, make_pair(c, s)));
		while (!q.empty()) {
			pair<int, pair<int, int> > current = q.top();
			q.pop();
			if (current.second.second == d) {
				cout << current.first * 10 << endl;
				break;
			}
			for (int i = 1; i <= n; i++) {
				if (current.first + field[current.second.second][i] < visited[current.second.first][i]) {
					int next = current.first + field[current.second.second][i];
					q.push(make_pair(next, make_pair(current.second.first, i)));
					visited[current.second.first][i] = next;
				}
				if (current.second.first > 0 && current.first + field[current.second.second][i] / 2 < visited[current.second.first - 1][i]) {
					int next = current.first + field[current.second.second][i] / 2;
					q.push(make_pair(next, make_pair(current.second.first - 1, i)));
					visited[current.second.first - 1][i] = next;
				}
			}
		}
	}
	return 0;
}