Go: package main

import (
	"container/heap"
	"fmt"
	"runtime"
)

type Node struct {
	NowPosition          int
	DiscountTicketTCount int
	Amount               int
}

type PriorityQueueNode []*Node

func (pq PriorityQueueNode) Len() int {
	return len(pq)
}

func (pq PriorityQueueNode) Less(i, j int) bool {
	return pq[i].Amount < pq[j].Amount
}

func (pq PriorityQueueNode) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *PriorityQueueNode) Push(node interface{}) {
	*pq = append(*pq, node.(*Node))
}

func (pq *PriorityQueueNode) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	*pq = old[0 : n-1]
	return item
}

type Point struct {
	NextPoint int
	Fee       int
}

type Memo struct {
	NowPoint             int
	DiscountTicketTCount int
}

func main() {
	var c, n, m, s, d int
	var a, b, f int

	for {
		fmt.Scan(&c, &n, &m, &s, &d)
		if c == 0 && n == 0 && m == 0 && s == 0 && d == 0 {
			break
		}
		s--
		d--
		pointMap := make([][]Point, n)
		for i := 0; i < n; i++ {
			pointMap[i] = make([]Point, 0, n)
		}
		for i := 0; i < m; i++ {
			fmt.Scan(&a, &b, &f)
			a--
			b--
			pointMap[a] = append(pointMap[a], Point{NextPoint: b, Fee: f})
			pointMap[b] = append(pointMap[b], Point{NextPoint: a, Fee: f})
		}
		pq := make(PriorityQueueNode, 0, m)
		start := Node{NowPosition: s, DiscountTicketTCount: c, Amount: 0}
		heap.Push(&pq, &start)
		memo := make(map[Memo]struct{})
		for len(pq) != 0 {
			now := heap.Pop(&pq).(*Node)
			if now.NowPosition == d {
				fmt.Println(now.Amount)
				break
			}
			memo[Memo{NowPoint: now.NowPosition, DiscountTicketTCount: now.DiscountTicketTCount}] = struct{}{}
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				if _, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok {
					heap.Push(&pq, next)
				}
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					if _, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok {
						heap.Push(&pq, next)
					}
				}
			}
		}
		runtime.GC()
	}
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int c,n,m,s,d;

