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

using namespace std;
typedef pair<int, int> Pi;
typedef pair<int, Pi> Ti;

int c, n, m, s, d;
vector<vector<Pi>> edge;

int djk(int s, int g) {
	int d[100][11];
	for (int i = 0; i < n; i++)for (int j = 0; j <= c; j++)d[i][j] = 1000000000;
	d[s][c] = 0;
	priority_queue<Ti, vector<Ti>, greater<Ti>> pq;
	pq.push(make_pair(0, make_pair(s, c)));
	while (pq.size()) {
		Ti tmp = pq.top(); pq.pop();
		int N = tmp.second.first, ticket = tmp.second.second;
		if (d[N][ticket] < tmp.first)continue;
		for (int i = 0; i < edge[N].size(); i++) {
			int to = edge[N][i].first, cost = edge[N][i].second;
			if (ticket > 0) {
				if (d[to][ticket - 1] > d[N][ticket] + cost / 2) {
					d[to][ticket - 1] = d[N][ticket] + cost / 2;
					pq.push(make_pair(d[to][ticket - 1], make_pair(to, ticket - 1)));
				}
			}
			else {
				if (d[to][ticket] > d[N][ticket] + cost) {
					d[to][ticket] = d[N][ticket] + cost;
					pq.push(make_pair(d[to][ticket], make_pair(to, ticket)));
				}
			}
		}
	}
	int ans = 1000000000;
	for (int i = 0; i <= c; i++)ans = min(ans, d[g][i]);
	return ans;
}

int main() {
	while (cin >> c >> n >> m >> s >> d, c + n + m + s + d) {
		s--, d--;
		edge.resize(n);

		for (int i = 0; i < m; i++) {
			int a, b, f;
			cin >> a >> b >> f, a--, b--;
			edge[a].push_back(make_pair(b, f));
			edge[b].push_back(make_pair(a, f));
		}
		cout << djk(s, d) << endl;
		edge.clear();
	}
	return 0;
}

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;
const int MAX = 101;

int mindis[11][101];
bool used[11][101];
int eCost[101][101];

int main(){
	int c, n, m, s, d;
	while(cin >> c >> n >> m >> s >> d && c|n|m|s|d){
		for(int i = 0; i < 11; i++){ 
			fill(mindis[i], mindis[i]+MAX, 1<<29);
			fill(used[i], used[i]+MAX, false);
		}
		for(int i = 0; i < MAX; i++) fill(eCost[i], eCost[i]+MAX, 1<<29);

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

		mindis[0][s] = 0;

		while(1){
			//firstが使った割引チケット数、secondが頂点
			pair<int, int> v(-1, -1);
			int mostmin = 1<<29;
			for(int i = 0; i <= c; i++){
				for(int j = 1; j <= n; j++){
					if(!used[i][j] && ((v.first == -1 || v.second == -1) || mindis[i][j] < mostmin)){
						v.first = i;
						v.second = j;
						mostmin = mindis[i][j];
					}
				}
			}

			if(v.first == c && v.second == n) break;
			used[v.first][v.second] = true;


			for(int i = 1; i <= n; i++){
				if(v.first<c){
					if(mindis[v.first+1][i] > mindis[v.first][v.second]+(eCost[v.second][i]/2)){
						mindis[v.first+1][i] = mindis[v.first][v.second]+(eCost[v.second][i]/2);
					}
				}
				if(mindis[v.first][i] > mindis[v.first][v.second]+eCost[v.second][i]){
					mindis[v.first][i] = mindis[v.first][v.second]+eCost[v.second][i];
				}
			}
		}

		int minc = 1<<29;
		for(int i = 0; i <= c; i++){
			minc = minc>mindis[i][d]?mindis[i][d]:minc;
		}

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

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[110], pre[110];
bool used[110];
int eCost[110][110];

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+110, 1<<30);
		fill(pre, pre+110, 0);
		fill(used, used+110, false);
		for(int i = 0; i < 110; i++) fill(eCost[i], eCost[i]+110, 0);

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