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]int)
		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}] = now.Amount
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				if memoAmount, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok || (ok && memoAmount > next.Amount) {
					memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}] = next.Amount
					heap.Push(&pq, next)
				}
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					if memoAmount, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok || (ok && memoAmount > next.Amount) {
						memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}] = next.Amount
						heap.Push(&pq, next)
					}
				}
			}
		}
		//runtime.GC()
	}
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define EACH( v, c ) for ( auto &v : c )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

struct Node
{
	int num, cost, tic;

	Node( int n, int c, int t ) : num( n ), cost( c ), tic( t )
	{
		return;
	}

	bool operator > ( const Node &a ) const
	{
		return a.cost < cost;
	}
};

struct Edge
{
	int to, cost;

	Edge( int t, int c ) : to( t ), cost( c )
	{
		return;
	}
};

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	while ( true )
	{
		int c, n, m, s, d;
		cin >> c >> n >> m >> s >> d;

		if ( !( c | n | m | s | d ) )
		{
			break;
		}

		s--;
		d--;

		vector< vector< Edge > > G( n, vector< Edge >() );

		REP( i, 0, m )
		{
			int a, b, f;
			cin >> a >> b >> f;

			a--;
			b--;

			G[a].PB( Edge( b, f ) );
			G[b].PB( Edge( a, f ) );
		}

		VVI dist( n, VI( c + 1, INT_MAX ) );

		priority_queue< Node, vector< Node >, greater< Node > > que;
		que.push( Node( s, 0, 0 ) );

		while ( !que.empty() )
		{
			Node cur = que.top();
			que.pop();

			if ( c < cur.tic || dist[ cur.num ][ cur.tic ] <= cur.cost )
			{
				continue;
			}

			dist[ cur.num ][ cur.tic ] = cur.cost;

			REP( i, 0, G[ cur.num ].size() )
			{
				que.push( Node( G[ cur.num ][i].to, cur.cost + G[ cur.num ][i].cost, cur.tic ) );
				que.push( Node( G[ cur.num ][i].to, cur.cost + G[ cur.num ][i].cost / 2, cur.tic + 1 ) );
			}
		}

		cout << *min_element( ALL( dist[d] ) ) << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <functional>
#include <queue>
using namespace std;

const int INF = 10000000;

typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef priority_queue<PP, vector<PP>, greater<PP> > Q;

int C, N, M, S, D;
int m[100][100];
int u[11][100];

bool input() {
  cin >> C >> N >> M >> S >> D;
  if (!C && !N && !M && !S && !D) {
    return false;
  }
  --S, --D;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      m[i][j] = INF;
    }
    m[i][i] = 0;
  }
  for (int i = 0; i < M; ++i) {
    int a, b, f;
    cin >> a >> b >> f;
    --a, --b;
    m[a][b] = f;
    m[b][a] = f;
  }
  return true;
}

void solve() {
  for (int k = 0; k <= C; ++k) {
    for (int i = 0; i < N; ++i) {
      u[k][i] = INF;
    }
  }
  Q q;
  q.push(PP(0, P(0, S)));
  int ans = INF;
  while (!q.empty()) {
    PP pp = q.top();
    q.pop();
    int f = pp.first;
    P p = pp.second;
    int k = p.first;
    int l = p.second;
    if (l == D) {
      ans = f;
      break;
    }
    if (u[k][l] != INF) {
      continue;
    }
    u[k][l] = f;
    for (int i = 0; i < N; ++i) {
      if (l == i || m[l][i] == INF) {
        continue;
      }
      q.push(PP(f + m[l][i], P(k, i)));
      if (k < C) {
        q.push(PP(f + m[l][i]/2, P(k + 1, i)));
      }
    }
  }
  cout << ans << endl;
}

int main() {
  while (input()) {
    solve();
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

struct Node {
    Node(int s, int t, int c) : s(s), t(t), c(c) {}

    int s, t, c;

    bool operator<(const Node &rhs) const {
        return c > rhs.c;
    }
};

int main() {
    int c, n, m, s, d;
    while (cin >> c >> n >> m >> s >> d && n) {
        --s, --d;
        vector<vector<Node> > g(n);

        for (int i = 0; i < m; i++) {
            int a, b, c;
            cin >> a >> b >> c;
            --a, --b;
            g[a].push_back(Node(b, 0, c));
            g[b].push_back(Node(a, 0, c));
            g[a].push_back(Node(b, 1, c / 2));
            g[b].push_back(Node(a, 1, c / 2));
        }
        priority_queue<Node> Q;
        Q.push(Node(s, c, 0));
        bool flg[111][11] = {};
        while (Q.size()) {
            Node q = Q.top();
            Q.pop();
            if (flg[q.s][q.t]) continue;
            if (q.s == d) {
                cout << q.c << endl;
                break;
            }
            flg[q.s][q.t] = true;
            for (auto e : g[q.s]) {
                if (q.t >= e.t) {
                    Q.push(Node(e.s, q.t - e.t, q.c + e.c));
                }
            }
        }
    }
}