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

using namespace std;

typedef pair<int, int> P;
const int N = 100, INF = 10e6;
int edge[N][N];

vector<int> dijkstra(int n, int s, int d){
    priority_queue<P> pq;
    vector<int> ret;
    int visited[n], now = d;
    fill(visited, visited + n, 0);
    pq.push(make_pair(1, s));
    visited[s] = 1;
    while(!pq.empty()){
        P t = pq.top();
        pq.pop();
        visited[t.second] = t.first;
        if(t.second == d) break;
        for(int i = 0;i < n;++i){
            if(edge[t.second][i] != INF && !visited[i]){
                pq.push(make_pair(t.first + 1, i));
            }
        }
    }
    while(now != s){
        int m = INF, t = now;
        for(int i = 0;i < n;++i){
            if(visited[i] == visited[now] - 1 && edge[now][i] != INF){
                m = min(m, edge[now][i]);
                t = m == edge[now][i] ? i : t;
            }
        }
        ret.push_back(m);
        now = t;
    }
    return ret;
}

int main(){
    int c, n, m, s, d;
    while(cin >> c >> n >> m >> s >> d, c){
        int a, b, f, res = 0;
        --s; --d;
        for(int i = 0;i < N;++i){
            fill(edge[i], edge[i] + N, INF);
        }
        for(int i = 0;i < m;++i){
            cin >> a >> b >> f;
            --a; --b;
            edge[a][b] = edge[b][a] = f;
        }
        vector<int> r = dijkstra(n, s, d);
        sort(r.begin(), r.end(), greater<int>());
        vector<int>::iterator vit = r.begin();
        for(;vit != r.end();++vit){
            if(c != 0){
                *vit = *vit / 2;
                --c;
            }
            res += *vit;
        }
        cout << res << endl;
    }
    return 0;
}

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

#define N 104
#define C 53
#define INF 1000000007
#define rep(i, n) for(int i=0; i<n; ++i)
#define min(a,b) (a>b?b:a)

struct Edge {
	int to, cost;
};

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

typedef vector<Edge> Graph;

Graph g[N];

int dist[N][C];
int n, c;
int dijkstra(int s, int t) {
	priority_queue<IP, vector<IP>, greater<IP> > q;
	rep(i, n) rep(j, c+2) dist[i][j] = INF;
	dist[s][0] = 0;
	q.push(IP(0, P(s, 0)));
	while(!q.empty()) {
		int cost = q.top().first, u = q.top().second.first;
		int p = q.top().second.second; q.pop();
		if(dist[u][p] < cost) continue;
		rep(i, g[u].size()) {
			Edge &e = g[u][i];
			if(dist[u][p] + e.cost < dist[e.to][p]) {
				dist[e.to][p] = dist[u][p] + e.cost;
				q.push(IP(dist[e.to][p], P(e.to, p)));
			}
			if(p < c && dist[u][p] + (e.cost / 2) < dist[e.to][p+1]) {
				dist[e.to][p+1] = dist[u][p] + (e.cost / 2);
				q.push(IP(dist[e.to][p+1], P(e.to, p+1)));
			}
		}
	}
	int ans = INF;
	rep(i, c+1) ans = min(ans, dist[t][i]);
	return ans;
}


int main() {
	int m, s, d;
	while(cin >> c >> n >> m >> s >> d && c && n && m && s && d) {
		--s; --d;
		rep(i, n) g[i].clear();
		rep(i, m) {
			int a, b, f;
			cin >> a >> b >> f; --a; --b;
			g[a].push_back((Edge){b, f});
			g[b].push_back((Edge){a, f});
		}
		cout << dijkstra(s, d) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <algorithm>
#define INF 9999999
using namespace std;

class State {
public:
  int id;
  int cost;
  int tiket;
  bool operator <(const State &s) const {
    return cost > s.cost;
  }
  State(int id, int cost, int tiket) : id(id), cost(cost), tiket(tiket) {}
};

int main() {
  while(1) {
    int c, n, m, s, d;
    cin >> c >> n >> m >> s >> d;
    if(c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break;
    int data[n+1][n+1];
    fill(&data[0][0], &data[n][n]+1, INF);
    for(int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      data[a][b] = data[b][a] = f;
    }

    priority_queue<State> PQ;
    PQ.push(State(s, 0, c));
    int visited[n+1][c+1];
    int cost[n+1][c+1];
    fill(&visited[0][0], &visited[n][c] + 1, false);
    fill(&cost[0][0], &cost[n][c] + 1, INF);
    cost[s][c] = 0;
    while(!PQ.empty()) {
      State s = PQ.top();
      PQ.pop();
      visited[s.id][s.tiket] = true;
      for(int i = 1; i <= n; i++) {
	if(!visited[i][s.tiket] && data[s.id][i] != INF) {
	  int newValue = cost[s.id][s.tiket] + data[s.id][i];
	  if(newValue < cost[i][s.tiket]) {
	    cost[i][s.tiket] = newValue;
	    PQ.push(State(i, newValue, s.tiket));
	  }
	}
	if(s.tiket >= 1 && !visited[i][s.tiket-1] && data[s.id][i] != INF) {
	  int newValue = cost[s.id][s.tiket] + data[s.id][i]/2;
	  if(newValue < cost[i][s.tiket-1]) {
	    cost[i][s.tiket-1] = newValue;
	    PQ.push(State(i, newValue, s.tiket-1));
	  }
	}
      }
    }
    cout << cost[d][0] << endl;
  }
  return 0;
}