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 <bits/stdc++.h>
#define INF (1<<29)
#define all(c) (c).begin(),(c).end()
#define pb(c) push_back(c)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int dis[111][111];
int c, n, m, s, d;
int next[111][111];

void dfs(int cur, int sum, int cost){
  if(next[cur][sum] < cost) return;
  next[cur][sum] = cost;
  for(int i = 0; i < n; i++){
    if(dis[cur][i] == (1<<29)) continue;
    if(sum > 0) dfs(i, sum-1, cost+(dis[cur][i]/2));
    dfs(i, sum, cost+dis[cur][i]);
  }
}

int main(){
  ios::sync_with_stdio(false);

  while(cin >> c >> n >> m >> s >> d && (c||n||m||s||d)){

    for(int i = 0; i < 111; i++){
      for(int j = 0; j < 111; j++){
        dis[i][j] = next[i][j] = (1<<29);
      }
    }

    for(int i = 0; i < m; i++){
      int a, b, f;
      cin >> a >> b >> f;
      a--; b--;
      dis[a][b] = f;
      dis[b][a] = f;
    }

    dfs(s-1,c,0);
    int ans = (1<<29);
    for(int i =0; i < c; i++){
      ans = min(ans, next[d-1][i]);
    }
    cout << ans << endl;
  }


}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <functional>

constexpr int MAX_N=100,INF=(int)1e9;

struct edge {
	int to,cost;
	edge()=default;
	edge(int _t,int _c) : to(_t),cost(_c) {}
};

struct Node{
	int v,num,cost;
	Node()=default;
	Node(int _v,int _n,int _c) : v(_v),num(_n),cost(_c) {}

	bool operator<(const Node& rhs) const{
		return cost==rhs.cost ? v < rhs.v : cost < rhs.cost; 
	}
	bool operator>(const Node& rhs) const{
		return cost==rhs.cost ? v > rhs.v : cost > rhs.cost;
	}
};

int c,n,m,s,g;

std::vector<edge> G[MAX_N];

int d[11][MAX_N];

int dijkstra() {

	for(int i=0;i<=c;++i){
		for(int j=0;j<n;++j)
			d[i][j]=INF;
	}

	std::priority_queue<Node,std::vector<Node>,std::greater<Node>> que;

	d[c][s]=0;
	que.emplace(s,c,0);

	while(!que.empty()){

		Node node=que.top();
		que.pop();

		int v=node.v;

		if(v==g)
			return d[node.num][g];

		if(node.cost>d[node.num][v])
			continue;

		for(int i=0;i<G[v].size();++i){
			edge e=G[v][i];
			if(node.num==0)
				goto a;
			else{
				if(d[node.num-1][e.to]>d[node.num][v]+e.cost/2){
					d[node.num-1][e.to]=d[node.num][v]+e.cost/2;
					que.emplace(e.to,node.num-1,d[node.num-1][e.to]);
				}
				a:;
				if(d[node.num][e.to]>d[node.num][v]+e.cost){
					d[node.num][e.to]=d[node.num][v]+e.cost;
					que.emplace(e.to,node.num,d[node.num][e.to]);
				}
			}
		}
	}
	return -1;
}

int main() {
	while(1){
		std::cin>>c>>n>>m>>s>>g;
		if(c+n+m+s+g==0)
			return 0;
		--s;
		--g;
		int a,b,f;
		for(int i=0;i<m;++i){
			std::cin>>a>>b>>f;
			--a;
			--b;
			G[a].emplace_back(b,f);
			G[b].emplace_back(a,f);
		}
		int ans=dijkstra();
		std::cout<<ans<<std::endl;
		for(int i=0;i<n;++i)
			G[i].clear();
	}
	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++){
	//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;
}