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()
	}
}

Python: """ Created by Jieyi on 10/31/16. """
import io
import sys
import heapq

# Simulate the redirect stdin.
from collections import defaultdict

if len(sys.argv) > 1:
    filename = sys.argv[1]
    inp = ''.join(open(filename, "r").readlines())
    sys.stdin = io.StringIO(inp)


def input_data():
    while True:
        tickets, vertices, edges, start, end = map(int, input().split())
        if 0 == tickets + vertices + edges + start + end:
            break

        shortest_path = [[sys.maxsize for _ in range(vertices + 1)] for _ in range(tickets + 1)]
        for i in range(tickets + 1):
            shortest_path[i][start] = 0
        g = defaultdict(list)
        h = []
        # Generate a map.
        for _ in range(edges):
            i, j, fare = map(int, input().split())
            g[i].append((j, fare))
            g[j].append((i, fare))
        # (price, vertices, remainder ticket)
        heapq.heappush(h, (0, start, tickets))

        while True:
            if 0 == len(h):
                break
            ori_price, vertex, remain_ticket = heapq.heappop(h)
            for v, price in g[vertex]:
                if shortest_path[remain_ticket][v] > ori_price + price:
                    shortest_path[remain_ticket][v] = min(shortest_path[remain_ticket][v], ori_price + price)
                    heapq.heappush(h, (shortest_path[remain_ticket][v], v, remain_ticket))
                if remain_ticket and shortest_path[remain_ticket - 1][v] > ori_price + int(price / 2):
                    shortest_path[remain_ticket - 1][v] = min(shortest_path[remain_ticket - 1][v], ori_price + int(price / 2))
                    heapq.heappush(h, (shortest_path[remain_ticket - 1][v], v, remain_ticket - 1))

        cheapest = sys.maxsize
        for i in range(tickets):
            cheapest = min(shortest_path[i][end], cheapest)
        print(cheapest)


def main():
    input_data()


if __name__ == '__main__':
    main()

Python: def solve():
    from heapq import heappush, heappop
    from sys import stdin
    f_i = stdin
    
    while True:
        c, n, m, s, d = map(int, f_i.readline().split())
        if c == 0:
            break
        s -= 1
        d -= 1
        
        adj = [[] for i in range(n)]
        for i in range(m):
            a, b, f = map(int, f_i.readline().split())
            a -= 1
            b -= 1
            adj[a].append((b, f))
            adj[b].append((a, f))
        
        # Dijkstra's algorithm
        fare = [[1000000] * (c + 1) for i in range(n)]
        fare[s][c] = 0
        hq = [(0, s, c)]
        while hq:
            f1, u, rc = heappop(hq) # rc: number of remaining coupons
            
            if u == d:
                print(f1)
                break
            
            for v, f2 in adj[u]:
                f3 = f1 + f2
                if f3 < fare[v][rc]: # not using coupon
                    fare[v][rc] = f3
                    heappush(hq, (f3, v, rc))
                f4 = f1 + f2 // 2
                if rc and f4 < fare[v][rc-1]: # using coupon
                    fare[v][rc-1] = f4
                    heappush(hq, (f4, v, rc - 1))

solve()

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

const int INF = 1<<30;
int c, n, m, s, d, a, b, f;
int path[101][101];
int dp[101][11] = {};

void solve(){
    // { cost, {source, ticket} }
    priority_queue<pair<int, pair<int,int>>> pq;
    pq.push({0, {s, c}});
    dp[s][c] = 0;

    while(!pq.empty()){
        auto p = pq.top();  pq.pop();
        int cost = -p.first;
        int source = p.second.first;
        int ticket = p.second.second;

        for(int dst = 0; dst < n; dst++){
            if(path[source][dst] == 0)  continue;

            if(dp[dst][ticket] > cost + path[source][dst]){
                dp[dst][ticket] = cost + path[source][dst];
                pq.push({-dp[dst][ticket], {dst, ticket}});
            }

            if(ticket > 0 && dp[dst][ticket-1] > cost + path[source][dst]/2){
                dp[dst][ticket-1] = cost + path[source][dst]/2;
                pq.push({-dp[dst][ticket-1], {dst, ticket-1}});
            }
        }
    }
}

int main(){
    while(cin >> c >> n >> m >> s >> d){
        if(c + n + m + s + d == 0)  break;

        s--; d--;
        // initialize
        memset(path, 0, sizeof(path));
        for(int i = 0; i < 101; i++){
            for(int j = 0; j < 11; j++){
                dp[i][j] = INF;
            }
        }

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

        solve();

        int ans = INF;
        for(int ticket = 0; ticket <= c; ticket++)  ans = min(ans, dp[d][ticket]);
        cout << ans << endl;
    }
    return 0;
}