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>
using namespace std;

const int INF = 1000000000;

struct Edge{
  int to,cost;
};

struct Node{
  int val,num;
};

struct P{
  int val,num,c;
  bool operator<(const P &r)const{
    return val < r.val;
  }
};




int main(){
  int c,n,m,s,d;
  
  while(1){
  cin >> c >> n >> m >> s >> d;
  s--;d--;
  if(c == 0 && n == 0 && m == 0 && s == -1 && d == -1)break;
  vector<Edge>edge[105];
  Node node[11][105];
  
  priority_queue<P> que;
  for(int i = 0; i < m; i++){
    int a,b,c;
    cin >> a >> b >> c;
    a--;b--;
    edge[a].push_back((Edge){b,c});
    edge[b].push_back((Edge){a,c});
  }


  for(int i = 0; i < 11; i++){
    for(int j = 0; j < n; j++){
      node[i][j].val = INF;
      node[i][j].num = j;
    }
  }

  node[c][s].val = 0;
  
  que.push((P){0,s,c});

  while(!que.empty()){
    int val = que.top().val;
    int now = que.top().num;
    int ticket = que.top().c;
    que.pop();
  
    if(ticket){
      for(int i = 0; i < edge[now].size(); i++){
	int next = edge[now][i].to;
	int cost = edge[now][i].cost;

	if(node[ticket - 1][next].val > node[ticket][now].val + cost / 2){
	  node[ticket - 1][next].val = node[ticket][now].val + cost / 2;
	  que.push((P){node[ticket - 1][next].val,next,ticket - 1});
	}
      } 
    }

    for(int i = 0; i < edge[now].size(); i++){
      int next = edge[now][i].to;
      int cost = edge[now][i].cost;
      
      if(node[ticket][next].val > node[ticket][now].val + cost){
	node[ticket][next].val = node[ticket][now].val + cost;
	que.push((P){node[ticket][next].val,next,ticket});
      }
    }
  }
  
  int ans = INF;

  for(int i = 0; i < c; i++){
    ans = min(ans,node[i][d].val);
  }

  cout << ans << endl;
  }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
#define MAX 101
#define INF 9999999

int c,n,m,s,d,a,b,f,cost[MAX+1][MAX+1],dd[MAX+1][MAX+1];
bool used[MAX+1][MAX+1];

int dijkstra(int s,int d,int c){
    fill(dd[0],dd[MAX],INF);
    fill(used[0],used[MAX],false);
    
    dd[c][s-1] = 0;
    while(true){
        int Mini = -1 , Minj;
        
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j <= c ; j++){
                if((dd[j][i] < dd[Minj][Mini] || Mini == -1)&&!used[j][i]){
                    Mini = i;
                    Minj = j;
                }
            }
        }
        
        if(Mini == -1) break;
        used[Minj][Mini] = true;
        
        for(int i = 0 ; i < n ; i++){
            dd[Minj][i] = min(dd[Minj][i],dd[Minj][Mini]+cost[Mini][i]);
            if(Minj > 0) dd[Minj-1][i] = min(dd[Minj-1][i],dd[Minj][Mini]+cost[Mini][i]/2);
        }
    }
    
    int res = INF;
    for(int i = 0 ; i <= c ; i++){
        res = min(res,dd[i][d-1]);
    }
    
    return res;
}

int main(){
    while(cin >> c >> n >> m >> s >> d, c+n+m+s+d){
        fill(cost[0],cost[MAX],INF);
        for(int i = 0 ; i < m ; i++){
            cin >> a >> b >> f;
            cost[a-1][b-1] = f;
            cost[b-1][a-1] = f;
        }
        
        cout << dijkstra(s,d,c) << endl;
    }
    
    return 0;
}

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

class State{
public:
  int v, c, t;
  State(int _v, int  _c, int _t)
    :v(_v), c(_c), t(_t) {}
  bool operator < (const State& st) const{
    return this->c > st.c;
  }
};


int main(){

  const int INF = 1 << 27;
 
  int ticket, n, m, start, goal;
  while(cin >> ticket >> n >> m >> start >> goal){
    if((ticket|n|m|start|goal) == 0) break;
    --start;
    --goal;

    int s, dd, score;
    vector<vector<vector<int> > >cost(n, vector<vector<int> >(n, vector<int>(ticket +1, INF*2)));
    for(int i=0; i<m; i++){
      cin >> s >> dd >> score;
      --s;
      --dd;
      cost[s][dd][0] = cost[dd][s][0] = score;
      for(int j=1; j<ticket; j++) cost[s][dd][j] = cost[dd][s][j] = score;      
      //cout << cost[s][dd][0] << endl;
    }

    priority_queue<State> req;
    vector<vector<int> > d(n, vector<int>(ticket+1, INF));
    for(int i=0; i<=ticket; i++){
      d[start][i] = 0;
 
    }
    req.push(State(start, 0, ticket));

    while(! req.empty()){
      State st = req.top();
      req.pop();

      //cout << "v:" << st.v+1 <<" c:" << st.c << " t:" << st.t << endl;

      if(st.v == goal){
	   cout << st.c << endl;
	   break;
      }

      for(int i=0; i<n; i++){
	if(i != st.v){
	//use ticket
	if(st.t>0 && d[st.v][st.t] + cost[st.v][i][st.t-1]/2 < d[i][st.t-1]){
	  d[i][st.t-1] = st.c + cost[st.v][i][st.t-1]/2;
	  req.push(State(i, d[i][st.t-1], st.t-1));
	}
	//not use ticket
	if(d[st.v][st.t] + cost[st.v][i][st.t] < d[i][st.t]){
	  d[i][st.t] = st.c + cost[st.v][i][st.t];
	  req.push(State(i, d[i][st.t], st.t));
	}
	}
      }
	
      
    }
  }
  return 0;
}