Go: package main

import (
	"container/heap"
	"fmt"
)

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]int)
		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}] = now.Amount
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				if memoAmount, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok || (ok && memoAmount > next.Amount) {
					memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}] = next.Amount
					heap.Push(&pq, next)
				}
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					if memoAmount, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok || (ok && memoAmount > next.Amount) {
						memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}] = next.Amount
						heap.Push(&pq, next)
					}
				}
			}
		}
		//runtime.GC()
	}
}

C++: // AOJ 0212 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0212)
#include <cstdio>
#include <algorithm>
#include <limits>
#include <queue>
#include <utility>
#include <vector>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)

constexpr int MAX_N = 100, INF = std::numeric_limits<int>::max()>>2;

using P = std::pair<int, std::pair<int, int>>;

struct edge
{
  int to, cost;

  edge( int to, int cost )
  : to(to), cost(cost)
  {}
};

int c, n, m, s, d;
std::vector<edge> G[MAX_N];
int dp[MAX_N][11];

int main()
{
  while( scanf( "%d%d%d%d%d", &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;
      scanf( "%d%d%d", &a, &b, &f );
      --a; --b;
      G[a].emplace_back( edge( b, f ) );
      G[b].emplace_back( edge( a, f ) );
    }

    std::fill( (int*)dp, (int*)(dp+n), INF );

    std::priority_queue<P, std::vector<P>, std::greater<P>> pque;
    pque.push( P( 0, std::make_pair( s, 0 ) ) );
    dp[s][0] = 0;
    while( !pque.empty() )
    {
      P p = pque.top(); pque.pop();
      int dist = p.first, v = p.second.first, ticket = p.second.second;

      if( dist > dp[v][ticket] )
        continue;

      for( auto e : G[v] )
      {
        if( dp[e.to][ticket] > dist+e.cost )
        {
          dp[e.to][ticket] = dist+e.cost;
          pque.push( P( dp[e.to][ticket], std::make_pair( e.to, ticket ) ) );
        }
        if( ticket < 10 && dp[e.to][ticket+1] > dist+e.cost/2 )
        {
          dp[e.to][ticket+1] = dist+e.cost/2;
          pque.push( P( dp[e.to][ticket+1], std::make_pair( e.to, ticket+1 ) ) );
        }
      }
    }

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

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <set>

using namespace std;

#define INF 100000000

typedef pair<int, int> P;

int main() {
	int c, n, m, s, d;
	while (cin >> c >> n >> m >> s >> d) {
		if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) {
			break;
		}
		vector< vector<int> > ff(n+1, vector<int>(n+1, INF));
		vector< vector<int> > ddd(101, vector<int>(101, INF));
		int a, b, f;
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> f;
			ff[a][b] = ff[b][a] = f;
			ddd[a][b] = ddd[b][a] = f;
		}
		for (int i = 1; i <= n; i++) {
			ddd[i][i] = 0;
		}
		for (int k = 1; k <= n; k++) {
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= n; j++) {
					ddd[i][j] = min(ddd[i][j], ddd[i][k]+ddd[k][j]);
				}
			}
		}
		vector< vector< vector<int> > > dp(101, vector< vector<int> >(101, vector<int>(11, INF)));
		for (int i = 1; i < 101; i++) {
			dp[i][i][0] = 0;
			dp[s][i][0] = ddd[s][i];
			dp[i][d][0] = ddd[i][d];
		}
		/*for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				cout << ddd[i][j] << ' ';
			}
			cout << endl;
		}
		cout << endl;*/
		set<P> st[101][101][11];
		for (int k = 1; k <= c; k++) {
			for (int i = 1; i <= n; i++) {
				dp[s][i][k] = dp[s][i][k-1];
				dp[i][d][k] = dp[i][d][k-1];
				st[s][i][k] = st[s][i][k-1];
				st[i][d][k] = st[i][d][k-1];
				int ka, kb, kc, kd;
				ka = kb = kc = kd = 0;
				for (int j = 1; j <= n; j++) {
					for (int p = 1; p <= n; p++) {
						if (ff[j][p] != INF && st[s][i][k].find(P(j, p)) == st[s][i][k].end()) { //dd[s][i][k][j][p]
							if (dp[s][i][k] > dp[s][j][k-1]+(ff[j][p]/2)+ddd[p][i]) {
								dp[s][i][k] = dp[s][j][k-1]+(ff[j][p]/2)+ddd[p][i];
								ka = j;
								kb = p;
							}
						}
						if (ff[j][p] != INF && st[i][d][k].find(P(j, p)) == st[i][d][k].end()) { //dd[i][D][k][j][p]
							if (dp[i][d][k] > dp[i][j][k-1]+(ff[j][p]/2)+ddd[p][d]) {
								dp[i][d][k] = dp[i][j][k-1]+(ff[j][p]/2)+ddd[p][d];
								kc = j;
								kc = p;
							}
						}
					}
				}
				st[s][i][k].insert(P(ka, kb));
				st[i][d][k].insert(P(kc, kd));
				//dd[s][i][k][a][b] = dd[i][D][k][C][dk] = 1;
			}
			/*for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= n; j++) {
					cout << dp[i][j][k] << ' ';
				}
				cout << endl;
			}
			cout << endl;*/
		}
		cout << dp[s][d][c] << endl;
	}
	return 0;
}

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

using namespace std;

struct P {
	int from, cost, coupon;

	bool operator >(const P& p) const {
		return cost > p.cost;
	}
};

int ecost[101][101];
int ccost[101][11];
int c, n, m, s, d;
int i;

int main() {
	while(cin >> c >> n >> m >> s >> d, c|n|m|s|d) {
		fill_n((int *)ecost, 101*101, -1);
		fill_n((int *)ccost, 101*11, 1e8);
		fill_n((int *)ccost, 11, 0);
		for(i=0; i<m; i++){
			int a, b, f;
			cin >> a >> b >> f;
			ecost[a][b] = f;
			ecost[b][a] = f;
		}
		priority_queue<P, vector<P>, greater<P>> q;
		q.push((P){s, 0, 0});
		int dcost;
		while(!q.empty()){
			P p = q.top(); q.pop();
			// cout << "pos=" << p.from << endl;
			// goal
			if(p.from == d) {
				dcost = p.cost;
				break;
			}
			//
			for(i=0; i<n+1; i++){ // i:to
				if(ecost[p.from][i] != -1){
					if(p.cost + ecost[p.from][i] < ccost[i][p.coupon]){
						q.push((P){i, p.cost + ecost[p.from][i], p.coupon});
					}
					if(p.coupon+1 <= c) {
						if(p.cost + ecost[p.from][i]/2 < ccost[i][p.coupon+1]){
							q.push((P){i, p.cost + ecost[p.from][i]/2, p.coupon+1});
						}
					}
				}
			}
		}
		cout << dcost << endl;
	}
	return 0;
}