Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

var pass [][]int

func main() {
	for scanner.Scan() {
		pass = make([][]int, 200)
		for i := range pass {
			pass[i] = make([]int, 200)
		}
		buf := strings.Split(scanner.Text(), " ")
		c, _ := strconv.Atoi(buf[0])
		n, _ := strconv.Atoi(buf[1])
		m, _ := strconv.Atoi(buf[2])
		s, _ := strconv.Atoi(buf[3])
		d, _ := strconv.Atoi(buf[4])
		if c == 0 && n == 0 && m == 0 && s == 0 && d == 0 {
			break
		}
		s--
		d--
		for i := 0; i < 200; i++ {
			for j := 0; j < 200; j++ {
				pass[i][j] = INF
			}
		}
		for i := 0; i < 200; i++ {
			pass[i][i] = 0
		}
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			a, _ := strconv.Atoi(buf_[0])
			b, _ := strconv.Atoi(buf_[1])
			f, _ := strconv.Atoi(buf_[2])
			a--
			b--
			pass[a][b] = f
			pass[b][a] = f
		}
		fmt.Println(dijkstra(s, d, c, n))
	}
}

func dijkstra(s, g, c, n int) float64 {
	d := make([][]float64, 200)
	used := make([][]bool, 200)
	for i := 0; i < 200; i++ {
		d[i] = make([]float64, 20)
		used[i] = make([]bool, 20)
	}
	for i := range d {
		for j := range d[i] {
			d[i][j] = INF
		}
	}
	d[s][c] = 0
	for {
		v := -1
		w := -1
		for u := 0; u < n; u++ {
			for i := 0; i < c+1; i++ {
				if !used[u][i] && (v == -1 || d[u][i] < d[v][w]) {
					v = u
					w = i
				}
			}
		}
		if v == -1 {
			break
		}
		used[v][w] = true
		for u := 0; u < n; u++ {
			d[u][w] = math.Min(d[u][w], d[v][w]+float64(pass[v][u]))
			if w-1 >= 0 && pass[v][u] != INF {
				d[u][w-1] = math.Min(d[u][w-1], d[v][w]+(float64(pass[v][u]/2.0)))
			}
		}
	}
	ret := float64(INF)
	for i := 0; i < c+1; i++ {
		ret = math.Min(ret, d[g][i])
	}
	return ret
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)

const int INF = 1 << 24;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};

typedef unsigned long long ull;

struct Edge{
    int to, cost;
};

int C, N, M, S, D;
int cost[110][110];
int d[110][20], used[110][20];

void dijkstra(){
    REP(i, 110){
        REP(j, 20){
            d[i][j] = INF;
            used[i][j] = 0;
        }
    }

    d[S][0] = 0;

    while(1){
        int u = -1, v = -1;
        REP(i, N){
            REP(j, C+1){
                if(!used[i][j] && (u == -1 || d[u][v] > d[i][j])){u = i; v = j;}
            }
        }

        if(u == -1){break;}
        used[u][v] = 1;

        REP(i, N){
            if(v+1 <= C){
                d[i][v+1] = std::min(d[i][v+1], d[u][v] + cost[u][i] / 2);
            }
            d[i][v] = std::min(d[i][v], d[u][v] + cost[u][i]);
        }
    }
}

int main(){
    while(std::cin >> C >> N >> M >> S >> D, C){
        S--; D--;

        REP(i, 110){
            REP(j, 110){
                cost[i][j] = INF;
            }
        }

        REP(i, M){
            int from, to, co;
            std::cin >> from >> to >> co;
            from--; to--;
        
            cost[from][to] = co;
            cost[to][from] = co;
        }

        dijkstra();

        int res = INF;
        REP(i, C+1){
            res = std::min(res, d[D][i]);
        }

        std::cout << res << std::endl;
    }
}

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;
    int stats[ C + 1 ][ N ];
    int res;
    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 に対してダイクストラ法を適用 */
    for ( i = 0; i <= c; ++i )
    for ( j = 0; j < n; ++j )
      stats[ i ][ j ] = INF;
    lis.push ( stat ( s, 0, c ) );
    while ( !lis.empty ( ) )
    {
      const stat st = lis.top ( );
      lis.pop ( );

      /* 既に探索済みであれば処理しない */
      if ( stats[ st.rem ][ st.pos ] != INF )
        continue ;
      stats[ st.rem ][ 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 ) );
      }
    }

    /* s->d への最小費用を算出 */
    res = INF;
    for ( i = 0; i <= c; ++i )
      res = min ( res, stats[ i ][ d ] );
    printf ( "%d\n", res );
  }

  return ( EXIT_SUCCESS );
}

C++: #include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct P {
	int from, cost, coupon;

	bool operator >(const P& p) const {
		return cost > p.cost;
	}
};

int ecost[101][101];
int ccost[101][11];
int c, n, m, s, d;
int i;

int main() {
	while(cin >> c >> n >> m >> s >> d, c|n|m|s|d) {
		fill_n((int *)ecost, 101*101, -1);
		fill_n((int *)ccost, 101*11, 1e8);
		for(i=0; i<m; i++){
			int a, b, f;
			cin >> a >> b >> f;
			ecost[a][b] = f;
			ecost[b][a] = f;
		}
		priority_queue<P, vector<P>, greater<P>> q;
		q.push((P){s, 0, 0});
		int dcost;
		while(!q.empty()){
			P p = q.top(); q.pop();
			// goal
			if(p.from == d) {
				dcost = p.cost;
				break;
			}
			//
			for(i=0; i<n+1; i++){ // i:to
				if(ecost[p.from][i] != -1){
					if(p.cost + ecost[p.from][i] < ccost[i][p.coupon]){
						q.push((P){i, p.cost + ecost[p.from][i], p.coupon});
					}
					if(p.coupon+1 <= c) {
						if(p.cost + ecost[p.from][i]/2 < ccost[i][p.coupon+1]){
							q.push((P){i, p.cost + ecost[p.from][i]/2, p.coupon+1});
						}
					}
				}
			}
		}
		cout << dcost << endl;
	}
	return 0;
}