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)
		}
		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)
		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)
					}
				}
			}
		}
	}
}

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
//typedef pair<int,int> P;
//typedef pair<int,P> PP;

//-----------------------------------------------------------------------------

#define INIT INF
typedef int Weight;
typedef pair<Weight,int> P;

typedef pair<P,int> PP;

int c,n,m,s,g;

struct Edge {
	int to;
	Weight cost;
	Edge(int to,Weight cost):to(to),cost(cost) {}
};

typedef vector< vector<Edge> > Graph;

bool operator<(const Edge &e,const Edge &f) {
	return e.cost!=f.cost?e.cost>f.cost:e.to<f.to;
}

void dijkstra(const Graph &G,int s,vector< vector<Weight> > &dist) {
	int n=(int)G.size();
	dist.assign(n,vector<Weight>(c+1,INIT));
	dist[s][0]=0;
	priority_queue< PP,vector<PP>,greater<PP> > Q;
	Q.push(PP(P(0,s),0));
	while(!Q.empty()) {
		PP p=Q.top();Q.pop();
		int v=p.first.second,times=p.second;
		if(dist[v][times]<p.first.first) continue;
		for(auto e:G[v]) {
			if(dist[e.to][times]>dist[v][times]+e.cost) {
				dist[e.to][times]=dist[v][times]+e.cost;
				Q.push(PP(P(dist[e.to][times],e.to),times));
			}
			if(times+1<=c&&dist[e.to][times+1]>dist[v][times]+e.cost/2) {
				dist[e.to][times+1]=dist[v][times]+e.cost/2;
				Q.push(PP(P(dist[e.to][times+1],e.to),times+1));
			}
		}
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(true) {
	cin>>c>>n>>m>>s>>g;
	if(c==0&&n==0&&m==0&&s==0&&g==0) break;
	s--,g--;
	Graph G(n);
	vector< vector<Weight> > dist(n,vector<Weight>(c+1));
	REP(i,m) {
		int a,b;Weight c;
		cin>>a>>b>>c;
		a--,b--;
		G[a].emplace_back(b,c);
		G[b].emplace_back(a,c);
	}
	dijkstra(G,s,dist);
	Weight ans=INIT;
	REP(i,c+1) ans=min(ans,dist[g][i]);
	cout<<ans<<endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define llong long long 
typedef pair<int, int> P;

typedef struct data{
	int now;
	int time;
	int dep;

	bool operator > (const data& right) const{
		return dep == right.dep ? time > right.time : dep > right.dep;
	}

}data;

int c, n, m, s, d;
vector<P> edges[101];
int cost[101][11];
priority_queue<data, vector<data>, greater<data> > q;

void solve(){
	data D;
	D.now = s;
	D.time = 0;
	D.dep = 0;
	q.push(D);

	while(!q.empty()){
		data nd = q.top(); q.pop();
		int nowp = nd.now;
		int nowtime = nd.time;
		int nowdep = nd.dep;

		if(cost[nowp][nowdep] < nowtime) continue;

		for(int i = 0; i < edges[nowp].size(); i++){
			P edge = edges[nowp][i];
			if(cost[edge.first][nowdep] > cost[nowp][nowdep] + edge.second){
				data in;
				in.now = edge.first;
				in.time = cost[nowp][nowdep] + edge.second;
				in.dep = nd.dep;
				cost[in.now][in.dep] = in.time;
				q.push(in);
			}

			if(nowdep < c){
				if(cost[edge.first][nowdep + 1] > cost[nowp][nowdep] + edge.second / 2){
					data in;
					in.now = edge.first;
					in.time = nowtime + (edge.second / 2);
					in.dep = nowdep + 1;
					cost[in.now][in.dep] = in.time;
					q.push(in);
				}			
			}

		}
	}

	return;
}

int main(){
	while(cin >> c >> n >> m >> s >> d, n){
		fill_n(*cost, 101 * 11, 99999999);
		for(int i = 0; i <= c; i++){
			cost[s][i] = 0;
		}
		for(int i = 0; i < m; i++){
			int n, to, cos;
			cin >> n >> to >> cos;
			edges[n].push_back(P(to, cos));
			edges[to].push_back(P(n, cos));
		}

		solve();

		cout << cost[d][c] << endl;
		// for(int i = 1; i <= n; i++){
		// 	cout << cost[i][2] << endl;
		// }

		for(int i = 0; i < 101; i++){
			edges[i].clear();
		}
	}

	return 0;
}

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

using namespace std;

int c,n,m,s,d;
vector<pair<int,int> >  v[101];
int dp[101][11];

int inf = 1111111111;

typedef pair<pair<int,int>,int> p;

void dijkstra(int s,int ticket){
  priority_queue<p,vector<p>,greater<p> > que;
  
  fill((int *)dp,(int *)dp+101*11,inf);
  dp[s][0] = 0;
  que.push(make_pair(make_pair(0,s),0));

  while(!que.empty()){
    p x = que.top();
    que.pop();
    int V = (x.first).second;
    if(dp[V][x.second] < (x.first).first)
      continue;
    for(int i = 0;i<v[V].size();i++){
      pair<int,int> tmp = v[V][i];
      if(dp[tmp.first][x.second] > dp[V][x.second] + tmp.second){
	dp[tmp.first][x.second] = dp[V][x.second] + tmp.second;
	que.push(make_pair(make_pair(dp[tmp.first][x.second],tmp.first),x.second));
      }

      if(x.second < ticket && dp[tmp.first][x.second+1] > dp[V][x.second] + tmp.second/2) {
	//cout<<"a"<<endl;
	dp[tmp.first][x.second+1] = dp[V][x.second] + tmp.second/2;
	que.push(make_pair(make_pair(dp[tmp.first][x.second+1],tmp.first),x.second+1));
      }		

    }

  }
}


int main(){

  while(cin>>c>>n>>m>>s>>d){
    if(c==0&&n==0&&m==0&&s==0&&d==0)
      break;
  
    for(int i = 0;i<101;i++){
      v[i].clear();
    }
    s--;
    d--;
    for(int i = 0;i<m;i++){
      int a,b,f;
      cin>>a>>b>>f;
      a--;b--;
      v[a].push_back(make_pair(b,f));
      v[b].push_back(make_pair(a,f));
    }
    dijkstra(s,c);
    int ans = inf;
    for(int i = 0;i<=c;i++){
      ans = min(ans,dp[d][i]);
    }
    
    /*
    for(int i = 0;i<=c;i++){
      for(int j = 0;j< n;j++){
	cout<<dp[j][i]<<" ";
      }
      cout<<endl;
    }
    */
    cout<<ans<<endl;
  }

  return 0;
}