Go: package main

import (
	"container/heap"
	"fmt"
	"runtime"
)

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)
					}
				}
			}
		}
		runtime.GC()
	}
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX 10000
#define MAX_NODE_ID 100
#define MAX_TICKET 10
class Node{
public:
	int id;
	int cost;
	int ticket;
	Node(int i,int c,int t){
		id=i;
		cost=c;
		ticket=t;
	}
	bool operator < (const Node &o) const{
		return cost > o.cost;
	}
};
typedef priority_queue<Node> PQ;
int table[MAX_NODE_ID][MAX_NODE_ID];
bool visited[MAX_NODE_ID][MAX_TICKET];
int main(){

	for(int c,n,m,s,d;cin>>c>>n>>m>>s>>d&&(c||n||m||s||d);){
		for(int i=0;i<MAX_NODE_ID;i++)for(int j=0;j<MAX_NODE_ID;j++)table[i][j]=MAX;
		for(int i=0,a,b,f;i<m&&cin>>a>>b>>f;i++){
			table[a][b]=f;
			table[b][a]=f;
//			cout<<"table["<<a<<"]["<<b<<"]"<<table[a][b]<<endl;
//			cout<<"table["<<b<<"]["<<a<<"]"<<table[b][a]<<endl;
		}
		PQ pq;
		Node start(s,0,c);
		pq.push(start);
		while(!pq.empty()){
			int current_node=pq.top().id;
			int current_cost=pq.top().cost;
			int current_ticket=pq.top().ticket;
//			cout<<"current:"<<current_node<<endl;
			if(current_node==d)break;
			if(visited[current_node][current_ticket]);
			else{
				for(int i=1;i<=n;i++){
					if(current_node==i)continue;
					if(table[current_node][i]==MAX)continue;
	//				cout<<"["<<current_node<<"]["<<i<<"]"<<"cost:"<<table[current_node][i]<<endl;
					Node node1(i,current_cost+table[current_node][i],current_ticket);
					pq.push(node1);
					if(current_ticket>0){
						Node node2(i,current_cost+(table[current_node][i]/2),current_ticket-1);
						pq.push(node2);
					}
				}
			}
			pq.pop();
		}
		cout<<pq.top().cost<<endl;
	}


	return 0;
}

C++: #define _USE_MATH_DEFINES
#define INF 10000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;

static const double eps = 1e-8;

int path[101][101];
int used[101][11];

void bfs(int s,int d,int n,int c){
	priority_queue<PP,vector<PP>,greater<PP> > que;
	que.push(PP(0,P(s,c)));
	used[s][c] = 0;

	while(!que.empty()){
		int cost = que.top().first;
		int src = que.top().second.first;
		int ticket = que.top().second.second;
		que.pop();

		for(int dst=1;dst<=n;dst++){
			if(used[dst][ticket] > cost + path[src][dst]){
				used[dst][ticket] = cost + path[src][dst];
				que.push(PP(cost + path[src][dst],P(dst,ticket)));
			}

			if(ticket > 0 && used[dst][ticket-1] > cost + path[src][dst]/2){
				used[dst][ticket-1] = cost + path[src][dst]/2;
				que.push(PP(cost + path[src][dst]/2,P(dst,ticket-1)));
			}
		}
	}
}

int main(){
	//c:number of tickets
	//n:number of towns
	//m:number of buses
	//s:source
	//d:destination
	int c,n,m,s,d;
	while(~scanf("%d %d %d %d %d",&c,&n,&m,&s,&d)){
		if(c==n && n==m && m==s && s==d && d==0) break;
		memset(path,0x3,sizeof(path));
		memset(used,0x3,sizeof(used));

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

		bfs(s,d,n,c);
		int res=numeric_limits<int>::max();
		for(int i=c;i>=0;i--) res = min(res,used[d][i]);
		printf("%d\n",res);
	}
}

C++: #include <iostream>
#include <vector>

using namespace std;

#define INF 100000000

int main() {
	int c, n, m, s, d;
	while (cin >> c >> n >> m >> s >> d) {
		if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) {
			break;
		}
		vector< vector<int> > ff(n+1, vector<int>(n+1, INF));
		vector< vector<int> > ddd(n+1, vector<int>(n+1, INF));
		int a, b, f;
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> f;
			ff[a][b] = ff[b][a] = f;
			ddd[a][b] = ddd[b][a] = f;
		}
		for (int i = 1; i <= n; i++) {
			ddd[i][i] = 0;
		}
		for (int k = 1; k <= n; k++) {
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= n; j++) {
					ddd[i][j] = min(ddd[i][j], ddd[i][k]+ddd[k][j]);
				}
			}
		}
		vector< vector< vector<int> > > dp(n+1, vector< vector<int> >(n+1, vector<int>(c+1, INF)));
		for (int i = 1; i <= n; i++) {
			dp[s][i][0] = ddd[s][i];
		}
		for (int k = 1; k <= c; k++) {
			for (int p = 1; p <= n; p++) {
				dp[s][p][k] = dp[s][p][k-1];
				for (int i = 1; i <= n; i++) {
					for (int j = 1; j <= n; j++) {
						for (int q = 1; q <= n; q++) {
							if (ff[j][q] != INF) {
								if (dp[s][p][k] > dp[s][i][k-1]+ddd[i][j]+(ff[j][q]/2)+ddd[q][p]) {
									dp[s][p][k] = dp[s][i][k-1]+ddd[i][j]+(ff[j][q]/2)+ddd[q][p];
								}
							}
						}
					}
				}
			}
		}
		cout << dp[s][d][c] << endl;
	}
	return 0;
}