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
}

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)
		for len(pq) != 0 {
			now := heap.Pop(&pq).(*Node)
			if now.NowPosition == d {
				fmt.Println(now.Amount)
				break
			}
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				heap.Push(&pq, next)
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					heap.Push(&pq, next)
				}
			}
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };



int c, n, m, s, d;
int dp[ 11 ][ 101 ];
static VVI _root;

uint solve( int now, int can_use, set<int> p ) {
	p.insert( now );
	if ( now == d ) {
		return 0;
	}
	if ( dp[ can_use ][ now ] != -1 ) {
		return dp[ can_use ][ now ];
	}
	uint res = 1 << 30;
	for ( int i = 0; i < n; i++ ) {
		if ( p.count( i ) || _root[ now ][ i ] == INF ) {
			continue;
		}
		res = min( res, solve( i, can_use, p ) + _root[ now ][ i ] );
		if ( can_use ) {
			res = min( res, solve( i, can_use - 1, p ) + _root[ now ][ i ] / 2 );
		}
	}
	return dp[ now ][ can_use ] = res;
}

int main() {
	while ( ~scanf("%d %d %d %d %d", &c, &n, &m, &s, &d) && n && m ) {
		_root.resize( n, VI( n ) );
		--s;
		--d;
		for ( int i = 0; i < n; i++ ) {
			for ( int j = 0; j < n; j++ ) {
				_root[ i ][ j ] = INF;
				if ( i == j ) {
					_root[ i ][ j ] = 0;
				}
			}
		}
		for ( int i = 0; i < m; i++ ) {
			int a, b, f;
			scanf( "%d %d %d", &a, &b, &f );
			--a;
			--b;
			_root[ a ][ b ] = _root[ b ][ a ] = f;
		}
		NCLR( dp );
		uint ans = solve( s, c, set<int>() );
		printf( "%d\n", ans );
		_root.clear();
	}
	return 0;
}

C++: #include <cstdio>
#include <queue>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int,int> _P;
typedef pair<_P,int> P;
int main() {
	int c,n,m,s,d;
	int a,b,f;
	int i,ans;
	int cost1[100][100],cost2[100][11];
	priority_queue<P,vector<P>,greater<P> > que;
	while(scanf("%d%d%d%d%d",&c,&n,&m,&s,&d),c) {
		fill(cost1[0],cost1[0]+100*100,1<<30);
		fill(cost2[0],cost2[0]+100*11,1<<30);
		for(i=0;i<m;i++) {
			scanf("%d%d%d",&a,&b,&f);
			cost1[a-1][b-1]=cost1[b-1][a-1]=min(cost1[a-1][b-1],f);
		}
		que.push(P(_P(s-1,0),0));
		cost2[s-1][0]=0;
		while(!que.empty()) {
			P p=que.top();que.pop();
			if(cost2[p.first.first][p.second]<p.first.second) break;
			for(i=0;i<n;i++) {
				if(p.first.second+cost1[p.first.first][i]<cost2[i][p.second]) {
					cost2[i][p.second]=p.first.second+cost1[p.first.first][i];
					que.push(P(_P(i,cost2[i][p.second]),p.second));
				}
				if(p.second+1<=m&&(p.first.second+cost1[p.first.first][i]/2)<cost2[i][p.second+1]) {
					cost2[i][p.second+1]=p.first.second+cost1[p.first.first][i]/2;
					que.push(P(_P(i,cost2[i][p.second+1]),p.second+1));
				}
			}
		}
		ans=1<<30;
		for(i=0;i<=m;i++) if(ans>cost2[d-1][i]) ans=cost2[d-1][i];
		printf("%d\n",ans);
	}
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#define INF 1000010;

using namespace std;


int main(void){
  long c,n,m,s,d;
  while(true){
    cin>>c>>n>>m>>s>>d;
    if(c==0 and n==0 and m==0 and s==0 and d==0)break;
    d--;
    s--;

    long a,b,f;
    map<long,long> costs[n];
    for(int i=0;i<m;i++){
      cin>>a>>b>>f;
      a--;
      b--;
      costs[a][b]=f;
      costs[b][a]=f;
    }
    map<long,long> dist[n];
    for(long i=0;i<n;i++){
      for(long j=0;j<=c;j++){
        dist[i][j]=INF;
      }
    }
    dist[s][0]=0;

    vector<long> next,_next;
    next.push_back(s);
    while(next.empty()==false){
      _next.clear();
      for(auto iter=next.begin();iter!=next.end();iter++){
        long city=*iter;
        for(auto iter2=costs[city].begin();iter2!=costs[city].end();iter2++){
          long next_city=iter2->first;
          long fee=iter2->second;
          for(long ticket=0;ticket<=c;ticket++){
            if(dist[next_city][ticket]>dist[city][ticket]+fee){
              dist[next_city][ticket]=dist[city][ticket]+fee;
              _next.push_back(next_city);
            }
          }
          for(long ticket=1;ticket<=c;ticket++){
            if(dist[next_city][ticket]>dist[city][ticket-1]+fee/2){
              dist[next_city][ticket]=dist[city][ticket-1]+fee/2;
              _next.push_back(next_city);
            }
          }
        }
      }

      sort(_next.begin(),_next.end());
      _next.erase(unique(_next.begin(),_next.end()),_next.end());
      next.clear();
      copy(_next.begin(),_next.end(),back_inserter(next));

    }
    long retval=INF;
    for(long ticket=0;ticket<=c;ticket++){
        retval=min(dist[d][ticket],retval);
    }
    cout<<retval<<endl;
  }
}