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 <queue>
#include <utility>
using namespace std;

int calcCost(int c, int n, vector<int> path, vector<int> rt){
	priority_queue<int> costs;
	for(int i=1; i<rt.size(); i++){
		costs.push(path[rt[i]+n*rt[i-1]]);
	}
	int total = 0;
	while(costs.size() && c--){
		total += costs.top() / 2;
		costs.pop();
	}
	while(costs.size()){
		total += costs.top();
		costs.pop();
	}
	return total;
}
int main(){
	for(;;){
		int c,n,m,s,d;
		cin>>c>>n>>m>>s>>d;
		if(c+n+m+s+d==0) break;
		vector<pair<int, vector<int>>> route(n,make_pair(-1,vector<int>()));
		vector<int> path(n*n,-1);
		for(int i=0; i<m; i++){
			int a,b,f;
			cin>>a>>b>>f;
			path[--a+n*--b] = path[b+n*a] = f;
		}
		priority_queue<pair<int, vector<int>>, vector<pair<int, vector<int>>>, greater<pair<int, vector<int>>>> pq;
		pq.push(make_pair(0,vector<int>(1,s-1)));
		while(pq.size()){
			int cur = *(pq.top().second.rbegin());
			if(route[cur].first == -1){
				route[cur] = pq.top();
				for(int i=0; i<n; i++){
					if(route[i].first == -1 && path[i+n*cur] != -1){
						auto copy = pq.top();
						copy.second.push_back(i);
						copy.first = calcCost(c,n,path,copy.second);
						pq.push(copy);
					}
				}
			}
			pq.pop();
		}
		cout<<route[d-1].first<<endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <stack>
#include <list>

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,b,n) for(int i=b;i<n;i++)
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

const float PI = 3.1415926;
static const int INFTY = (1<<30);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int MAX = 100;

using namespace std;

typedef long long ll;
typedef long double ld;

int c, n, m, s, d;
int C[101][101], D[101][101];
int DP[101][16];

// co=割引券, cost=現在の値段
void solve(int st, int co, int cost) {
  if (st == d-1) {
    return;
  }
  // 次の町探し
  rep(i, n) {
    if (i == st || C[st][i] == INFTY) {
      continue;
    }
    int tmp = cost + C[st][i];
    int tmp2 = cost + C[st][i]/2;
    if (DP[i][co] > tmp) {
      DP[i][co] = tmp;
      solve(i, co, tmp);
    }
    if (co < c && DP[i][co+1] > tmp2) {
      DP[i][co+1] = tmp2;
      solve(i, co+1, tmp2);
    }
  }
}


int main() {
  while (cin >> c >> n >> m >> s >> d, c) {
    rep(i, n) {
      rep(j, n) {
        C[i][j] = INFTY;
        D[i][j] = INFTY;
      }
      rep (q, c+1) {
        DP[i][q] = INFTY;
      }
    }
    rep(i, m) {
      int a,b,f;
      cin >> a >> b  >> f;
      C[a-1][b-1] = f;
      C[b-1][a-1] = f;
    }
    solve(s-1, 0, 0);
    int res = INFTY;
    rep(i, c+1) {
      res = min(res, DP[d-1][i]);
    }
    cout << res << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
 
using ll = long long int;
using ull = unsigned long long int;
using P = pair<int, int>;
using P3 = pair<int,P>;
using PP = pair<P ,P>;
constexpr ll MOD = ll(1e9+7);
constexpr int IINF = INT_MAX;

vector<vector<P> > g;
int dist[105][15];

int main(){
    int c, n, m, s, d;
    while(cin >> c >> n >> m >> s >> d, n){
        s--; d--;
        g.clear();
        g.resize(n);
        for(int i=0;i<m;i++){
            int a, b, f;
            cin >> a >> b >> f;
            a--; b--;
            g[a].push_back(P(b,f));
            g[b].push_back(P(a,f));
        }
        for(int i=0;i<n;i++){
            fill(dist[i], dist[i]+c+1, IINF/2);
        }
        dist[s][c] = 0;
        priority_queue<P3, vector<P3>, greater<P3> > que;
        que.push(P3(0, P(s, c)));
        while(!que.empty()){
            auto p = que.top();
            que.pop();
            int now = p.second.first, r = p.second.second;
            if(dist[now][r] < p.first) continue;
            for(auto e : g[now]){
                int nxt = e.first, f = e.second;
                if(dist[nxt][r] > dist[now][r] + f){
                    dist[nxt][r] = dist[now][r] + f;
                    que.push(P3(dist[nxt][r], P(nxt,r)));
                }
                if(r>0 && dist[nxt][r-1] > dist[now][r] + f/2){
                    dist[nxt][r-1] = dist[now][r] + f/2;
                    que.push(P3(dist[nxt][r-1], P(nxt,r-1)));
                }
            }
        }
        int ans = IINF;
        for(int i=0;i<=c;i++){
            ans = min(ans, dist[d][i]);
        }
        cout << ans << endl;
    }

    return 0;
}