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 <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;
}

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

using namespace std;
#define INF 99999999
typedef pair<int, int> P;
typedef pair<P, int> PP;
struct Edge
{
	int to;
	int cost;
};

void solve()
{
	int c, n, m, s, d;
	while(cin >> c >> n >> m >> s >> d, c || n || m || m || s)
	{
		vector<Edge> V[101];
		for(int i = 0; i < m; ++i)
		{
			int from, to, cost;
			cin >> from >> to >> cost;
			--from; --to;
			Edge e1 = {to, cost};
			Edge e2 = {from, cost};
			V[from].push_back(e1);
			V[to].push_back(e2);
		}
		vector< vector<int> > dist(n, vector<int>(c + 1));
		for(int i = 0; i < dist.size(); ++i)
		{
			for(int j = 0; j < dist[i].size(); ++j)
			{
				dist[i][j] = INF;
			}
		}
		priority_queue<PP, vector<PP>, greater<PP> > pq;
		--s;
		dist[s][0] = 0;
		pq.push(PP(P(0, s), 0));
		while(!pq.empty())
		{
			PP pp = pq.top();
			pq.pop();
			int v = pp.first.second;
			int ticket = pp.second;
			if(dist[v][ticket] < pp.first.first)
			{
				continue;
			}
			for(int i = 0; i < V[v].size(); ++i)
			{
				Edge e = V[v][i];
				if(dist[e.to][ticket] > pp.first.first + e.cost)
				{
					dist[e.to][ticket] = pp.first.first + e.cost;
					pq.push(PP(P(dist[e.to][ticket], e.to), ticket));
				}
				if(ticket < c && dist[e.to][ticket + 1] > pp.first.first + e.cost / 2)
				{
					dist[e.to][ticket + 1] = pp.first.first + e.cost / 2;
					pq.push(PP(P(dist[e.to][ticket + 1], e.to), ticket + 1));
				}
			}
		}
		int min_cost = INF;
		for(int i = 0; i <= c; ++i)
		{
			if(min_cost > dist[d - 1][i])
			{
				min_cost = dist[d - 1][i];
			}
		}
		cout << min_cost << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int INF = 1e9;

int C, N, M, S, T;

class Cl{
  public:
    int n, c, cost;
    Cl(){}
    Cl(int nn, int cc, int co) { n = nn; c = cc; cost = co; }
    bool operator < (const Cl &tmp) const { return cost > tmp.cost; }
};

int solve(vector< vector<P> > &v){
  priority_queue<Cl> open;
  open.push(Cl(S, C, 0));
  vector< vector<int> > closed(N, vector<int>(C + 1, INF));
  closed[S][C] = 0;
  while(!open.empty()){
    Cl cl = open.top(); open.pop();
    if(closed[cl.n][cl.c] < cl.cost) continue;
    REP(i, v[cl.n].size()){
      P nx = v[cl.n][i];
      if(closed[nx.first][cl.c] > cl.cost + nx.second){
        closed[nx.first][cl.c] = cl.cost + nx.second;
        open.push(Cl(nx.first, cl.c, cl.cost + nx.second));
      }
      if(cl.c > 0 && closed[nx.first][cl.c - 1] > cl.cost + nx.second / 2){
        closed[nx.first][cl.c - 1] = cl.cost + nx.second / 2;
        open.push(Cl(nx.first, cl.c - 1, cl.cost + nx.second / 2));
      }
    }
  }
  int res = INF;
  REP(i, C) res = min(res, closed[T][i]);
  return res;
}

int main() {
  while(cin >>C >>N >>M >>S >>T && C && N && M && S && T){
    --T; --S;
    vector< vector<P> > v(N);
    REP(i, M){
      int a, b, f; cin >>a >>b >>f;
      --a; --b;
      v[a].push_back(P(b, f));
      v[b].push_back(P(a, f));
    }
    cout <<solve(v) <<endl;
  }
  return 0;
}