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
}

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)
		for len(pq) != 0 {
			now := heap.Pop(&pq).(*Node)
			if now.NowPosition == d {
				fmt.Println(now.Amount)
				break
			}
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				heap.Push(&pq, next)
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					heap.Push(&pq, next)
				}
			}
		}
	}
}

C++: #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
using namespace std;

const int INF=10000000;
typedef struct edge{
  int t,c;
  edge(){}
  edge(int t,int c):t(t),c(c){}
};
typedef pair<int ,pair<int,int> > P;


vector<edge> g[105];
int id[11][105];
int c,n,m,s,d;

int main(void){
  while(1){
    scanf("%d %d %d %d %d",&c,&n,&m,&s,&d);
    if(!c && !n && !m && !s && !d) break;
    for(int i=0;i<=n;i++)g[i].clear();

    for(int i=0;i<m;i++){
      int a,b,f;
      scanf("%d %d %d",&a,&b,&f);
      g[a].push_back(edge(b,f));
      g[b].push_back(edge(a,f));
    }


    priority_queue<P,vector<P>,greater<P> > que;
    for(int i=0;i<=c;i++) fill(id[i],id[i]+n+1,INF);
    for(int i=0;i<10;i++)id[i][s]=0;
    que.push(P(0,pair<int,int>(s,c)));
    while(!que.empty()){
      P p=que.top();
      que.pop();
      int v=p.second.first;
      int nc=p.second.second;
      if(id[c-nc][v]<p.first) continue;
      for(int i=0;i<g[v].size();i++){
	edge e = g[v][i];
	if(id[c-nc][e.t]>id[c-nc][v]+e.c){
	  id[c-nc][e.t]=id[c-nc][v]+e.c;
	  que.push(P(id[c-nc][e.t],pair<int,int>(e.t,nc)));
	}
	if(nc>0 && id[c-nc+1][e.t]>id[c-nc][v]+e.c/2){
	  id[c-nc+1][e.t]=id[c-nc][v]+e.c/2;
	  que.push(P(id[c-nc+1][e.t],pair<int,int>(e.t,nc-1)));
	}
      }
    }
    printf("%d\n",id[c][d]);
  }
}

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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<28

using namespace std;
typedef pair<int,int> P;


struct edge
{
	int to;
	int cost;

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

int d[101][11];
vector<edge> G[101];

void dijkstra(int s,int n,int ticket)
{
	priority_queue<P,vector<P>,greater<P> > que;
	rep(i,n) d[i][ticket] = INF;
	d[s][ticket] = 0;
	que.push(P(0,s));

	while(!que.empty())
	{
		P p = que.top();
		que.pop();

		int v = p.second;
		if(d[v][ticket] < p.first) continue;

		rep(i,G[v].size())
		{
			edge e = G[v][i];
			int temp;

			if(ticket == 0)
			{
				temp = d[v][ticket] + e.cost;
			}
			else
			{
				temp =  min(d[v][ticket]+e.cost,d[v][ticket-1]+e.cost/2);
			}

			if(d[e.to][ticket] > temp)
			{
				d[e.to][ticket] = temp;
				que.push(P(d[e.to][ticket],e.to));
			}
		}
	}
}

int main()
{
	int c,n,m,s,t;
	while(cin >> c >> n >> m >> s >> t)
	{
		if(c == 0 && n == 0 && m == 0 && s == 0 && t == 0) break;
		
		s--;
		t--;
		rep(i,100) G[i].clear();

		rep(i,m)
		{
			int a,b,cost;
			cin >> a >> b >> cost;
			a--;
			b--;

			edge e(b,cost);
			G[a].push_back(e);

			edge e2(a,cost);
			G[b].push_back(e2);
		}
		
		rep(i,n) dijkstra(s,n,i);

		cout <<  d[t][c] << endl;
	}

	return 0;
}

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

using namespace std;

#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 fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS  = 1e-9;

int C, N, M, S, D;
vector<pii> g[100];
int d[11][100];

int main() {
	while (cin >> 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].push_back(pii(b, f));
			g[b].push_back(pii(a, f));
		}
		
		fill(d[0], d[11], INF);
		
		priority_queue<pii, vector<pii>, greater<pii> > pq[11];
		
		pq[0].push(pii(0, S));
		d[0][S] = 0;
		
		REP(i, C + 1) {
			while (!pq[i].empty()) {
				pii now = pq[i].top(); pq[i].pop();
				int u = now.se, cost = now.fi;
				REP(i, g[u].size()) {
					int v = g[u][i].fi, c = g[u][i].se;
					if (i < C && chmin(d[i + 1][v], cost + c / 2)) pq[i + 1].push(pii(cost + c / 2, v));
					if (chmin(d[i][v], cost + c)) pq[i].push(pii(cost + c, v));
				}
			}
		}
		
		int ans = INF;
		REP(i, C + 1) chmin(ans, d[i][D]);
		cout << ans << endl;
	}
	return 0;
}