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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

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

const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int n, m, res;

#define debug(a, b, c) for(int i = 0; i < b; i++){for(int j = 0; j < c; j++){printf("%12d", a[i][j]);}puts("");}puts("-----------------------------------------");

int main()
{
	int wk, tk, c;
	int disc, city, line, start, goal;
	int route[512][512], cost[512][16];
	bool used[512];
	while(cin >> disc >> city >> line >> start >> goal)
	{
		if(!disc && !city && !line && !start && !goal) break;

		start--, goal--;
		for(int i = 0; i < city; i++)
		{
			fill(route[i], route[i] + city, INF);
			fill(cost[i], cost[i] + city, INF);
		}

		for(int i = 0; i < line; i++)
		{
			cin >> wk >> tk >> c;
			route[wk-1][tk-1] = c;
			route[tk-1][wk-1] = c;
		}

		for(int m = 0; m <= disc; m++)
		{
			cost[start][m] = 0;
			//debug(cost, disc, city);
			fill(used, used + city, false);

			while(true)
			{
				int v = -1;
				for(int i = 0; i < city; i++)
				{
					if(!used[i] && (v == -1 || cost[i][m] < cost[v][m])) v = i;
				}

				if(v == -1) break;

				used[v] = true;

				for(int i = 0; i < city; i++)
				{
					if(m == 0)	
						cost[i][m] = min(cost[i][m], cost[v][m] + route[v][i]);
					else
						cost[i][m] = min(cost[v][m - 1] + route[v][i] / 2, min(cost[i][m], cost[v][m] + route[v][i]));
				}
			}
		}
		cout <<	cost[goal][disc] << endl;

	}
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class Edge
{
public:
    int to, cost;
    Edge(){};
    Edge(int to0, int cost0){to = to0; cost = cost0;}
};

void shortestPath(const vector<vector<Edge> >& edges, int start, vector<int>& dist)
{
    dist.assign(edges.size(), INT_MAX);
    dist[start] = 0;
    priority_queue<pair<int,int> ,vector<pair<int,int> >, greater<pair<int,int> > > q;
    q.push(make_pair(0, start));

    while(!q.empty()){
        pair<int, int> p = q.top();
        q.pop();
        int v = p.second;
        if(dist[v] < p.first)
            continue;
        for(unsigned i=0; i<edges[v].size(); ++i){
            Edge e = edges[v][i];
            if(dist[v] + e.cost < dist[e.to]){
                dist[e.to] = dist[v] + e.cost;
                q.push(make_pair(dist[e.to], e.to));
            }
        }
    }
}

int main()
{
    for(;;){
        int c, n, m, s, d;
        cin >> c >> n >> m >> s >> d;
        if(c == 0)
            return 0;
        -- s;
        -- d;

        vector<vector<Edge> > edges((c+1) * n);
        for(int i=0; i<m; ++i){
            int a, b, f;
            cin >> a >> b >> f;
            -- a;
            -- b;

            for(int j=0; j<=c; ++j){
                edges[a+j*n].push_back(Edge(b+j*n, f));
                edges[b+j*n].push_back(Edge(a+j*n, f));
            }
            for(int j=0; j<c; ++j){
                edges[a+j*n].push_back(Edge(b+(j+1)*n, f/2));
                edges[b+j*n].push_back(Edge(a+(j+1)*n, f/2));
            }
        }

        vector<int> dist;
        shortestPath(edges, s, dist);
        int ret = INT_MAX;
        for(int i=0; i<=c; ++i)
            ret = min(ret, dist[d+i*n]);
        cout << ret << endl;
    }
}