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<iostream>
#include<vector>
#include<queue>
#include<set>
#include<climits>
using namespace std;

struct Data{
  int id;
  int cost;
  int ticket;
};
bool operator<(Data a, Data b){
  return a.cost!=b.cost?a.cost>b.cost:(a.ticket!=b.ticket?a.ticket>b.ticket:a.id>b.id);
}
struct Data2{
  int id;
  int ticket;
};
bool operator<(Data2 a, Data2 b){
  return a.id!=b.id?a.id>b.id:a.ticket<b.ticket;
}

#define NONE INT_MAX

int Dijkstra(vector< vector<int> >A,int start,int goal,int ticket)
{
  vector<int>cost(A.size(),NONE);
  priority_queue<Data>q;
  set<Data2>used;

  Data node={start,0,ticket};
  q.push(node);
  cost[start]=0;

  while(q.size()){
    node=q.top();q.pop();
    if(node.id==goal)return node.cost;
    Data2 t={node.id,node.ticket};
    if(used.count(t))continue;
    used.insert(t);
    for(int i=0;i<A[node.id].size();i++){
      if(A[node.id][i]==NONE)continue;
      if(cost[i]>cost[node.id]+A[node.id][i]){
	Data next={i,cost[node.id]+A[node.id][i],node.ticket};
	cost[i]=next.cost;
	q.push(next);
      }
      if(node.ticket){
	if(cost[i]>cost[node.id]+(A[node.id][i])/2){
	  Data next={i,cost[node.id]+(A[node.id][i])/2,node.ticket-1};
	  cost[i]=next.cost;
	  q.push(next);
	}
      }
    }
  }
  return -1;
}

int main()
{
  int c,n,m,s,d;
  int a,b,f;
  while(cin>>c>>n>>m>>s>>d,c|n|m|s|d){
    s--;d--;
    vector< vector<int> >A(n,vector<int>(n,NONE));
    for(int i=0;i<m;i++){
      cin>>a>>b>>f;
      a--;b--;
      A[a][b]=A[b][a]=f;
    }
    cout<<Dijkstra(A,s,d,c)<<endl;
  }
}

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

int calcCost(int c, int n, vector<int> path, vector<int> rt){
	priority_queue<int> costs;
	for(int i=1; i<rt.size(); i++){
		costs.push(path[rt[i]+n*rt[i-1]]);
	}
	int total = 0;
	while(costs.size() && c--){
		total += costs.top() / 2;
		costs.pop();
	}
	while(costs.size()){
		total += costs.top();
		costs.pop();
	}
	return total;
}
int main(){
	for(;;){
		int c,n,m,s,d;
		cin>>c>>n>>m>>s>>d;
		if(c+n+m+s+d==0) break;
		vector<pair<int, vector<int>>> route(n,make_pair(-1,vector<int>()));
		vector<int> path(n*n,-1);
		for(int i=0; i<m; i++){
			int a,b,f;
			cin>>a>>b>>f;
			path[--a+n*--b] = path[b+n*a] = f;
		}
		priority_queue<pair<int, vector<int>>, vector<pair<int, vector<int>>>, greater<pair<int, vector<int>>>> pq;
		pq.push(make_pair(0,vector<int>(1,s-1)));
		while(pq.size()){
			int cur = *(pq.top().second.rbegin());
			if(route[cur].first == -1){
				route[cur] = pq.top();
				for(int i=0; i<m; i++){
					if(route[i].first == -1 && path[i+n*cur] != -1){
						pair<int, vector<int>> copy = pq.top();
						copy.second.push_back(i);
						copy.first = calcCost(c,n,path,copy.second);
						pq.push(copy);
					}
				}
			}
			pq.pop();
		}
		cout<<route[d-1].first<<endl;
	}
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<int,pii> piii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999
#define MAX_V 100

struct edge{int to,cost;};
vector<edge> G[MAX_V];
int d[MAX_V][10];
int c,n,m,s,g;

int dijkstra(int s,int V){
    priority_queue< piii,vector<piii>,greater<piii> > que;
    rep(i,MAX_V)rep(j,10)d[i][j]=INF;
    d[s][c]=0;
    que.push(make_pair(0,pii(s,c)));
    
    while(que.size()){
        piii p=que.top();
        que.pop();
        
        int v=p.second.first ;
        int w=p.second.second;
        if(d[v][w]<p.first)continue;
        
        rep(i,G[v].size()){
            edge e=G[v][i];
            rep(j,10){
                if(d[e.to][j]>d[v][j]+e.cost){
                    d[e.to][j]=d[v][j]+e.cost;
                    que.push(make_pair(d[e.to][j],pii(e.to,j)));
                }
            }
            for(int j = 1;j<10;j++){
                if(d[e.to][j-1]>d[v][j]+e.cost/2){
                    d[e.to][j-1]=d[v][j]+e.cost/2;
                    que.push(make_pair(d[e.to][j-1],pii(e.to,j-1)));
                }
            }
        }
    }
    int mini = INF;
    rep(i,10){
        mini = min(mini,d[V][i]);
    }
    return mini;
}

int main(){
    while(cin>>c>>n>>m>>s>>g&&c&&n&&m&&s&&g){
        rep(i,MAX_V)rep(j,10)G[i].clear();
        s--,g--;
        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});
        }
        cout<<dijkstra(s,g)<<endl;
    }
}