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 <cstdlib>
#include <queue>
#include <vector>
#include <utility>
#include <functional>
using namespace std;

#define INF 19980725

/** 割引券の枚数 */
static int c;
#define C 10
/** 町の数 */
static int n;
#define N 100
/** 路線数 */
static int m;
#define M 500
/** 出発地 */
static int s;
/** 目的地 */
static int d;

/** 路線情報 */
static int map[ N ][ N ];

/** 各探索先における状態 */
struct stat
{
  /** 到達先の町 */
  int pos;
  /** 現時点までの費用 */
  int cost;
  /** 割引券の残枚数 */
  int rem;

  stat (
    )
  {
    this->cost = INF;
    this->rem  = 0;
  }
  stat (
    int pos,
    int cost,
    int rem
    )
  {
    this->pos  = pos;
    this->cost = cost;
    this->rem  = rem;
  }
  bool operator> (
    const stat &a
    )
  const
  {
    return ( cost > a.cost
          || ( cost == a.cost && rem < a.rem ) );
  }
};

int
main (
  int   argc,
  char *argv[ ]
  )
{
  for ( ; ; )
  {
    /** 探索キュー */
    priority_queue<stat, vector<stat>, greater<stat> > lis;
    /** s->k への最小費用res[ k ] */
    int res[ N ];
    int i, j;

    /* 基本情報読み込み */
    scanf ( "%d%d%d%d%d", &c, &n, &m, &s, &d );
    if ( !( c | n | m | s | d ) ) break ;
    --s; --d;

    /* 路線情報初期化 */
    for ( i = 0; i < n; ++i )
    for ( j = 0; j < n; ++j )
      map[ i ][ j ] = INF;

    /* 路線情報読み込み */
    for ( i = 0; i < m; ++i )
    {
      int a, b, f;

      scanf ( "%d%d%d", &a, &b, &f );
      --a; --b;
      map[ a ][ b ] =
      map[ b ][ a ] = f;
    }

    /* s->d に対してダイクストラ法を適用 */
    fill ( res, res + n, INF );
    lis.push ( stat ( s, 0, c ) );
    while ( !lis.empty ( ) )
    {
      const stat st = lis.top ( );
      lis.pop ( );

      /* 結果が既に求まっていれば処理しない */
      if ( res[ st.pos ] != INF )
        continue ;
      res[ st.pos ] = st.cost;

      for ( i = 0; i < n; ++i )
      {
        /* st.pos からi への経路が存在しなければ処理しない */
        if ( map[ st.pos ][ i ] == INF )
          continue ;

        /* 隣接する町の探索を予約する．割引券が使えればその探索も予約する． */
        lis.push ( stat ( i, st.cost + map[ st.pos ][ i ], st.rem ) );
        if ( st.rem > 0 )
          lis.push ( stat ( i, st.cost + map[ st.pos ][ i ] / 2, st.rem - 1 ) );
      }
    }
    printf ( "%d\n", res[ d ] );
  }

  return ( EXIT_SUCCESS );
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
typedef long long ll;
using namespace std;

int mindis[101], pre[101];
bool used[101];
int eCost[101][101];

int getPathSum(int c, int p, int g){
	vector<int> edge;
	edge.push_back(eCost[p][g]);
		
	int to = p;
	for(int from = pre[to]; from != 0; from = pre[from]){
		edge.push_back(eCost[from][to]);
		to = from;
	}

	sort(edge.begin(), edge.end());

	int idx = edge.size()-1;
	for(int i = 0; i < c; i++){
		edge[idx--] /= 2;
		if(idx < 0) break;
	}
	
	int ans = 0;
	for(int i = 0; i < edge.size(); i++){
		ans += edge[i];
	}

	return ans;
}

int main(){
	int c, n, m, s, d;
	while(cin >> c >> n >> m >> s >> d){
		if(c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break;

		fill(mindis, mindis+101, 1<<30);
		fill(pre, pre+101, 0);
		fill(used, used+101, false);
		for(int i = 0; i < 101; i++) fill(eCost[i], eCost[i]+101, 1<<30);

		for(int i = 0; i < m; i++){
			int a, b, cost;
			cin >> a >> b >> cost;
			eCost[a][b] = cost;
			eCost[b][a] = cost;
		}

		mindis[s] = 0;
		vector<int> goals;

		//最短経路(ダイクストラ)
		while(true){
			int v = -1;
			for(int i = 1; i <= n; i++){
				if(!used[i] && (v == -1 || mindis[v] > mindis[i])) v = i;
			}

			if(v == d) break;
			used[v] = true;

			for(int i = 1; i <= n; i++){
				if(mindis[i] > mindis[v]+eCost[v][i]){
					if(i == d){
						//次がゴールならここでチケットを適用した総和を出して最終的な結果にする
						//それをgoalsに格納していく
						goals.push_back(getPathSum(c, v, i));
					}else{
						mindis[i] = mindis[v]+eCost[v][i];
						pre[i] = v;
					}
				}
			}
		}

		int minc = 100000000;
		for(int i = 0; i < goals.size(); i++){
			if(minc > goals[i]) minc = goals[i];
		}

		cout << minc << endl;
	}
	return 0;
}

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

typedef pair<int, int> P;
typedef pair<P, int> State;

const int N = 100, INF = 1e8;

int edge[N][N];

int dijkstra(int n, int s, int d, int c){
	priority_queue<State, vector<State>, greater<State> > q;
	vector<int> visited(n, INF);
	q.push(make_pair(make_pair(0, s), c));
	while(!q.empty()){
		int c = q.top().first.first;
		int curr = q.top().first.second;
		int dc = q.top().second;
		q.pop();
		visited[curr]  = c;
		if(curr == d) return c;
        if(c > visited[curr]) continue;
		for(int i(0);i < n;++i){
			if(edge[curr][i] != INF){
				int nc = c + edge[curr][i];
				int next = i;
                q.push(make_pair(make_pair(nc, next), dc));
				nc = c + edge[curr][i] / 2;
				if(dc > 0){
					q.push(make_pair(make_pair(nc, next), dc - 1));
				}
			}
		}
	}
	return -1;
}

int main(){
	int c, n, m, s, d, a, b, f;
	while(cin >> c >> n >> m >> s >> d, c && n && m && s && d){
		for(int i(0);i < N;++i){
			for(int j(0);j < N;++j){
				edge[i][j] = INF;
			}
		}
		--s;
		--d;
		for(int i(0);i < m;++i){
			cin >> a >> b >> f;
			--a;
			--b;
			edge[a][b] = edge[b][a] = f;
		}
        cout << dijkstra(n, s, d, c) << endl;
	}
}