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 <queue>
#include <algorithm>
#include <vector>
using namespace std;

const int INF = 1e9;

//<最小コスト, 町番号>
typedef pair<int, int> P;
struct edge{int to, cost;};
int c, n, m, s, dist;
vector<edge> G[100];
//割引券が残りiの時のj地点における最小コスト
int d[10][100];

void dijkstra(int s, int a){
	priority_queue<P, vector<P>, greater<P> > Q;
	for(int i = 0; i < n; i++) d[a][i] = INF;
	d[a][s] = 0;
	Q.push(P(0, s));

	while(!Q.empty()){
		P p = Q.top(); Q.pop();
		int v = p.second;

		if(d[a][v] < p.first)	continue;
		for(int i = 0; i < G[v].size(); i++){
			edge e = G[v][i];
			
			//割引券を使った時と使ってない時でよりコストが小さい時をdmに代入
			int dm;
			if(a == 0)	dm = d[a][v] + e.cost;
			else dm = min(d[a][v] + e.cost, d[a-1][v] + e.cost / 2);

			if(d[a][e.to] > dm){
				d[a][e.to] = dm;
				Q.push(P(d[a][e.to], e.to));
			}
		}
	}
}

int main(void){
	while(true){
		cin >> c >> n >> m >> s >> dist;
		s--; dist--;
		if(!c)	break;

		edge e;
		for(int i = 0; i < m; i++){
			int a, b, f;
			cin >> a >> b >> f;
			a--; b--;
			e.to = b; e.cost = f;
			G[a].push_back(e);
			e.to = a;
			G[b].push_back(e);
		}

		int ans = INF;

		for(int i = 0; i <= c; i++){
			dijkstra(s, i);
		}

		cout << d[c][dist] << endl;

		for(int i = 0; i < 100; i++) G[i].clear();
	}

  return 0;
}

C++: #include <map>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
int cost[101][11];
int main(){
	int c,N,m,s,d,i,j,k,x;
	for(;scanf("%d%d%d%d%d",&c,&N,&m,&s,&d),N;){
		vector<map<int,int> >dist(N+1);
		memset(cost,99,sizeof(cost));
		for(k=0;k<m;k++){
			scanf("%d%d%d",&i,&j,&x);dist[i][j]=dist[j][i]=x;
		}
		for(i=0;i<=c;i++)cost[s][i]=0;
		for(;~c;c--){
			vector<bool>used(N+1);
			for(;;){ //dijkstra
				int m=1e9;
				for(i=1;i<=N;i++)if(!used[i]&&m>cost[i][c])m=cost[i][c];
				if(m==1e9)break;
				for(j=1;j<=N;j++)if(m==cost[j][c]){
					map<int,int>::iterator it=dist[j].begin();
					for(used[j]=1;it!=dist[j].end();it++){
						if(cost[it->first][c]>it->second+cost[j][c])cost[it->first][c]=it->second+cost[j][c];
						if(c&&cost[it->first][c-1]>it->second/2+cost[j][c])cost[it->first][c-1]=it->second/2+cost[j][c];
					}
				}
			}
		}
		printf("%d\n",cost[d][0]);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct line {
    ll a, b, f;
};
using pllll = pair<ll, ll>;
using pl6 = pair<ll, pair<ll, ll>>;
struct edge {
    ll dstb, dstc, cost;
};
int main() {
    
    ll c, n, m, s, d;
    while (cin >> c >> n >> m >> s >> d, c) {

        s--; d--;

        vector<line> l(m, line{0, 0, 0});
        for (ll i = 0; i < m; i++) {
            cin >> l[i].a >> l[i].b >> l[i].f;
            l[i].a--; l[i].b--;
            if (l[i].a > l[i].b) swap(l[i].a, l[i].b);
        }

        sort(l.begin(), l.end(), [](line &i, line &j) -> bool {
                 if (i.a != j.a) return i.a < j.a;
            else if (i.b != j.b) return i.b < j.b;
            else return i.f < j.f;
        });

        // 被った路線はなかったことに
        for (ll i = m; --i; ) {
            if (l[i].a == l[i-1].a && l[i].b == l[i-1].b) {
                l.erase(l.begin() + i);
            }
        }
        m = l.size();

        
        // 無向グラフを構築
        vector<vector<vector<edge>>> g(n, vector<vector<edge>>(c+1));
        for (ll i = m; i--; ) {
            ll a = l[i].a;
            ll b = l[i].b;
            ll f = l[i].f;
            for (ll c0 = c; c0 >= 0; c0--) {
                g[a][c0].push_back(edge{b, c0, f});
                g[b][c0].push_back(edge{a, c0, f});
                if (c0 > 0) {
                    g[a][c0].push_back(edge{b, c0-1, f/2});
                    g[b][c0].push_back(edge{a, c0-1, f/2});
                }
            }
        }

        // 拡張Dijkstra
        vector<vector<ll>> dp(n, vector<ll>(c+1, 0x7FFFFFFFLL));
        vector<vector<bool>> determined(n, vector<bool>(c+1, false));
        priority_queue<pl6, vector<pl6>, greater<pl6>> q;
        q.push(pl6(0, pllll(s, c)));
        dp[s][c] = 0;
        while (!q.empty()) {
            pl6 cv = q.top(); q.pop();
            pllll v = cv.second;
            ll vf = dp[v.first][v.second];
            if (determined[v.first][v.second]) continue;
            determined[v.first][v.second] = true;
            for (edge &ed : g[v.first][v.second]) {
                if (determined[ed.dstb][ed.dstc]) continue;
                if (vf + ed.cost < dp[ed.dstb][ed.dstc]) {
                    q.push(pl6(vf + ed.cost, pllll(ed.dstb, ed.dstc)));
                    dp[ed.dstb][ed.dstc] = vf + ed.cost;
                }
            }
        }

        ll ans = 0x7FFFFFFFLL;
        for (ll i = 0; i <= c; i++) {
            ans = min(ans, dp[d][i]);
        }
        cout << ans << endl;

    }

}