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 <cstdio>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

struct Edge {
	int to;
	int ticket;
	int cost;
	Edge(int to, int cost, int ticket) : to(to), cost(cost), ticket(ticket) {}
	Edge() {}
};

bool operator < (const Edge &a, const Edge &b)
{
	return (a.cost > b.cost);
}

int main()
{
	int c, n, m, s, d;
	
	while (scanf("%d %d %d %d %d", &c, &n, &m, &s, &d) && c){
		
		vector<Edge> graph[128];
		
		for (int i = 0; i < m; i++){
			int f, t, cost;
			scanf("%d %d %d", &f, &t, &cost);
			graph[--f].push_back(Edge(--t, cost, 0));
			graph[t].push_back(Edge(f, cost, 0));
		}
		
		bool done[16][128];
		memset(done, false, sizeof(done));
		
		priority_queue<Edge> pq;
		
		pq.push(Edge(s - 1, 0, 0));
		
		int ans = -1;
		while (!pq.empty()){
			Edge t = pq.top(); pq.pop();
			
			if (done[t.ticket][t.to]) continue;
			done[t.ticket][t.to] = true;
			
			if (t.to == d - 1){
				if (~ans) ans = min(ans, t.cost);
				else ans = t.cost;
			}
			
			int v = t.to;
			for (int i = 0; i < graph[v].size(); i++){
				pq.push(Edge(graph[v][i].to, t.cost + graph[v][i].cost, t.ticket));
				if (t.ticket < c){
					pq.push(Edge(graph[v][i].to, t.cost + graph[v][i].cost / 2, t.ticket + 1));
				}
			}
		}
		
		printf("%d\n", ans);
	}
	
	return (0);
}

C++: #include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1e9;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={0, 1, 0, -1}, dy[4]={-1, 0, 1, 0};
using namespace std;
typedef long long ll;
//typedef pair<int, int> P;
struct P {
	int x, y, n;
	P(int n, int x, int y):n(n), x(x), y(y){}
	P(){}
};

/** Problem0212 : Highway Express Bus **/
struct edge{
	int from, to, cost;
	edge(int f, int t, int c):from(f), to(t), cost(c){}
	edge(){}
};
vector<edge> G;

int main()
{
	int C, N, M, S, D;
	while (cin>>C>>N>>M>>S>>D, C) {
		S--, D--;
		G.clear();
		
		rep(i, M) {
			int a, b, f;
			cin>>a>>b>>f;
			a--, b--;
			G.push_back(edge(a, b, f));
			G.push_back(edge(b, a, f));
		}
		
		int d[100][11];
		fill(d[0], d[0]+1000, INF);
		for (int i=0; i<C+1; i++) d[S][i]=0;
		
		while (true) {
			bool update=false;
			
			for (int i=0; i<G.size(); i++) {
				edge e = G[i];
				for (int j=0; j<=C+1; j++) {
					if (d[e.to][j] > d[e.from][j] + e.cost) {
						d[e.to][j] = d[e.from][j] + e.cost;
						update = true;
					}
					
					
					if (j+1<=C) {
						if (d[e.to][j+1] > d[e.from][j]+e.cost/2) {
							d[e.to][j+1] = d[e.from][j]+e.cost/2;
							update = true;
						}
					}
				}
			}
			
			if (!update) break;
		}
		
		int ans = INF;
		for (int i=0; i<C+1; i++) {
			ans = min(ans, d[D][i]);
		}
		cout << ans << endl;
	}
}

C++: #include<algorithm>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  unsigned i,j;
  unsigned a[100][100],b[100][11],c[100][11],m,n,o,u,v;
  pair<pair<unsigned,unsigned>,unsigned> mn;
  while(cin>>m>>n>>o>>u>>v&&m+n+o+u+v){
    u--;
    v--;
    memset(a,-1,sizeof(a));
    for(i=0;i<o;i++){
      int p,q,r;
      cin>>p>>q>>r;
      p--;
      q--;
      a[p][q]=a[q][p]=r;
    }
    memset(b,0,sizeof(b));
    memset(c,-1,sizeof(c));
    c[u][0]=0;
    mn.first.first=u;
    mn.first.second=0;
    mn.second=0;
    while(mn.first.first!=v){
      b[mn.first.first][mn.first.second]=1;
      for(i=0;i<n;i++){
	if((int)a[mn.first.first][i]!=-1){
	  c[i][mn.first.second]=min(c[i][mn.first.second],mn.second+a[mn.first.first][i]);
	  if(mn.first.second<m)
	    c[i][mn.first.second+1]=min(c[i][mn.first.second+1],mn.second+a[mn.first.first][i]/2);
	}
      }
      mn.second=-1;
      for(i=0;i<m+1;i++){
	for(j=0;j<n;j++){
	  if(b[j][i]==0&&mn.second>c[j][i]){
	    mn.first.first=j;
	    mn.first.second=i;
	    mn.second=c[j][i];
	  }
	}
      }
    }
    cout<<mn.second<<endl;
  }
  return 0;
}