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++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define F first
#define S second


int c,n,m,s,d;
typedef struct{
  int to,cost;
}edge;

vector<edge> V[11*100];


int dijkstra(int from,int to){
  from=(c+1)*from+c;
  bool used[n*(c+1)];
  memset(used,0,sizeof(used));
  int len[n*(c+1)];
  rep(i,n*(c+1))len[i]=1<<23;
  len[from]=0;

  priority_queue<PI,vector<PI>,greater<PI> > Q;
  Q.push(mp(0,from));
  while(!Q.empty()){
    int cost=Q.top().F,tfrom=Q.top().S;Q.pop();
    used[tfrom]=true;
    rep(i,V[tfrom].size()){
      int tto=V[tfrom][i].to,tcost=V[tfrom][i].cost;
      if(used[tto])continue;
      len[tto]=min(len[tfrom]+tcost,len[tto]);
      Q.push(mp(len[tto],tto));
    }
  }
/*
  rep(i,n){
    rep(j,c+1)printf("%6d",len[i*(c+1)+j]);
    cout<<endl;
  }
*/
  int ans=len[to*(c+1)];
  rep(i,c+1)ans=min(ans,len[to*(c+1)+i]);
  return ans;
}

main(){
  while(cin>>c>>n>>m>>s>>d,c|n|m|s|d){
    --s,--d;
    rep(i,n*(c+1))V[i].clear();

    rep(j,m){
      int a,b,cost;
      cin>>a>>b>>cost;
      --a,--b;
      rep(i,c+1){
        V[a*(c+1)+i].pb((edge){b*(c+1)+i,cost});
        V[b*(c+1)+i].pb((edge){a*(c+1)+i,cost});
        if(i){
          V[a*(c+1)+i].pb((edge){b*(c+1)+i-1,cost/2});
          V[b*(c+1)+i].pb((edge){a*(c+1)+i-1,cost/2});
        }
      }
    }
/*
    rep(i,n*(c+1)){
      printf("%d ",i/(c+1)+1);
      rep(j,V[i].size()){
        printf("%d %6d ",V[i][j].to/(c+1)+1,V[i][j].cost);
      }
      cout<<endl;
    }
*/
    cout<<dijkstra(s,d)<<endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#define INF (1e9)
#define N 101
#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++: //Solution for aoj:0212 Highway Express Bus
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int, int>P;


struct S {
	int cost, to, num, jud;
	S(int c, int t, int n, int j) : cost(c), to(t), num(n), jud(j) {}
	bool operator<(const S &s) const {
		return cost < s.cost;
	}
};

const int INF = 1 << 31;
int c, n, m, s, d, w;
int tocost[101][11];
vector<P> town[101];
priority_queue<S, vector<S>> pq;

void pq_set(int p, int sum, int j){
	for (int i = 0; i < town[p].size(); i++){
			pq.push(S(town[p][i].second + sum, town[p][i].first, j, 0));
			if (j)
				pq.push(S(town[p][i].second / 2 + sum, town[p][i].first, j - 1, 1));
	}
}

void dijkstra(){
	while (!pq.empty()){
		S a = pq.top();
		pq.pop();
		if (a.jud){
			if (tocost[a.to][a.num] < a.cost&&a.to!=s){
				tocost[a.to][a.num] = a.cost;
					pq_set(a.to, tocost[a.to][a.num], a.num);

			}
		}
			else{
				if (tocost[a.to][a.num] < a.cost&&a.to!=s){
					tocost[a.to][a.num] = a.cost;
					pq_set(a.to, tocost[a.to][a.num], a.num);
					
				}
			}
	}
}
	int main(){
		while (cin >> c >> n >> m >> s >> d&&c){
			for (int i = 0; i < 101; i++)
				fill(tocost[i], tocost[i] + 11, INF);
			fill(tocost[s], tocost[s] + 11, 0);
			while (pq.size())
				pq.pop();
			for (int i = 0; i < n; i++)
					town[i].erase(town[i].begin(),town[i].end());
			for (int i = 0; i < m; i++){
				int a, b, c;
				cin >> a >> b >> c;
				town[a].push_back({ b, c*-1 });
				town[b].push_back({ a, c*-1 });
			}
			pq_set(s, 0, c);
			dijkstra();
			cout << -tocost[d][0] << endl;
		}
	}