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 <vector>
#include <set>

using namespace std;

#define INF 100000000

typedef pair<int, int> P;

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];
			dp[i][d][0] = ddd[i][d];
		}
		set<P> st[101][101][11];
		for (int k = 1; k <= c; k++) {
			for (int i = 1; i <= n; i++) {
				dp[s][i][k] = dp[s][i][k-1];
				dp[i][d][k] = dp[i][d][k-1];
				st[s][i][k] = st[s][i][k-1];
				st[i][d][k] = st[i][d][k-1];
				int ka, kb, kc, kd;
				ka = kb = kc = kd = 0;
				for (int j = 1; j <= n; j++) {
					for (int p = 1; p <= n; p++) {
						if (ff[j][p] != INF && st[s][i][k].find(P(j, p)) == st[s][i][k].end()) {
							if (dp[s][i][k] > dp[s][j][k-1]+(ff[j][p]/2)+ddd[p][i]) {
								dp[s][i][k] = dp[s][j][k-1]+(ff[j][p]/2)+ddd[p][i];
								ka = j;
								kb = p;
							}
						}
						if (ff[j][p] != INF && st[i][d][k].find(P(j, p)) == st[i][d][k].end()) {
							if (dp[i][d][k] > dp[i][j][k-1]+(ff[j][p]/2)+ddd[p][d]) {
								dp[i][d][k] = dp[i][j][k-1]+(ff[j][p]/2)+ddd[p][d];
								kc = j;
								kc = p;
							}
						}
					}
				}
				if (ka != 0) {
					st[s][i][k].insert(P(ka, kb));
				}
				if (kc != 0) {
					st[i][d][k].insert(P(kc, kd));
				}
			}
		}
		cout << dp[s][d][c] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
 
#define MAX 10000000
#define n_MAX 101
#define c_MAX 11

typedef pair<int, pair<int, int> > edge;

inline int get_cost(edge e){
  return e.first;
}

inline int get_tickets(edge e){
  return -(e.second.first);
}

inline int get_station(edge e){
  return e.second.second;
}

inline edge make_edge(int cost, int tickets, int station){
  return make_pair(cost, make_pair(-tickets, station));
}

int main() {
 
  while( true ) {
    
    long long int c, n, m, s, d;
    cin >> c >> n >> m >> s >> d;
    if ( n == 0 ) break;
    
    long long int l[n_MAX][n_MAX] = {0};
    
    for ( long long int i = 0; i < m; i++ ) {
      long long int a, b, f;
      cin >> a >> b >> f;
      l[a][b] = l[b][a] = f;
    }
 
    long long int t[c_MAX][n_MAX];
    for ( long long int j = 0; j <= c; j++ ) {
      for ( long long int i = 1; i <= n; i++ ) {
	t[j][i] = MAX;
      }
    }
    
    priority_queue<edge, vector<edge>, less<edge> > queue;
    queue.push(make_edge(0, c, s));
    t[c][s] = 0;

    int ans = MAX;
    while(!queue.empty()){
      edge e = queue.top(); queue.pop();

      const int cost = get_cost(e);
      const int tickets = get_tickets(e);
      const int station = get_station(e);
      if(t[tickets][station] > cost){
	continue;
      }else if(station == d){
	ans = min(ans, cost);
      }
      

      for(long long int next = 1; next <= n; next++){
	if(l[station][next] == 0){
	  continue;
	}
	
	const int next_non_use_cost = cost + l[station][next];
	const int next_use_cost = cost + l[station][next] / 2;
 
	if(tickets > 0 && t[tickets - 1][next] > next_use_cost){
	  t[tickets - 1][next] = next_use_cost;
	  queue.push(make_edge(next_use_cost, tickets - 1, next));
	}
	
	if(t[tickets][next] > next_non_use_cost){
	  t[tickets][next] = next_non_use_cost;
	  queue.push(make_edge(next_non_use_cost, tickets, next));
	}
      }
    }
 
    cout << ans << endl;
  }
 
  return 0;
 
}

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

using namespace std;

typedef pair<int, int> Pair;

int main() {
  int c, n, m, s, d, a, b, f;
  unsigned long long answer;
  vector<Pair> edges[1100];
  unsigned long long min_cost[1100];
  priority_queue<Pair, vector<Pair>, greater<Pair> > heap;
  while (true) {
    cin >> c >> n >> m >> s >> d;
    if (c == 0) {
      return 0;
    }
    for (int i = 0; i < (c + 1) * n; ++i) {
      edges[i].clear();
      min_cost[i] = -1;
    }
    for (int i = 0; i < m; ++i) {
      cin >> a >> b >> f;
      for (int ci = 0; ci <= c; ++ci) {
        edges[a - 1 + ci * n].push_back(Pair(b - 1 + ci * n, f));
        edges[b - 1 + ci * n].push_back(Pair(a - 1 + ci * n, f));
        if (ci < c) {
          edges[a - 1 + ci * n].push_back(Pair(b - 1 + ci * n + n, f / 2));
          edges[b - 1 + ci * n].push_back(Pair(a - 1 + ci * n + n, f / 2));
        }
      }
    }
    heap.push(Pair(0, s - 1));
    min_cost[s - 1] = 0;
    while (not heap.empty()) {
      int p1 = heap.top().second;
      heap.pop();
      for (size_t i = 0; i < edges[p1].size(); ++i) {
        int p2 = edges[p1][i].first;
        int len = edges[p1][i].second;
        if (min_cost[p1] + len < min_cost[p2]) {
          min_cost[p2] = min_cost[p1] + len;
          heap.push(Pair(min_cost[p2], p2));
        }
      }
    }
    answer = min_cost[d - 1];
    for (int ci = 1; ci <= c; ++ci) {
      answer = min(answer, min_cost[d - 1 + n * ci]);
    }
    cout << answer << endl;
  }
}