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, n)
		}
		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, m)
		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 <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<n; 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;
	}
}

C++: #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
struct edge{int to,cost;};
typedef pair<int,int>P;
typedef pair<int,P>P2;
int c,n,m,s,d,a1,a2,a3,dis[102][11];
vector<edge>v[102];
int main(){
  while(cin>>c>>n>>m>>s>>d,c){
    r(i,102)r(j,11)dis[i][j]=1e8;
    r(i,102)v[i].clear();
    r(i,m){
      cin>>a1>>a2>>a3;
      v[a1].push_back(edge{a2,a3});
      v[a2].push_back(edge{a1,a3});
    }
    priority_queue<P2,vector<P2>,greater<P2> >q;
    q.push(P2(0,P(s,c)));
    dis[s][c]=0;
    while(!q.empty()){
      P2 p=q.top();q.pop();
      int now=p.second.first;
      int card=p.second.second;
      int len=p.first;
      if(dis[now][card]<len)continue;
      r(i,v[now].size()){
        int next=v[now][i].to;
        int cost2=v[now][i].cost;
        if(dis[next][card]>cost2+len){
          dis[next][card]=cost2+len;
          q.push(P2(cost2+len,P(next,card)));
        }
        if(card&&dis[next][card-1]>cost2/2+len){
          dis[next][card-1]=cost2/2+len;
          q.push(P2(cost2/2+len,P(next,card-1)));
        }
      }
    }
    int ans=1e9;
    r(i,11)ans=min(ans,dis[d][i]);
    cout<<ans<<endl;
  }
}

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

#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
typedef pair<int,int> pii;

int C,N,M,S,D;
int pass[110][110], d[110][110], used[110][20];

int min(int a,int b){
	return (a>b?b:a);
}

int dijkstra(int s,int V){
 	rep(i,110)rep(j,110){	// clean 
 		d[i][j] = (1<<30);
 		used[i][j] = false;
 	}   
 	d[S][C] = 0;

 	while(true){	// search loop
 		int v = -1, w = -1;	
 		rep(u,N){	// loop ever node
 			rep(i,C+1){
 				if( !used[u][i] && (v==-1 || d[u][i] < d[v][w] ))
 					v=u,w=i;
 			}
 		}

 		if(v == -1)break;	// couldn't access to D node
 		used[v][w] = true;

 		rep(u,N){
 			d[u][w] = min(d[u][w] , d[v][w] + pass[v][u]);
 			if(w-1 >= 0 && pass[v][u] != (1<<30)){
 				d[u][w-1] = min(d[u][w-1],d[v][w] + pass[v][u]/2.0);
 			}
 		}
 	}
 	int ret = (1<<30);
 	rep(i,C+1)ret = min(ret,d[D][i]);
 	return ret;

}

int main(void){
	while(cin>>C>>N>>M>>S>>D,C+N+M+S+D){
		--S,--D;
		rep(i,110){
			rep(j,110)pass[i][j] = (1<<30);
			pass[i][i] = 0;
		}
		while(M--){
			int a,b,c;
			cin>>a>>b>>c;
			--a,--b;
			pass[a][b] = pass[b][a] = c;
		}
		cout<<dijkstra(S,D)<<endl;;
	}
	

}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef pair<int, int>P;

struct node {
	int first, second, c;
};
bool operator<(node a, node b) {
	return a.second > b.second;
}
vector<P>E[100];
int d[100][11];

int main() {
	int c, n, m, s, g;
	while (scanf("%d%d%d%d%d", &c, &n, &m, &s, &g), c) {
		s--; g--;
		rep(i, 100)E[i].clear();
		memset(d, 0x3f, sizeof(d));
		rep(i, m) {
			int a, b, f; scanf("%d%d%d", &a, &b, &f);
			a--; b--;
			E[a].push_back(P(b, f)); E[b].push_back(P(a, f));
		}
		priority_queue<node>que;
		que.push({ s,0,c });
		d[s][0] = 0;
		while (!que.empty()) {
			node p = que.top(); que.pop();
			if (d[p.first][p.c - 1] < p.second)continue;
			for (P i : E[p.first]) {
				if (p.c&& d[i.first][p.c - 1] > p.second + i.second / 2) {
					d[i.first][p.c - 1] = p.second + i.second / 2;
					que.push({ i.first, d[i.first][p.c - 1],p.c - 1 });
				}
				if (d[i.first][p.c] > p.second + i.second) {
					d[i.first][p.c] = p.second + i.second;
					que.push({ i.first, d[i.first][p.c],p.c });
				}
			}
		}
		int Min = INT_MAX;
		rep(i, c + 1)Min = min(Min, d[g][i]);
		printf("%d\n", Min);
	}
}