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 <queue>
//#include <fstream>	// require freopen

using namespace std;

const int MAX_N = 100;
const int INF = 10000000;
const int MAX_C = 10;

// XÌîñ
// pair <cèÌø, XÌÔ>
typedef pair<int, int > T;

// n_Ìîñ
// pair <Å¬RXg,n_Ìîñ>
typedef pair<int, T > P;

// üÍ
int c, n, m, s, d;

// »ÌXÜÅÌÅ¬ÌRXg
// cost[cèÌø][XÌÔ] = ïp
int cost[MAX_C + 1][MAX_N];

// ùÉÅKlªèÏÝ©Ç¤©
bool flag[MAX_C + 1][MAX_N];

// HüÌîñ
// line[n_][I_] = ¿à
int line[MAX_N][MAX_N];

// DPpÌL[
typedef priority_queue <P, vector <P>, greater<P> > Q;
Q dp_q;

// _CNXg@
int dijkstra ()
{
	while (!dp_q.empty() ){
		P p = dp_q.top(); dp_q.pop();
		T &t = p.second;

		// ùÉÅKlªèÏÝ©²×é
		if (flag[t.first][t.second] ){
			continue;
		} // end if
		
		flag[t.first][t.second] = true;

		// ÅIn_
		if (t.second == d ){
			return p.first;
		} // end if

		for (int i = 0; i < n; ++i ){
			// øðgíÈ¢ê
			if (cost[t.first][i] > p.first + line[t.second][i] ){
				cost[t.first][i] = p.first + line[t.second][i];
				dp_q.push (P(p.first + line[t.second][i], T(t.first, i ) ) );
			} // end if

			// øðg¤ê
			if (t.first > 0 && cost[t.first - 1][i] > p.first + line[t.second][i]/2 ){
				cost[t.first - 1][i] = p.first + line[t.second][i]/2;
				dp_q.push (P(p.first + line[t.second][i]/2, T(t.first - 1, i ) ) );
			} // end if
		} // end for
	} // end while

	return INF;
}

void solve()
{
	// ú»
	for (int i = 0; i <= MAX_C; ++i ){
	 	for (int j = 0; j < MAX_N; ++j ){
			cost[i][j] = INF;
			flag[i][j] = false;
		} // end for
	} // end for

	// Jnn_
	dp_q = Q();
	dp_q.push (P(0, T(c, s ) ) );

	// _CNXg@
	int res = dijkstra();

	cout << res << endl;
}

int main()
{
//	cut here before submit 
//	freopen ("testcase.heb", "r", stdin );

	while (cin >> c >> n >> m >> s >> d && c && n && m && s && d){
		s--;d--;
		for (int i = 0; i < MAX_N; ++i ){
			for (int j = 0; j < MAX_N; ++j ){
				line[i][j] = INF;
			} // end for
		} // end for

		for (int i = 0; i < m; ++i ){
			int a, b, f;
			cin >> a >> b >> f;
			a--; b--;
			line[a][b] = f; line[b][a] = f;
		} // end for

		solve();
	} // end while

	return 0;
}

C++: #include<iostream>
#include<functional>
#include<queue>

#define INFTY 1<<30
#define N 101
#define C 11
#define CONNECTED 1
#define IN_QUEUE 1

using namespace std;

int cost[N][C];
bool vis[N][C];
int W[N][N];
int G[N][N];

class Node{
public:
	Node():id(0),cost(INFTY),c(0){}
	int id;
	int cost;
	int c;
	bool operator>(const Node &t)const{return this->cost > t.cost;}
};

void Dijkstra( Node s ){
	priority_queue< Node,vector<Node>,greater<Node> > pq;

	pq.push( s );

	while( !pq.empty() ){
		Node p = pq.top();

		pq.pop();

		if( vis[p.id][p.c] )
			continue;
		vis[p.id][p.c] = true;
		cost[p.id][p.c] = min( cost[p.id][p.c], p.cost );

		for(int i = 0; i < N; ++i){
			if( G[p.id][i] == CONNECTED ){
				Node np;

				np.id = i;
				np.cost = cost[p.id][p.c] + W[p.id][i];
				np.c = p.c;
				pq.push( np );
				if( p.c > 0 ){
					np.cost = cost[p.id][p.c] + (W[p.id][i]/2);
					np.c = p.c - 1;
					pq.push( np );
				}
			}
		}
	}
}
void ResetGraph(){
	for(int i = 0; i < N; ++i){
		for(int j = 0; j < C; ++j){
			cost[i][j] = INFTY;
			vis[i][j] = false;
		}
		for(int j = 0; j < N; ++j){
			G[i][j] = 0;
			W[i][j] = 0;
		}
	}
}
int main(){
	while( true ){
		int c, n, m, s, d;
		int ans = INFTY;
		Node start;
		
		cin >> c >> n >> m >> s >> d;
		if( !(c||n||m||s||d) )
			break;

		ResetGraph();
		for(int i = 0; i < m; ++i){
			int a, b, f;

			cin >> a >> b >> f;
			G[a][b] = G[b][a] = CONNECTED;
			W[a][b] = W[b][a] = f;
		}
		start.id = s;
		start.cost = 0;
		start.c = c;

		Dijkstra( start );

		for(int i = 0; i < C; ++i){
			ans = min(ans, cost[d][i]);
		}
		cout << ans << '\n';
	}
	return 0;
}

C++: #include<cstdio>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;

typedef pair<int,int> P;

int main(){
  int c,n,m,s,g,a,b,f;
  int cost[101][101];
  int d[11][101];

  while(1){
    scanf("%d %d %d %d %d",&c,&n,&m,&s,&g);
    if(!c && !n && !m && !s && !g)break;

    for(int i=0;i<=n;i++){
      for(int j=0;j<=n;j++){
	if(i-j)cost[i][j] = 1<<29;
	else cost[i][j] = 0;
      }
    }

    for(int i=0;i<m;i++){
      scanf("%d %d %d",&a,&b,&f);
      cost[a][b] = f;
      cost[b][a] = f;
    }

    priority_queue<P,vector<P>,greater<P> > q;
    fill(d[0],d[0]+n+1,1<<29);
    d[0][s] = 0;
    q.push(P(0,s));

    while(q.size()){
      P p=q.top();
      q.pop();
      int v = p.second;
      if(d[0][v] < p.first)continue;
      for(int i=1;i<=n;i++){
	if(d[0][i] > d[0][v] + cost[v][i]){
	  d[0][i] = d[0][v] + cost[v][i];
	  q.push(P(d[0][i],i));
	}
      }
    }

    for(int i=1;i<=c;i++){
      fill(d[i],d[i]+n+1,1<<29);
      d[i][s] = 0;
      q.push(P(0,s));

      while(q.size()){
	P p=q.top();
	q.pop();
	int v = p.second;
	if(d[i][v] < p.first)continue;
	for(int j=1;j<=n;j++){
	  if(d[i][j] > d[i][v] + cost[v][j]){
	    d[i][j] = d[i][v] + cost[v][j];
	    q.push(P(d[i][j],j));
	  }
	  if(d[i][j] > d[i-1][v] + cost[v][j]/2){
	    d[i][j] = d[i-1][v] + cost[v][j]/2;
	    q.push(P(d[i][j],j));
	  }
	}
      }
    }

    printf("%d\n",d[c][g]);
  }
}