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 <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;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
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, ans;
vector<vector<PII> > v;
int dp[ 11 ][ 100 ];
bool check[ 11 ][ 100 ];

struct Union_Find {
	vector<int> par;
	vector<int> rank;

	void init( int n ) {
		par.resize( n );
		rank.resize( n );
		for ( int i = 0; i < n; ++i ) {
			par[ i ] = i;
			rank[ i ] = 0;
		}
	}
	int find( int s ) {
		if ( par[ s ] == s ) {
			return s;
		}
		return par[ s ] = find( par[ s ] );
	}
	void unite( int x, int y ) {
		int a = find( x ), b = find( y );
		if ( a == b ) {
			return;
		}
		if ( rank[ x ] < rank[ y ] ) {
			par[ x ] = y;
		}
		else {
			par[ y ] = x;
			if ( rank[ x ] == rank[ y ] ) {
				++rank[ x ];
			}
		}
	}
	bool same( int x, int y ) {
		return find( x ) == find( y );
	}
	unsigned int size() {
		return par.size();
	}
};

int solve( int canUseT, int now, Union_Find u ) {
	if ( now == d ) {
		return 0;
	}
	if ( dp[ canUseT ][ now ] >= 0 ) {
		return dp[ canUseT ][ now ];
	}
	if ( check[ canUseT ][ now ] ) {
		return INF;
	}
	u.unite( s, now );
	int res = INF;
	for ( int i = 0; i < v[ now ].size(); i++ ) {
		if ( u.same( now, v[ now ][ i ].first ) ) {
			continue;
		}
		if ( canUseT ) {
			res = min( res, solve( canUseT - 1, v[ now ][ i ].first, u ) + v[ now ][ i ].second / 2 );
		}
		res = min( res, solve( canUseT, v[ now ][ i ].first, u ) + v[ now ][ i ].second );
	}
	return dp[ canUseT ][ now ] = res;
}

int main() {
	Union_Find uf;
	while ( ~scanf( "%d %d %d %d %d", &c, &n, &m, &s, &d ) && c && n && m && s && d ) {
		s--;
		d--;
		v.resize( n );
		uf.init( n );
		NCLR( dp );
		CLR( check );
		for ( int i = 0; i < m; i++ ) {
			int a, b, f;
			scanf( "%d %d %d", &a, &b, &f );
			v[ a - 1 ].push_back( MP( b - 1, f ) );
			v[ b - 1 ].push_back( MP( a - 1, f ) );
		}
		ans = solve( c, s, uf );
		printf( "%d\n", ans );
		v.clear();
	}
	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <queue>
using namespace std;

class State
{
public:
	int p,c,u;
	State(int p, int c, int u)
	:p(p),c(c),u(u)
	{}
	
	bool operator<(const State& s) const
	{
		return c>s.c;
	}
};

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

int main()
{
	int c,n,m,s,d;
	while(scanf("%d%d%d%d%d", &c,&n,&m,&s,&d), (c||n||m||s||d))
	{
		Node node[101];
		while(m--)
		{
			int a,b,f;
			scanf("%d%d%d", &a,&b,&f);
			node[a].push_back(make_pair(b,f));
			node[b].push_back(make_pair(a,f));
		}
		
		priority_queue<State> q;
		q.push(State(s,0,0));
		bool v[10][101]={0};
		
		while(!q.empty())
		{
			State S=q.top(); q.pop();
			if(S.p==d)
			{
				printf("%d\n", S.c);
				break;
			}
			if(v[S.u][S.p]) continue;
			v[S.u][S.p]=1;
			
			for(int i=0; i<node[S.p].size(); i++)
			{
				pair<int, int> next=node[S.p][i];
				if(!v[S.u][next.first]) q.push(State(next.first, S.c+next.second, S.u));
				if(S.u!=c)
				{
					if(!v[S.u+1][next.first]) q.push(State(next.first, S.c+next.second/2, S.u+1));
				}
			}
		}
	}
}

C++: //04
#include<iostream>
#include<algorithm>

using namespace std;

struct edge{
  int a,b,f;
  edge(int aa,int bb,int ff){
    a=aa;
    b=bb;
    f=ff;
  }
  edge(){};
};

int main(){
  for(int c,n,m,s,ds;cin>>c>>n>>m>>s>>ds,c|n|s|m|ds;){
    edge e[500];
    for(int i=0;i<m;i++){
      int a,b,f;
      cin>>a>>b>>f;
      e[i]=edge(a,b,f);
    }
    int d[101][11];
    fill_n(d[0],101*11,1<<29);
    d[s][c]=0;
    for(int l=0;l<n;l++){
      for(int i=0;i<m;i++){
	for(int j=0;j<2;j++){
	  int s=e[i].a,t=e[i].b;
	  if(j){
	    swap(s,t);
	  }
	  for(int k=0;k<=10;k++){
	    d[t][k]=min(d[t][k],d[s][k]+e[i].f);
	    if(k){
	      d[t][k-1]=min(d[t][k-1],d[s][k]+e[i].f/2);
	    }
	  }
	}
      }
    }
    cout<<*min_element(d[ds],d[ds+1])<<endl;
  }
  return 0;
}