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)
		}
		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)
		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 <functional>
#include <algorithm>
#include <map>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef pair<P, int> State;

const int N = 100, INF = 1e8;

int edge[N][N];

int dijkstra(int n, int s, int d, int c){
	priority_queue<State, vector<State>, greater<State> > q;
	int visited[N][11] = {}, res;
	for(int i(0);i < N;++i){
		for(int j(0);j <= 10;++j){
			visited[i][j] = INF;
		}
	}
	q.push(State(P(0, s), c));
	while(!q.empty()){
		int c = q.top().first.first;
		int curr = q.top().first.second;
		int dc = q.top().second;
		q.pop();
        if(c > visited[curr][dc]) continue;
		for(int i(0);i < n;++i){
			if(edge[curr][i] != INF){
				int nc = c + edge[curr][i];
				int next = i;
				if(visited[next][dc] > nc){
                    visited[next][dc] = nc;
					q.push(State(P(nc, next), dc));
				}
				nc = c + edge[curr][next] / 2;
				if(dc > 0 && visited[next][dc - 1] > nc){
                    visited[next][dc - 1] = nc;
					q.push(State(P(nc, next), dc - 1));
				}
			}
		}
	}
	res = INF;
	for(int i(0);i < n;++i){
		res = min(res, visited[d][i]);
	}
    return res;
}

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

C++: #include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <utility>
#include <climits>

using namespace std;

int c, n, m, s, d;

struct N {
  int cost, ticket, stop;
  bool operator < (const N &t)const {
    return cost > t.cost;
  }
  N(int cost, int ticket, int stop) :
    cost(cost), ticket(ticket), stop(stop){}
  void print() {
    printf("cost = %d, ticket = %d, stop = %d\n", cost, ticket, stop);
  }
};

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

edge G[100];
int visited[100][11];

const int MAX = INT_MAX;

int solve()
{
  for (int i = 0; i < 100; i++) {
    fill(visited[i], &visited[i][11], MAX);
  }
  priority_queue<N> Q;
  Q.push(N(0, c, s));
      
  //printf("ok\n");  
      
  while (true) {    
    N cur = Q.top();
    Q.pop();

    //cur.print();
    int cc = cur.cost, ct = cur.ticket, cs = cur.stop;
    if (cs == d) {
      return cc;
    }

    for (int i = 0; i < G[cs].size(); i++) {
      int t = G[cs][i].first, ec = G[cs][i].second;
      if (cc + ec < visited[t][ct]) {
        visited[t][ct] = cc + ec;
        Q.push(N(visited[t][ct], ct, t));
      }
      if (ct > 0 && cc + ec / 2 < visited[t][ct - 1]) {
        visited[t][ct - 1] = cc + ec / 2;
        Q.push(N(visited[t][ct - 1], ct - 1, t));
      }
    }
  }                
}

int main()
{
  while (scanf("%d %d %d %d %d ", &c, &n, &m, &s, &d) == 5) {
    if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) {
      break;
    }
    s--, d--;
    for (int i = 0; i < n; i++) {
      G[i].clear();
    }
    for (int i = 0; i < m; i++) {
      int a, b, cost;
      scanf("%d %d %d ", &a, &b, &cost);
      //printf("%d %d %d\n", a, b, cost);
      a--, b--;
      G[a].push_back(make_pair(b, cost));
      G[b].push_back(make_pair(a, cost));
    }
    /*for (int i = 0; i < n; i++) {
      for (int j = 0; j < G[i].size(); j++) {
        printf("%d %d: %d\n", i, G[i][j].first, G[i][j].second);
      }
      }*/
    printf("%d\n", solve());
  }
  return 0;
}

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][11];

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 < n; i++){
      for(int j = 0; j < n; 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 = INT_MAX;
    for(int i =0; i < c; i++){
      ans = min(ans, next[d-1][i]);
    }
    cout << ans << endl;
  }


}