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 <map>
#include <cassert>
#include <algorithm>
using namespace std;
typedef pair<int, int> edge;
struct state{
  int cost, coupon, station;
  state(int cost_, int coupon_, int station_):cost(cost_), coupon(coupon_), station(station_){}
  bool operator< (const state &s) const{
    /*
    if(cost == s.cost){
      return coupon > s.coupon;
    }
    */
    return cost > s.cost;
  }
};
int djikstra(vector<vector<edge> > graph, int start, int goal, int coupon){
  priority_queue<state> pq;
  pq.push(state(0, coupon, start));
  bool visited[coupon][graph.size()];
  //fill_n((bool*)visited, (coupon+1) * graph.size(), false);
  for(int i = 0; i < coupon+1; ++i){
    for(int j = 0; j < graph.size(); ++j){
      visited[i][j] = false;
    }
  }
  while(!pq.empty()){
    state cur = pq.top(); pq.pop();
    //cout << cur.station << ": " << cur.cost << ": " << cur.coupon << endl;
    if(visited[cur.coupon][cur.station]){
      continue;
    }
    if(cur.station == goal){
      return cur.cost;
    }
    visited[cur.coupon][cur.station] = true;
    for(int i = 0; i < graph[cur.station].size(); ++i){
      int to = graph[cur.station][i].first;
      int c = graph[cur.station][i].second;
      if(cur.coupon > 0){
        pq.push(state(cur.cost+c/2, cur.coupon-1, to));
      }
      pq.push(state(cur.cost+c, cur.coupon, to));
    }
  }
  //assert(false);
  return -1;
}
int main(){
  int c, n, m, s, d;
  while(true){
    cin >> c >> n >> m >> s >> d;
    if((c|n|m|s|d) == 0){
      break;
    }
    vector<vector<edge> > graph(n+1, vector<edge>());
    for(int i = 0; i < m; ++i){
      int a, b, f;
      cin >> a >> b >> f;
      graph[a].push_back(make_pair(b, f));
      graph[b].push_back(make_pair(a, f));
    }
    cout << djikstra(graph, s, d, c) << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;

const int INF = (int)1e9;

struct Edge {
  int src, dst, cost;
  Edge(int src_, int dst_, int cost_) : src(src_), dst(dst_), cost(cost_) { }
};

int main() {
  for(int C, N, M, S, T; cin >> C >> N >> M >> S >> T, C || N || M || S || T; ) {
    --S, --T;
    vector<Edge> G;
    for(int i = 0; i < M; ++i) {
      int a, b, f; cin >> a >> b >> f;
      --a, --b;
      G.emplace_back(a, b, f);
      G.emplace_back(b, a, f);
    }
    int D[N][C + 1];
    for(int i = 0; i < N; ++i)
      for(int j = 0; j <= C; ++j)
        D[i][j] = INF;
    D[S][C] = 0;
    while(true) {
      bool update = false;
      for(const Edge &e : G) {
        for(int j = 0; j <= C; ++j) {
          if(D[e.dst][j] > D[e.src][j] + e.cost) {
            D[e.dst][j] = D[e.src][j] + e.cost;
            update = true;
          }
          if(j > 0 && D[e.dst][j - 1] > D[e.src][j] + e.cost / 2) {
            D[e.dst][j - 1] = D[e.src][j] + e.cost / 2;
            update = true;
          }
        }
      }
      if(!update) break;
    }
    int ans = INF;
    for(int j = 0; j <= C; ++j) {
      ans = min(ans, D[T][j]);
    }
    cout << ans << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#define INF (1e9)
#define N 102
#define C 11
using namespace std;

typedef struct state {
  int cost,pos,ticket;
  state (int a,int b,int c): cost(a),pos(b),ticket(c){} 
  bool operator < (const state &s)const{
    return cost > s.cost;
  }
} state;

void dijkstra(int);
int c,n,d[N][C],M[N][N];
int main(){
  int m,s,ind,a,b,f;
  while(1){
    cin >> c >> n >> m >> s >> ind;
    if(c==0&&n==0&&m==0&&s==0&&ind==0) break;
    for(int i=0;i<N;i++)
      for(int j=0;j<N;j++)
	M[i][j]=INF;
    for(int i=0;i<m;i++){
      cin >> a >> b >> f;
      M[a][b]=M[b][a]=f;
    }
    dijkstra(s);
    cout << d[ind][c] << endl;
  }
  return 0;
}
void dijkstra(int s){
  priority_queue< state > q;
  for(int i=1;i<=n;i++)
    for(int j=1;j<=c;j++)
      d[i][j]=INF;
  d[s][0]=0;
  q.push( (state){0,s,0} );
  while(!q.empty()){
    state r=q.top();
    q.pop();
    int cos=r.cost;
    pair<int,int> u=make_pair(r.pos,r.ticket);
    if(d[u.first][u.second]<cos) continue;
    for(int i=1;i<=n;i++){
      if(d[u.first][u.second]!=INF&&M[u.first][i]!=INF){
	if(d[i][u.second]>d[u.first][u.second]+M[u.first][i]){
	  d[i][u.second]=d[u.first][u.second]+M[u.first][i];
	  q.push((state){d[i][u.second],i,u.second});
	}
	if(u.second+1<=c&&d[i][u.second+1]>d[u.first][u.second]+M[u.first][i]/2){
	  d[i][u.second+1]=d[u.first][u.second]+M[u.first][i]/2;
	  q.push((state){d[i][u.second+1],i,u.second+1});
	}
      }
    }
  }
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

#define NONE -1

int c,n,m,s,d;
vector < vector <int> > P;
vector < vector <int> > E;
vector <int> R;
vector <bool> V;
int answer;

void solve( int t )
{
	if ( t == d )
	{
		sort( R.rbegin(), R.rend() );
		for ( int i = 0; i < c; i++ )
		{
			R[i] /= 2;
		}
		if ( answer == -1 )
			answer = accumulate( R.begin(), R.end(), 0 );
		else
			answer = min( answer, accumulate( R.begin(), R.end(), 0 ) );
	}
	else
	{
		V[t] = true;
		for ( vector <int>::iterator it = E[t].begin(); it != E[t].end(); it++ )
		{
			if ( V[*it] == false )
			{
				R.push_back( P[t][*it] );
				solve(*it);
				R.pop_back();
			}
		}
		V[t] = false;
	}
}

int main( void )
{
	while ( cin >> c >> n >> m >> s >> d && c && n && m && s && d )
	{
		s--;
		d--;
		P = vector < vector <int> >(n+1, vector <int>(n+1,NONE));
		E = vector < vector <int> >(n+1);
		for ( int i = 0; i < m; i++ )
		{
			int a, b, f;
			cin >> a >> b >> f;
			P[a-1][b-1] = f;
			P[b-1][a-1] = f;
			E[a-1].push_back( b-1 );
			E[b-1].push_back( a-1 );
		}

		R = vector <int>();
		V = vector <bool>(n+1,false);
		answer = -1;
		solve( s );
		cout << answer << endl;
	}

	return 0;
}