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 <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, 0, -1, 0};
int dy[]={0, -1, 0, 1};

int dijkstra(int s, int g, int ticketNum, vector<vector<P>> &G){
    vector<vector<int>> dist(110, vector<int>(15, 1e9));
    priority_queue<T, vector<T>, greater<T>> que;
    dist[s][0] = 0;
    que.push(T(0, s, 0));

    while(!que.empty()){
        int ccost, cv, cticket;
        tie(ccost, cv, cticket) = que.top();
        que.pop();

        if(dist[cv][cticket] < ccost) continue;

        for(auto x : G[cv]){
            int nv = x.first;
            int ncost = x.second;
            if(dist[cv][cticket] + ncost < dist[nv][cticket]){
                dist[nv][cticket] = dist[cv][cticket] + ncost;
                que.push(T(dist[nv][cticket], nv, cticket));
            }
            if(cticket < ticketNum && dist[cv][cticket] + ncost / 2 < dist[nv][cticket+1]){
                dist[nv][cticket+1] = dist[cv][cticket] + ncost / 2;
                que.push(T(dist[nv][cticket+1], nv, cticket+1));
            }
        }
    }

    int ans=1e9;
    for(int i=0; i<=ticketNum; i++){
        chmin(ans, dist[g][i]);
    }

    return ans;
}

int main(){
    while(1){
        int c, n, m, s, d; cin>>c>>n>>m>>s>>d;
        s--, d--;
        if(c == 0) return 0;

        vector<vector<P>> G(110);
        for(int i=0; i<m; i++){
            int a, b, f; cin>>a>>b>>f;
            a--, b--;
            G[a].eb(P(b, f));
            G[b].eb(P(a, f));
        }

        cout << dijkstra(s, d, c, G) << endl;
    }
}

C++: /*
g£_CNXg
*/
#include<iostream>
#include<queue>
#include<map>
#define INF 100000000
#include<vector>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int> p;
typedef pair<int,p> P;
//P(cost,p(node,ticket))
struct edge{
	int cost,to;
};
int nticket,nnode,nedge,start,goal,d[11][101];
vector<edge> v[101];
void dijkstra(){
	int nowticket,nowplace,nowsum;
	priority_queue<P,vector<P>,greater<P> > que;
	que.push(P(0,p(start,nticket)));
	while(!que.empty()){
		P now=que.top();
		que.pop();
		
		nowsum=now.first;
		nowplace=now.second.first;
		nowticket=now.second.second;
		
		if(d[nowticket][nowplace]<nowsum)
			continue;
		rep(i,(int)v[nowplace].size()){
			edge e=v[nowplace][i];
			if(nowticket>0){
				if(d[nowticket-1][e.to]>e.cost/2+nowsum){
				d[nowticket-1][e.to]=e.cost/2+nowsum;
				que.push(P(d[nowticket-1][e.to],p(e.to,nowticket-1)));
				}
			}
			if(d[nowticket][e.to]>e.cost+nowsum){
				d[nowticket][e.to]=e.cost+nowsum;
				que.push(P(d[nowticket][e.to],p(e.to,nowticket)));
			}
		}
	}
}
void input(){
	int a,b,f;
	rep(i,nedge){
			cin>>a>>b>>f;
			a--;b--;
			edge c;
			c.cost=f;
			c.to=b;v[a].push_back(c);
			c.to=a;v[b].push_back(c);
		}
}
void init(){
	rep(i,nticket+1)
		rep(j,nnode){
			if(j==start)
			d[i][j]=0;
			else
			d[i][j]=INF;
	}
}
int main(){
	//d[ticket][node]=Å­ïp
	while(cin>>nticket>>nnode>>nedge>>start>>goal,nticket||nnode||nedge||start||goal){
		start--;goal--;
		//ticket:`Pbg
		//nnode:nodeÌ
		//nedge:edgeÌ
		//start¨goal
		input();
		init();
		dijkstra();
		int ans=INF;
		for(int i=0;i<nticket+1;i++)
		if(ans>d[i][goal])
		ans=d[i][goal];
		cout<<ans<<endl;
		for(int i=0;i<nedge;i++)
		v[i].clear();
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;

class State{
public:
  int v, c, t;
  State(int _v, int  _c, int _t)
    :v(_v), c(_c), t(_t) {}
  bool operator < (const State& st) const{
    return this->c > st.c;
  }
};


int main(){

  const int INF = 1 << 27;
 
  int ticket, n, m, start, goal;
  while(cin >> ticket >> n >> m >> start >> goal){
    if((ticket|n|m|start|goal) == 0) break;
    --start;
    --goal;

    int s, dd, score;
    vector<vector<int> >cost(n, vector<int>(n, 2*INF));
    for(int i=0; i<m; i++){
      cin >> s >> dd >> score;
      --s;
      --dd;
      cost[s][dd] = cost[dd][s] = score;      
      //cout << cost[s][dd][0] << endl;
    }

    priority_queue<State> req;
    vector<vector<int> > d(n, vector<int>(ticket+1, INF));
    for(int i=0; i<=ticket; i++){
      d[start][i] = 0;
 
    }
    req.push(State(start, 0, ticket));

    while(! req.empty()){
      State st = req.top();
      req.pop();

      //cout << "v:" << st.v+1 <<" c:" << st.c << " t:" << st.t << endl;

      if(st.v == goal){
	   cout << st.c << endl;
	   break;
      }

      for(int i=0; i<n; i++){
	//use ticket
	if(st.t>0 && d[st.v][st.t] + cost[st.v][i]/2 < d[i][st.t-1]){
	  d[i][st.t-1] = st.c + cost[st.v][i]/2;
	  req.push(State(i, d[i][st.t-1], st.t-1));
	}
	//not use ticket
	if(d[st.v][st.t] + cost[st.v][i] < d[i][st.t]){
	  d[i][st.t] = st.c + cost[st.v][i];
	  req.push(State(i, d[i][st.t], st.t));
	}
	
      }
	
      
    }
  }
  return 0;
}