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]int)
		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}] = now.Amount
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				if memoAmount, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok || (ok && memoAmount > next.Amount) {
					memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}] = next.Amount
					heap.Push(&pq, next)
				}
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					if memoAmount, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok || (ok && memoAmount > next.Amount) {
						memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}] = next.Amount
						heap.Push(&pq, next)
					}
				}
			}
		}
		//runtime.GC()
	}
}

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

#define INF 1 << 29

using namespace std;

int c,n,m,s,d;
int cost[101][101];

class S{
public:
    int node;
    int tick;
    int cost;

    S(int node,int tick,int cost):
        node(node),tick(tick),cost(cost){}
    S(){}
    bool operator <(const S arg) const{
        return cost > arg.cost;
    }
};

int dijkstra(int src,int dst){
    priority_queue<S> q;
    int memo[n+1][c+1];
    S tmp;
    fill((int *)memo, (int *)memo+(n+1)*(c+1), INF);
    memo[src][c] = 0;
    q.push(S(src,c,0));

    while(!q.empty()){
        tmp = q.top();
        q.pop();
        for(int i=0;i<=n;i++){
            if(cost[tmp.node][i] != INF){
                if(cost[tmp.node][i] + tmp.cost < memo[i][tmp.tick]){
                    q.push(S(i,tmp.tick,cost[tmp.node][i] + tmp.cost));
                    memo[i][tmp.tick] = cost[tmp.node][i] + tmp.cost;
                }
                if(tmp.tick && cost[tmp.node][i]/2 + tmp.cost < memo[i][tmp.tick-1]){
                    q.push(S(i,tmp.tick-1,cost[tmp.node][i]/2 + tmp.cost));
                    memo[i][tmp.tick-1] = cost[tmp.node][i]/2 + tmp.cost;
                }
            }
        }
    }
    int ret = 1<<29;
    for(int i=0;i<=c;i++){
        ret = min(ret, memo[dst][i]);
    }
    return ret;
}

int main(){
    while(cin >> c >> n >> m >> s >> d){
        if((c|n|m|s|d) == 0) break;
        int src,dst,c;
        fill((int *)cost, (int *)cost+101*101, INF);
        for(int i=0;i<m;i++){
            cin >> src >> dst >> c;
            cost[src][dst] = c;
            cost[dst][src] = c;
        }

        cout << dijkstra(s,d) << endl;
    }
}

C++: #include<iostream>
#include<utility>
#include<vector>
#include<cstdio>
#include<queue>
#include <map>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
//int in(){int x;cin>>x;return x;}

int main ()
{
	int c,n,m,s,d;
	while(cin>>c>>n>>m>>s>>d&&c&&n&&m&&s&&d)
	{
		int flg[110][20]={};
		for(int i=0;i<110;i++)for(int j=0;j<20;j++)flg[i][j]=9999999;
		int ans =-1;
		priority_queue < PP, vector<PP>, greater<PP> > q;
		vector< P > way[110];
		for(int i=0;i<m;i++)
		{
			int a,b,f;
			cin>>a>>b>>f;
			way[a].push_back(P(b,f));
			way[b].push_back(P(a,f));
		}
		PP next;
		q.push(PP(0,P(s,c)));
		while(!q.empty())
		{
			PP now=q.top();q.pop();
			
			flg[now.second.first][now.second.second]=now.first;
			if(now.second.first==d)
			{
				ans=now.first;
				break;
			}
			
			for(int i=0;i<way[now.second.first].size();i++)
			{
				if(flg[way[now.second.first][i].first][now.second.second]>=now.first+way[now.second.first][i].second){
				next.first=now.first+way[now.second.first][i].second;
				next.second.first=way[now.second.first][i].first;
				q.push(next);
				}
				if(now.second.second==0)continue;
				if(flg[way[now.second.first][i].first][now.second.second-1]>=now.first+way[now.second.first][i].second/2){
				next.first=now.first+way[now.second.first][i].second/2;
				next.second.first=way[now.second.first][i].first;
				next.second.second=now.second.second-1;
				q.push(next);
				}
			}
			
		}
		cout<<ans<<endl;
	}
	
	
	return 0;
}

C++: #include <iostream>
#define INF 1000000000
using namespace std;


struct edge{
	int from, to, cost;
};

int ans[1000];
int counter=0;
bool flag=true;

void solve(){
	int d[101][11];//地点と割引回数
	edge route[500];//路線
	int c,n,m,s,t;//割引回数,町数,路線数,出発駅,到着駅
	cin >> c>>n>>m>>s>>t;
	if(c==0) {flag=false; return;}
	
	for(int i=0;i<m;i++){
		cin >>route[i].from>>route[i].to>>route[i].cost;
	}
	
	for(int i=1;i<=n;i++){
		for(int j=0;j<c+1;j++){//割引回数は割引無しも必要
			d[i][j]=INF;
		}
	}
	
	d[s][0]=0;
	bool update=true;
	while(update){
		update=false;
		for(int i=0;i<m;i++){
			edge e=route[i];//見やすく
			for(int j=0;j<=c;j++){
				//まずは割引無し
				if(d[e.from][j]!=INF && d[e.to][j]>d[e.from][j]+e.cost){
					d[e.to][j]=d[e.from][j]+e.cost; update=true;
				}
				if(d[e.to][j]!=INF && d[e.from][j]>d[e.to][j]+e.cost){
					d[e.from][j]=d[e.to][j]+e.cost; update=true;
				}
				
			}
			
			for(int j=0;j<c;j++){
				if(d[e.from][j]!=INF && d[e.to][j+1]>d[e.from][j]+e.cost/2){
					d[e.to][j+1]=d[e.from][j]+e.cost/2; update=true;
				}
				if(d[e.to][j]!=INF && d[e.from][j+1]>d[e.to][j]+e.cost/2){
					d[e.from][j+1]=d[e.to][j]+e.cost/2; update=true;
				}
			}
		}
	}
	
	ans[counter]=d[t][c];
	counter++;
}

int main(){
	while(flag){
		solve();
	}
	for(int i=0;i<counter;i++){
		cout << ans[i]<<endl;
	}
}