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<vector>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;// F -> to, S -> cost
typedef pair<P,int> PP;// F->P(to,cost), S->discount
int c,n,m,s,d;
vector<PP> vec[600];
int MD[600][13];
const int INF = 1<<29;

void dijkstra(){
  priority_queue<PP,vector<PP>,greater<PP> > que;
  rep(i,600)rep(j,13)MD[i][j] = INF;
  //syokika
  MD[s][c] = 0;
  que.push(PP(P(0,s),c));//dis,start,discount

  while(!que.empty() ){
    PP p = que.top();que.pop(); //priority_queのPPと
    int v = p.F.S;
    
    for(int i=0;i<vec[v].size();i++){
      PP po = vec[v][i]; //vectorのPPは意味が違うので注意！！！
      //if(MD[v][po.S] < po.F.S)continue;
      if(MD[po.F.F][c] > MD[v][c] + po.F.S){
	MD[po.F.F][c] = MD[v][c]+po.F.S;
	que.push(PP(P(MD[po.F.F][c],po.F.F),c));
      }
      if(po.S > 0 && MD[po.F.F][p.S-1] > MD[v][p.S] + po.F.S/2 ){
	MD[po.F.F][p.S-1] = MD[v][p.S]+po.F.S/2;
	que.push(PP(P(MD[po.F.F][p.S-1],po.F.F),p.S-1));
      }

    }


  }



}


int main(){
  int x,y,co;
  while(true){
    cin >> c >> n >> m >> s >> d;
    if(c+n+m+s+d == 0)break;
    rep(i,600)vec[i].clear();
    rep(i,m){
      cin >> x >> y >> co;
      vec[x].push_back(PP(P(y,co),c));
      vec[y].push_back(PP(P(x,co),c));
    }
    dijkstra();
    int ans = INF;
    for(int i=0;i<=c;i++){
      ans = min(ans,MD[d][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };



int c, n, m, s, d;
int dp[ 11 ][ 101 ];
static int _root[ 100 ][ 100 ];

uint solve( int now, int can_use, set<int> p ) {
	p.insert( now );
	if ( now == d ) {
		return 0;
	}
	if ( dp[ can_use ][ now ] >= 0 ) {
		return dp[ can_use ][ now ];
	}
	uint res = 1 << 30;
	for ( int i = 0; i < n; i++ ) {
		if ( p.count( i ) || _root[ now ][ i ] == INF ) {
			continue;
		}
		res = min( res, solve( i, can_use, p ) + _root[ now ][ i ] );
		if ( can_use ) {
			res = min( res, solve( i, can_use - 1, p ) + _root[ now ][ i ] / 2 );
		}
	}
	return dp[ now ][ can_use ] = res;
}

int main() {
	while ( ~scanf("%d %d %d %d %d", &c, &n, &m, &s, &d) && n && m ) {
		--s;
		--d;
		for ( int i = 0; i < n; i++ ) {
			for ( int j = 0; j < n; j++ ) {
				_root[ i ][ j ] = INF;
			}
			_root[ i ][ i ] = 0;
		}
		for ( int i = 0; i < m; i++ ) {
			int a, b, f;
			scanf( "%d %d %d", &a, &b, &f );
			--a;
			--b;
			_root[ a ][ b ] = _root[ b ][ a ] = f;
		}
		NCLR( dp );
		uint ans = solve( s, c, set<int>() );
		printf( "%d\n", ans );
	}
	return 0;
}

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

#define INF 1e9

using namespace std;

struct Edge {
	int to, cost;
};
struct Node {
	int now, rest, cost;
};
bool operator <(const Node& n1, const Node& n2) {
	return n1.cost < n2.cost;
}

int main() {
	int c, n, l, s, d;
	while ( cin >> c >> n >> l >> s >> d, c || n || l || s || d ) {
		--s, --d;

		vector<Edge> m[100];
		for (int i = 0; i < l; ++i) {
			int a, b, f; cin >> a >> b >> f; --a, --b;
			m[a].push_back( (Edge){ b, f } );
			m[b].push_back( (Edge){ a, f } );
		}

		int ans[100][11]; fill(&ans[0][0], &ans[0][0]+100*11, INF);
		priority_queue<Node> Q; Q.push( (Node){s, c, 0} );
		while ( !Q.empty() ) {
			Node node = Q.top(); Q.pop();
			if (node.cost >= ans[node.now][node.rest]) continue;
			ans[node.now][node.rest] = node.cost;

			for (int i = 0; i < m[node.now].size(); ++i) {
				Edge& e = m[node.now][i];
				if (node.cost+e.cost < ans[e.to][node.rest]) {
					Q.push( (Node){ e.to, node.rest, node.cost+e.cost } );
				}

				if (node.rest > 0) {
					if (node.cost+e.cost/2 < ans[e.to][node.rest-1]) {
						Q.push( (Node){ e.to, node.rest-1, node.cost+e.cost/2 } );
					}
				}
			}
		}
		int a = INF;
		for (int i = 0; i <= c; ++i) a = min(a, ans[d][i]);
		cout << a << endl;
	}
}