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<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>

#define INF 1010000000
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)
#define sint(i) scanf("%d",&i);
#define sintt(i,j) scanf("%d%d",&i,&j);
#define sinttt(i,j,k) scanf("%d%d%d",&i,&j,&k);

using namespace std;

class S{
	public:
	int p;
	int v;
	int n;
	
	S(int p,int v,int n):p(p),v(v),n(n){}
	
	bool operator<(const S&a)const{
		return v>a.v;
		return v==a.v ? v>a.v : n <a.n;
	}
};

int main(){
	while(1){
		int c,n,m,s,g;
		scanf("%d%d%d%d%d",&c,&n,&m,&s,&g);
		if(c==0&&n==0)break;
		
		int root[101][101];
		rep(i,101)rep(j,101)root[i][j]=INF;
		
		rep(i,m){
			int a,b,d;
			sinttt(a,b,d);
			root[a][b]=d;
			root[b][a]=d;
		}
		
		priority_queue<S> qui;
		qui.push(S(s,0,0));
		int ans=0;
		
		int visit[101][11];
		rep(i,101)rep(j,11)visit[i][j]=0;
		
		while(1){
			if(qui.empty()==1){
				ans=-1;
				break;
			}
			
			S u=qui.top();qui.pop();
			
			if(u.p==g){
				ans=u.v;
				break;
			}
			
			printf("%d %d %d\n",u.p,u.v,u.n);
			visit[u.p][u.n]=1;
			
			reps(i,n){
				if(root[u.p][i]!=INF){
					if(visit[i][u.n]==0){
						qui.push(S(i,u.v+root[u.p][i],u.n));
					}
					if(visit[i][u.n+1]==0 && visit[u.p][u.n+1]==0 && u.n+1 <= c){
						qui.push(S(i,u.v+root[u.p][i]/2,u.n+1));
					}
				}
			}
		}
		printf("%d\n",ans);
	}
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
double EPS=1e-10;
int INF=1e9;
static const int MEM_SIZE=1000000;
struct edge{int to,cost;};
struct dat{
      int s,cost,num;
      dat(int s,int cost,int num):s(s),cost(cost),num(num){}
      bool operator<(const dat& a)const{return cost<a.cost;}
};
vector<edge> G[100];
int dist[100][11];
int n,c;
void dijkstra(int s){
      REP(i,100)REP(j,11)dist[i][j]=INF;
      priority_queue<dat>Q;
      Q.push(dat(s,0,0));
      dist[s][0]=0;
      while(!Q.empty()){
            dat p=Q.top();Q.pop();
            REP(i,G[p.s].size()){
                  edge e=G[p.s][i];
                  if(p.num<c){
                        if(dist[p.s][p.num]+e.cost/2<dist[e.to][p.num+1]){
                              dist[e.to][p.num+1]=dist[p.s][p.num]+e.cost/2;
                              Q.push(dat(e.to,dist[e.to][p.num+1],p.num+1));
                        }
                  }
                  if(dist[p.s][p.num]+e.cost<dist[e.to][p.num]){
                        dist[e.to][p.num]=dist[p.s][p.num]+e.cost;
                        Q.push(dat(e.to,dist[e.to][p.num],p.num));
                  }
            }
      }
}
int main(){
      int m,s,d;
      while(cin>>c>>n>>m>>s>>d&&(c||n||m||s||d)){
            s--;d--;
            REP(i,100)G[i].clear();
            REP(i,m){
                  int a,b,f;
                  cin>>a>>b>>f;
                  a--;b--;
                  G[a].PB((edge){b,f});
                  G[b].PB((edge){a,f});
            }
            dijkstra(s);
            int ans=INF;
            REP(i,c+1)ans=min(ans,dist[d][i]);
            cout<<ans<<endl;
      }
}

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, n) rep(j, 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] = c;       // ?????°
    tmp[2] = s -  1;  // ??´???
    l[s - 1][c] = 0;
    que.push(tmp);
    while(!que.empty()){
      vi p = que.top();  que.pop();
      int ti = p[1];
      int v = p[2];
      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[1] = c;
          tmp[2] = e.to;
          que.push(tmp);
        }
      }

      if(c > 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[1] = c - 1;
            tmp[2] = e.to;
            que.push(tmp);
          }
        }
    }

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

  return 0;
}