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 <vector>
#include <algorithm>
#include <functional>
#include <queue>
#include <numeric>

using namespace std;

struct edge{ int to, cost; };
typedef pair<int, int> P;
const int INF = INT_MAX / 4;

int V;
vector<edge> G[100];
int D[100];
int pre[100];

int c, n, m, s, d;

void dijkstra(int s)
{
	priority_queue<P, vector<P>, greater<P> > que;
	fill(D, D + 100, INF);
	fill(pre, pre + 100, -1);
	D[s] = 0;
	que.push(P(0, s));

	while (!que.empty())
	{
		P p = que.top(); que.pop();
		int v = p.second;
		if (D[v] < p.first) continue;
		for (int i = 0; i < G[v].size(); i++)
		{
			edge e = G[v][i];
			if(D[e.to] > D[v] + e.cost)
			{
				D[e.to] = D[v] + e.cost;
				pre[e.to] = v;
				que.push(P(D[e.to], e.to));
			}
		}
	}
}

int solve()
{
	dijkstra(s - 1);
	vector<int> vec;
	for (int t = d - 1; pre[t] != -1 ; t = pre[t])
	{
		for (int i = 0; i < G[t].size(); i++)
		{
			if(pre[t] == G[t][i].to)
			{
				vec.push_back(G[t][i].cost);
				break;
			}
		}
	}
	sort(vec.begin(), vec.end(), greater<int>());
	for (int i = 0; i < c && i < vec.size(); i++)
	{
		vec[i] = vec[i] / 2;
	}
	return accumulate(vec.begin(), vec.end(), 0);
}

int main()
{
	while (cin >> c >> n >> m >> s >> d && c && n && m && s && d)
	{
		for (int i = 0; i < 100; i++)
		{
			G[i] = vector<edge>();
		}
		for (int i = 0; i < m; i++)
		{
			int a, b, f;
			cin >> a >> b >> f;
			edge e;
			e.to = b - 1;
			e.cost = f;
			G[a - 1].push_back(e);
			e.to = a - 1;
			e.cost = f;
			G[b - 1].push_back(e);
		}
		cout << solve() << endl;
	}
	return 0;
}

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

using namespace std;

#define INF 100000000

typedef pair<int, int> P;

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;
		}
		vector< vector<int> > ff(n+1, vector<int>(n+1, INF));
		vector< vector<int> > ddd(n+1, vector<int>(n+1, INF));
		int a, b, f;
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> f;
			ff[a][b] = ff[b][a] = f;
			ddd[a][b] = ddd[b][a] = f;
		}
		for (int i = 1; i <= n; i++) {
			ddd[i][i] = 0;
		}
		for (int k = 1; k <= n; k++) {
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= n; j++) {
					ddd[i][j] = min(ddd[i][j], ddd[i][k]+ddd[k][j]);
				}
			}
		}
		vector< vector< vector<int> > > dp(n+1, vector< vector<int> >(n+1, vector<int>(c+1, INF)));
		for (int i = 1; i <= n; i++) {
			dp[s][i][0] = ddd[s][i];
			dp[i][d][0] = ddd[i][d];
		}
		set<P> st[101][101][11];
		for (int k = 1; k <= c; k++) {
			for (int i = 1; i <= n; i++) {
				dp[s][i][k] = dp[s][i][k-1];
				dp[i][d][k] = dp[i][d][k-1];
				st[s][i][k] = st[s][i][k-1];
				st[i][d][k] = st[i][d][k-1];
				int ka, kb, kc, kd;
				ka = kb = kc = kd = 0;
				for (int j = 1; j <= n; j++) {
					for (int p = 1; p <= n; p++) {
						if (ff[j][p] != INF && st[s][i][k].find(P(j, p)) == st[s][i][k].end()) {
							if (dp[s][i][k] > dp[s][j][k-1]+(ff[j][p]/2)+ddd[p][i]) {
								dp[s][i][k] = dp[s][j][k-1]+(ff[j][p]/2)+ddd[p][i];
								ka = j;
								kb = p;
							}
						}
						if (ff[j][p] != INF && st[i][d][k].find(P(j, p)) == st[i][d][k].end()) {
							if (dp[i][d][k] > dp[i][j][k-1]+(ff[j][p]/2)+ddd[p][d]) {
								dp[i][d][k] = dp[i][j][k-1]+(ff[j][p]/2)+ddd[p][d];
								kc = j;
								kc = p;
							}
						}
					}
				}
				if (ka != 0) {
					st[s][i][k].insert(P(ka, kb));
				}
				if (kc != 0) {
					st[i][d][k].insert(P(kc, kd));
				}
			}
		}
		cout << dp[s][d][c] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
 
#define MAX 10000000
#define n_MAX 101
#define c_MAX 11

typedef pair<int, pair<int, int> > edge;

inline int get_cost(edge e){
  return e.first;
}

inline int get_tickets(edge e){
  return -(e.second.first);
}

inline int get_station(edge e){
  return e.second.second;
}

inline edge make_edge(int cost, int tickets, int station){
  return make_pair(cost, make_pair(-tickets, station));
}

int main() {
 
  while( true ) {
    
    long long int c, n, m, s, d;
    cin >> c >> n >> m >> s >> d;
    if ( n == 0 ) break;
    
    long long int l[n_MAX][n_MAX] = {0};
    
    for ( long long int i = 0; i < m; i++ ) {
      long long int a, b, f;
      cin >> a >> b >> f;
      l[a][b] = l[b][a] = f;
    }
 
    long long int t[c_MAX][n_MAX];
    for ( long long int j = 0; j <= c; j++ ) {
      for ( long long int i = 1; i <= n; i++ ) {
	t[j][i] = MAX;
      }
    }
    
    priority_queue<edge, vector<edge>, less<edge> > queue;
    queue.push(make_edge(0, c, s));
    t[c][s] = 0;

    int ans = MAX;
    while(!queue.empty()){
      edge e = queue.top(); queue.pop();

      const int cost = get_cost(e);
      const int tickets = get_tickets(e);
      const int station = get_station(e);
      if(t[tickets][station] > cost){
	continue;
      }else if(station == d){
	ans = min(ans, cost);
      }
      

      for(long long int next = 1; next <= n; next++){
	if(l[station][next] == 0){
	  continue;
	}
	
	const int next_non_use_cost = cost + l[station][next];
	const int next_use_cost = cost + l[station][next] / 2;
 
	if(tickets > 0 && t[tickets - 1][next] > next_use_cost){
	  t[tickets - 1][next] = next_use_cost;
	  queue.push(make_edge(next_use_cost, tickets - 1, next));
	}
	
	if(t[tickets][next] > next_non_use_cost){
	  t[tickets][next] = next_non_use_cost;
	  queue.push(make_edge(next_non_use_cost, tickets, next));
	}
      }
    }
 
    cout << ans << endl;
  }
 
  return 0;
 
}