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<queue>
#include<string.h>
using namespace std;

const int INF = 1<<30;

struct Node{
  int use,to,weight;
  Node() {;}
  Node(int use,int to,int weight) : use(use), to(to), weight(weight) {;}
};
bool operator<(const Node &e,const Node &f) {
  return e.weight > f.weight;
}

int dist[110][110];
bool visited[110][110];
int cost[110][110];

int main() {
  int c,n,m,s,d,a,b,f;
    while(cin>>c>>n>>m>>s>>d, c|n|m|s|d) {
      s--,d--;

      for(int i=0; i<110; ++i) {
	for(int j=0; j<110; ++j) {
	  dist[i][j] = INF;
	  cost[i][j] = INF;
	  visited[i][j] = false;
	}
      }

      for(int i=0; i<m; ++i) {
	cin>>a>>b>>f;
	a--,b--;
	cost[a][b] = f;
	cost[b][a] = f;
      }

      priority_queue<Node> Q;
      Q.push(Node(0,s,0));
      while(!Q.empty()) {
	Node now = Q.top(); Q.pop();
	if(visited[now.use][now.to]) continue;
	visited[now.use][now.to] = true;

	if(now.to == d) {
	  cout<<now.weight<<endl;
	  break;
	}

	int from = now.to;
	for(int to=0; to<n; ++to) {
	  if(cost[from][to] == INF) continue;
	  int nuse = now.use;
	  if(nuse < c && !visited[nuse+1][to]) {
	    int newcost = now.weight + cost[from][to]/2;
	    if(newcost < dist[nuse+1][to]) {
	      dist[nuse+1][to] = newcost;
	      Q.push(Node(nuse+1, to, newcost));
	    }
	  }
	  if(!visited[nuse][to]) {
	    int newcost = now.weight + cost[from][to];
	    if(newcost < dist[nuse][to]) {
	      dist[nuse][to] = newcost;
	      Q.push(Node(nuse, to, newcost));
	    }
	  }
	}
      }
    }
}

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

const int INF = 1e+9;

struct State {
  int f, v, tickets;
  State(int f, int v, int t) : f(f), v(v), tickets(t) {}
};

struct Node {
  int to, c;
  Node(int to, int c) : to(to), c(c) {}
};

bool operator > (const State& lhs, const State& rhs){
  if(lhs.f == rhs.f)
    return (lhs.v == rhs.v) ? lhs.tickets > rhs.tickets : lhs.v > rhs.v;
  else
    return lhs.f > rhs.f;
}

int N, M, C, d[101][11];
vector<Node> G[101];

void dijkstra(int s){
  for(int i = 0; i <= N; ++i)
    for(int j = 0; j <= C; ++j)
      d[i][j] = INF;
  for(int i = 0; i <= C; ++i)
    d[s][i] = 0;
  priority_queue<State, vector<State>, greater<State> > que;
  que.push(State(0, s, C));
  while(!que.empty()){
    State st = que.top();
    que.pop();
    if(d[st.v][st.tickets] < st.f)
      continue;
    for(int i = 0; i < G[st.v].size(); ++i){
      Node n = G[st.v][i];
      if(d[n.to][st.tickets] > st.f + n.c){
	d[n.to][st.tickets] = st.f + n.c;
	que.push(State(d[n.to][st.tickets], n.to, st.tickets));
      }
      if(d[n.to][st.tickets - 1] > st.f + n.c / 2){
	d[n.to][st.tickets - 1] = st.f + n.c / 2;
	que.push(State(d[n.to][st.tickets - 1], n.to, st.tickets - 1));
      }
    }
  }
}


int main() {
  int s, e, a, b, f;
  while(cin >> C >> N >> M >> s >> e && (N || M || C || s || e)){
    for(int i = 0; i <= 100; ++i)
      G[i].clear();
    for(int i = 0; i < M; ++i){
      scanf("%d%d%d", &a, &b, &f);
      G[a].push_back(Node(b, f));
      G[b].push_back(Node(a, f));
    }
    dijkstra(s);
    int ans = INF;
    for(int i = 0; i <= C; ++i)
      ans = min(ans, d[e][i]);
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(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);
#define sintttt(i,j,k,m) scanf("%d%d%d%d",&i,&j,&k,&m);

#define INF 1010000000
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

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


int main(){
	while(1){
		int c,n,m,s,g;
		cin>>c>>n>>m>>s>>g;
		if(n==0)break;
		vector<pair<int,int> > load[101];
		rep(i,m){
			int a,b,c;
			cin>>a>>b>>c;
			load[a].push_back(make_pair(b,c));
			load[b].push_back(make_pair(a,c));
		}
		
		priority_queue<S> que;
		que.push(S(s,0,0));
		int visit[11][101];
		int ans=-1;
		
		rep(j,11)rep(i,101)visit[j][i]=0;
		
		while(1){
			if(que.empty()==1){
				break;
			}
			
			S u=que.top();
			que.pop();
			
			if(visit[u.t][u.p]==1){
				continue;
			}
			visit[u.t][u.p]=1;
			
			if(u.p==g){
				ans=u.n;
				break;
			}
			
			rep(i,load[u.p].size()){
				if(u.t<c){
					que.push( S( load[u.p][i].first,  u.n+load[u.p][i].second/2 ,u.t+1));
				}
				que.push( S( load[u.p][i].first,  u.n+load[u.p][i].second,u.t));
			}
		}
		printf("%d\n",ans);
	}
}

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>(0)));
		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){
						auto 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;
	}
}