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 <vector>
#include <algorithm>
#include <queue>
#define INF 1000000000;
#define dis first
#define mati second.first
#define sta  second.second
using namespace std;
typedef pair <int ,pair <int,int> > P;
vector <P> mp[102];
int c,n,m,start,goal;

int bit_count(int a) {
  a = (a & 0x55555555)+((a & 0xAAAAAAAA) >> 1 );
  a = (a & 0x33333333)+((a & 0xCCCCCCCC) >> 2 );
  a = (a & 0x0F0F0F0F)+((a & 0xF0F0F0F0) >> 4 );
  a = (a & 0x00FF00FF)+((a & 0xFF00FF00) >> 8 );
  a = (a & 0x0000FFFF)+((a & 0xFFFF0000) >> 16);
  return a;
}
int dijkstra() {
  int flg[11][102] = {},dis[11][102];
  for(int j=0;j<=c;j++)
    for(int i=0;i<=n;i++) dis[j][i] = -INF;

  for(int j=1;j<=n;j++)flg[j][start] = 1;

  priority_queue <P> Q;
  P t;
  t.dis = 0, t.mati = start, t.sta = 0;
  Q.push(t);

  while(!Q.empty()) {
    t = Q.top(),Q.pop();
    if(flg[t.sta][t.mati] == 1) continue;
    flg[t.sta][t.mati] = 1;
    for(int i=0;i<mp[t.mati].size();i++){
      int ndis = mp[t.mati][i].dis;
      int nmati = mp[t.mati][i].mati;
      int nsta = t.sta+1;
      P tt;
      if(dis[t.sta][nmati] < t.dis-ndis && flg[t.sta][nmati] == 0) {
	tt.dis = t.dis-ndis,tt.mati = nmati,tt.sta = t.sta;
	dis[t.sta][nmati] = t.dis-ndis;
	Q.push(tt);
      }
      if(nsta<= c&&dis[nsta][nmati]<t.dis-ndis/2 && flg[nsta][nmati]==0){
	tt.dis = t.dis - ndis/2 , tt.mati = nmati, tt.sta = nsta;
	dis[nsta][nmati] = t.dis-ndis/2;
	Q.push(tt);
      }
    }
  }

  int ans = -INF;
  for(int j=0;j<=c;j++)ans = max(ans,dis[j][goal]);
   
  return -ans;
}


int main() {

  while(1) {
    cin >> c >> n >> m >> start >> goal;
    if(c==0&&n==0&&m==0&&start==0&&goal==0) break;

    for(int i=0;i<=n;i++) mp[i].clear();
    for(int i=0;i<m;i++) {
      int a,b,f;
      cin >> a >> b >> f;
      P t;
      t.dis = f,  t.mati = b , t.sta = 0;
      mp[a].push_back(t);
      t.mati = a;
      mp[b].push_back(t);
    }

    cout << dijkstra() << endl;
  }

  return 0;
}

C++: #include <iostream>
#define INF 10000000
using namespace std;


struct edge{
	int from, to, cost;
};

int ans[1000];
int counter=0;
bool flag=true;

void solve(){
	int d[101][11];//地点と割引回数
	edge route[500];//路線
	int c,n,m,s,t;//割引回数,町数,路線数,出発駅,到着駅
	cin >> c>>n>>m>>s>>t;
	if(c==0) {flag=false; return;}
	
	for(int i=0;i<m;i++){
		cin >>route[i].from>>route[i].to>>route[i].cost;
	}
	
	for(int i=1;i<=n;i++){
		for(int j=0;j<c+1;j++){//割引回数は割引無しも必要
			d[i][j]=INF;
		}
	}
	
	d[s][0]=0;
	bool update=true;
	while(update){
		update=false;
		for(int i=0;i<m;i++){
			edge e=route[i];//見やすく
			for(int j=0;j<=c;j++){
				//まずは割引無し
				if(d[e.from][j]!=INF && d[e.to][j]>d[e.from][j]+e.cost){
					d[e.to][j]=d[e.from][j]+e.cost; update=true;
				}
				if(d[e.to][j]!=INF && d[e.from][j]>d[e.to][j]+e.cost){
					d[e.from][j]=d[e.to][j]+e.cost; update=true;
				}
				
			}
			
			for(int j=0;j<c;j++){
				if(d[e.from][j]!=INF && 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(d[e.to][j]!=INF && d[e.from][j+1]>d[e.to][j]+e.cost/2){
					d[e.from][j+1]=d[e.to][j]+e.cost/2; update=true;
				}
			}
		}
	}
	
	ans[counter]=d[t][c];
	counter++;
}

int main(){
	while(flag){
		solve();
	}
	for(int i=0;i<counter;i++){
		cout << ans[i]<<endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <cassert>
#include <algorithm>
using namespace std;
typedef pair<int, int> edge;
struct state{
  int cost, coupon, station;
  state(int cost_, int coupon_, int station_):cost(cost_), coupon(coupon_), station(station_){}
  bool operator< (const state &s) const{
    if(cost == s.cost){
      return coupon > s.coupon;
    }
    return cost > s.cost;
  }
};
int djikstra(vector<vector<edge> > graph, int start, int goal, int coupon){
  priority_queue<state> pq;
  pq.push(state(0, coupon, start));
  bool visited[coupon][graph.size()];
  fill_n((bool*)visited, (coupon+1) * graph.size(), false);
  while(!pq.empty()){
    state cur = pq.top(); pq.pop();
    if(visited[cur.coupon][cur.station]){
      continue;
    }
    if(cur.station == goal){
      return cur.cost;
    }
    visited[cur.coupon][cur.station] = true;
    for(int i = 0; i < graph[cur.station].size(); ++i){
      int to = graph[cur.station][i].first;
      int c = graph[cur.station][i].second;
      if(cur.coupon > 0){
        pq.push(state(cur.cost+c/2, cur.coupon-1, to));
      }
      pq.push(state(cur.cost+c, cur.coupon, to));
    }
  }
  //assert(false);
  return -1;
}
int main(){
  int c, n, m, s, d;
  while(true){    
    cin >> c >> n >> m >> s >> d;
    if((c|n|m|s|d) == 0){
      break;
    }
    cout << 0 << endl;
    continue;
    vector<vector<edge> > graph(n+1, vector<edge>());
    for(int i = 0; i < m; ++i){
      int a, b, f;
      cin >> a >> b >> f;
      graph[a].push_back(make_pair(b, f));
      graph[b].push_back(make_pair(a, f));
    }
    cout << djikstra(graph, s, d, c) << endl;
  }
  return 0;
}