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]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<bits/stdc++.h>
#define F first
#define S second
#define INF 1<<28
using namespace std;
 
int c, n, s, d;
typedef pair < int, int > P;
typedef pair < int, P > PP;
vector < P > vec[111];
 
int solve(){
 
  int dist[111][11]; for(int i=0;i<111;i++)for(int j=0;j<11;j++) dist[i][j] = INF;
  priority_queue < PP, vector<PP>, greater<PP> > que;
 
  dist[s][c] = 0;
  que.push( PP(0, P(s, c)) ); //Now_cost, Now_node, Now_ticket
 
  while(!que.empty()){
    PP p = que.top(); que.pop();
    int cost = p.F, now = p.S.F, tic = p.S.S;
 
    //if(dist[now][tic] < cost) continue;
    for(int i=0;i<vec[now].size();i++){
      P to = vec[now][i];
      if(dist[to.S][tic] > dist[now][tic] + to.F){
	dist[to.S][tic] = dist[now][tic] + to.F;
	que.push( PP(dist[to.S][tic], P(to.S, tic)) );
      }
      if(tic && dist[to.S][tic-1] > dist[now][tic] + to.F/2){
	dist[to.S][tic-1] = dist[now][tic] + to.F/2;
	que.push( PP(dist[to.S][tic-1], P(to.S, tic-1)) );
      }
    }
  }
   
  int ans = INF;
  for(int i=0;i<=c;i++){ ans = min(ans, dist[d][i]); } //cout << ans << endl; }
  return dist[d][0];
}
 
int main(){
 
  int m, a, b, p;
 
   
  while(cin >> c >> n >> m >> s >> d){
    if((c|n|m|s|d) == 0) break;
    for(int i=0;i<111;i++) vec[i].clear();
    for(;m--;){
      cin >> a >> b >> p;
      vec[a].push_back(P(p, b)); //[now] P(cost, next);
      vec[b].push_back(P(p, a));
    }
     
    cout << solve() << endl;
  }
   
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

const int INF = 1 << 30;

struct edge {int to, cost; };

int V;
int d[100][100];

vector<edge> G[100];
priority_queue<P, vector<P>, greater<P> > que;

void dijkstra(int s,int a){
  for(int i = 0; i < V; i++) d[a][i] = INF;
  que.push(P(0, s));
  d[a][s] = 0;

  while(!que.empty()){
    P p = que.top(); que.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];
      int Min = (a == 0 ? d[a][v] + e.cost : min(d[a][v] + e.cost, d[a-1][v] + e.cost/2));
      if(d[a][e.to] > Min){
        d[a][e.to] = Min;
        que.push(P(d[a][e.to], e.to));
      }
    }
  }
}

int main(){
  int c,m,s,e,From,To,Cost;
  edge E;
  while(scanf("%d%d%d%d%d",&c,&V,&m,&s,&e)!=EOF,c|V|m|s--|e--){
    for(int i = 0; i < m; i++){
      scanf("%d%d%d",&From,&To,&Cost);

      From--,To--;
      E.to = From,E.cost = Cost;
      G[To].push_back(E);
      E.to = To;
      G[From].push_back(E);
    }

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

    printf("%d\n",d[c][e]);

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

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define INF (1<<20)
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> PP;
struct edge{ int to,cost;};
int C,N,M,S,D;
vector<edge> G[111];
int minc[111][11];
void solve(){
  memset(minc,-1,sizeof(minc));
  priority_queue< PP,vector<PP>,greater<PP> > q;
  q.push( PP(0,P(0,S)) );
  while(!q.empty()){
    PP p = q.top(); q.pop();
    int v=p.second.second;
    int m=p.second.first;
    if(minc[v][m]!=-1) continue;
    minc[v][m]=p.first;
    for(int i=0;i<G[v].size();i++){
      edge e=G[v][i];
      if(minc[e.to][m]==-1){
	q.push( PP(e.cost+p.first,P(m,e.to)) );
      }
      if(minc[e.to][m+1]==-1 && m<C){
	q.push( PP(e.cost/2+p.first,P(m+1,e.to)) );
      }
    }
  }
}

main(){
  while(1){
    scanf("%d %d %d %d %d",&C,&N,&M,&S,&D);
    if(C==0 && N==0 && M==0 && S==0 && D==0) break;
    for(int i=0;i<M;i++){
      int a,b,f;
      scanf("%d %d %d",&a,&b,&f);
      edge e;
      e.to=b;e.cost=f;
      G[a].push_back(e);
      e.to=a;
      G[b].push_back(e);
    }
    solve();
    int res=INF;
    for(int i=0;i<=C;i++){
      if(minc[D][i]==-1) continue;
      res=min(res,minc[D][i]);
    }
    printf("%d\n",res);
    for(int i=1;i<=N;i++)
      G[i].clear();
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;//cost, v, ticket
struct edge{
  int to, cost;
};

int main(int argc, char *argv[]){
  int c, n, m, s, d;
  while(std::cin >> c >> n >> m >> s >> d, n){
    s--;d--;
    vector<vector<edge> > g(n);
    for (int i = 0; i < m; i++) {
      edge e;
      int from;
      std::cin >> from >> e.to >> e.cost;
      e.to--;from--;
      g[from].push_back(e);
      g[e.to].push_back((edge){from, e.cost});
    }
    vector<vector<int> > dp(c + 1, vector<int>(n, 1e9));
    dp[0][s] = 0;
    priority_queue<piii, vector<piii>, greater<piii> > que;
    que.push(piii(0, pii(s, 0)));
    while(!que.empty()){
      int cost   = que.top().first;
      int v      = que.top().second.first;
      int ticket = que.top().second.second;
      que.pop();
      for (int i = 0; i < g[v].size(); i++) {
        edge e = g[v][i];
        if(dp[ticket][e.to] > dp[ticket][v] + e.cost){
          dp[ticket][e.to] = dp[ticket][v] + e.cost;
          que.push(piii(dp[ticket][e.to], pii(e.to, ticket)));
        }
        if(ticket + 1 <= c &&
           dp[ticket + 1][e.to] > dp[ticket][v] + e.cost/2){
          dp[ticket + 1][e.to] = dp[ticket][v] + e.cost/2;
          que.push(piii(dp[ticket + 1][e.to],
                        pii(e.to, ticket + 1)));
        }
      }
    }
    int ans = 1e9;
    for (int i = 0; i <= c; i++) {
      ans = min(ans, dp[i][d]);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}