int main(){
    while(true){
	scanf("%d%d%d%d%d",&c,&n,&m,&s,&d);
	if(c==0||n==0||m==0||s==0||d==0) break;
	int rosen[101][101] = {10000000};
	int dp[11][101];
	REP(i,101)REP(j,101) rosen[i][j] = 10000000;
	REP(i,11)REP(j,101) dp[i][j] = 10000000;
	REP(i,m){
	    int a,b,f;
	    cin >> a >> b >> f;
	    rosen[a][b] = f;
	    rosen[b][a] = f;
	}
	int ans = 10000000;
	vector<bool>  used(n+1,false);
	queue<int> q;
	queue<int> th;
	dp[c][s] = 0;
	q.push(s);
	th.push(c);
	while(!q.empty()){
	    int p = q.front();
	    int t = th.front();
	    q.pop();
	    th.pop();
	    FOR(i,1,n+1){
		if(dp[t][i] > dp[t][p] + rosen[p][i]){
		    dp[t][i] = dp[t][p] + rosen[p][i];
		    q.push(i);
		    th.push(t);
		}
		if(t>0&&dp[t-1][i] > dp[t][p] + rosen[p][i]/2){
		    dp[t-1][i] = dp[t][p] + rosen[p][i]/2;
		    q.push(i);
		    th.push(t-1);
		}
	    }
	}
	REP(i,c+1){
	    if(dp[i][d] > 0)
		ans = min(ans,dp[i][d]);
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
#include <numeric>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

struct edge { int to, cost; };
struct state {
  int cur, card, cost;
  bool operator<(const state& lhs) const {
    return cost > lhs.cost;
  }
};

typedef pair<int,int> P;

const int kInf = 1 << 28;
int C, N, M, S, D;
vector<edge> es[102];
int d[102][12];

int solve() {
  // init
  for (int i = 0; i < 102; ++i)
    for (int j = 0; j < 12; ++j)
      d[i][j] = kInf;
  // dijkstra
  priority_queue<state> Q;
  d[S][C] = 0;
  Q.push((state){S, C, 0});
  while (!Q.empty()) {
    state s = Q.top(); Q.pop();
    if (d[s.cur][s.card] < s.cost)
      continue;
    rep(i,es[s.cur].size()) {
      const edge& e = es[s.cur][i];
      // without ticket
      if (d[e.to][s.card] > s.cost + e.cost) {
        d[e.to][s.card] = s.cost + e.cost;
        Q.push((state){e.to, s.card, s.cost + e.cost});
      }
      // use ticket
      if (s.card > 0 &&
          d[e.to][s.card - 1] > s.cost + (e.cost / 2)) {
        d[e.to][s.card - 1] = s.cost + (e.cost / 2);
        Q.push((state){e.to, s.card - 1, s.cost + (e.cost / 2)});
      }
    }
  }
  return *min_element(d[D], d[D] + 12);
}

int main() {
  while (scanf("%d%d%d%d%d", &C, &N, &M, &S, &D), C) {
    --S; --D;
    rep(i,N) {
      es[i].clear();
    }
    rep(i,M) {
      int a, b, f;
      scanf("%d%d%d", &a, &b, &f);
      --a; --b;
      es[a].push_back((edge){b, f});
      es[b].push_back((edge){a, f});
    }
    printf("%d\n", solve());
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define pb            push_back
template<class T, class S> inline pair<T, S> mp(const T x, const S y){ return make_pair(x, y); }
template<class T, class S> inline bool minup(T& m, const S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, const S x){ return m<(T)x ? (m=(T)x, true) : false; }

static const int    INF = 1000000000;
static const ll     MOD = 1000000007LL;
static const double EPS = 1E-10;

const int MAX_N = 111;
const int MAX_C = 22;
int c, n, m, s, d;
int t, u, f;
struct Edge{int from, to, cost;};
vector<Edge> g[MAX_N];
int l[MAX_N][MAX_C];

int main()
{
  while(cin >> c >> n >> m >> s >> d, c){
    rep(i, n) g[i].clear();
    rep(i, MAX_N) rep(j, MAX_C) l[i][j] = INF;

    rep(i, m){
      cin >> t >> u >> f;
      g[t - 1].pb((Edge){t - 1, u - 1, f});
      g[u - 1].pb((Edge){u - 1, t - 1, f});
    }

    priority_queue<vi, vector<vi>, greater<vi> > que;
    vi tmp(3);
    tmp[0] = 0;      // ?????¢
    tmp[1] = s - 1;  // ?????°
    tmp[2] = c;      // ??´???
    l[s - 1][c] = 0;
    que.push(tmp);
    while(!que.empty()){
      vi p = que.top();  que.pop();
      int ti = p[2];
      int v = p[1];
      if(l[v][ti] < p[0]) continue;

      vrep(w, g[v]){
        Edge& e = *w;
        if(l[e.to][ti] > l[v][ti] + e.cost){
          l[e.to][ti] = l[v][ti] + e.cost;
          tmp[0] = l[e.to][ti];
          tmp[2] = ti;
          tmp[1] = e.to;
          que.push(tmp);
        }
      }

      if(ti > 0) vrep(w, g[v]){
          Edge& e = *w;
          if(l[e.to][ti - 1] > l[v][ti] + e.cost / 2){
            l[e.to][ti - 1] = l[v][ti] + e.cost / 2;
            tmp[0] = l[e.to][ti - 1];
            tmp[2] = ti - 1;
            tmp[1] = e.to;
            que.push(tmp);
          }
        }
    }

    cout << l[d - 1][0] << endl;
  }

  return 0;
}