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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

using namespace std;

int minimum,min_past_num,num_ticket,num_town,m,start,goal;


struct Info{
    Info(){
        current_town = 0;
        index = 0;
    }
    bool visited[102];
    int current_town,index,costs[102],visited_town[101];
};

struct Data{
	Data(int arg_to,int arg_cost){
		to = arg_to;
		cost = arg_cost;
	}
	int to,cost;
};

vector<Data> V[101];

void func(Info info){

    int sum = 0,limit;

    if(info.current_town == goal || minimum != 2000000000)
    {

    	limit = min(num_ticket,info.index);

    	for(int i = 0; i < limit; i++){
    		sum += info.costs[i]/2;
    	}

    	for(int i = limit; i < info.index; i++){
    		sum += info.costs[i];
    	}

		if(info.current_town == goal){
			minimum = min(minimum,sum);
			return;
		}else{
			if(sum >= minimum){
				return;
			}
		}

    }

    int calc;
    for(int i = 0; i < V[info.current_town].size(); i++){
        if(info.visited[V[info.current_town][i].to] == false){
            Info new_info;
            for(int k = 1; k <= num_town; k++)new_info.visited[k] = info.visited[k];
            new_info.visited[V[info.current_town][i].to] = true;
            new_info.current_town = V[info.current_town][i].to;

            if(info.index == 0 || V[info.current_town][i].cost > info.costs[0]){
            	new_info.costs[0] = V[info.current_town][i].cost;
            	for(int k = 1; k <= info.index; k++){
            		new_info.costs[k] = info.costs[k-1];
            	}
            }else if(V[info.current_town][i].cost <= info.costs[info.index-1]){
            	for(int k = 0; k < info.index; k++){
            		new_info.costs[k] = info.costs[k];
            	}
            	new_info.costs[info.index] = V[info.current_town][i].cost;
            }else{
            	for(calc = 0; info.costs[calc] >= V[info.current_town][i].cost;calc++)new_info.costs[calc] = info.costs[calc];
				new_info.costs[calc] = V[info.current_town][i].cost;
				calc++;
				for(;calc-1 < info.index;calc++)new_info.costs[calc] = info.costs[calc-1];
            }
            new_info.index = info.index+1;
            func(new_info);

        }
    }
}

int main(){

    int from,to,fee;

    while(true){
        scanf("%d %d %d %d %d",&num_ticket,&num_town,&m,&start,&goal);
        if(num_ticket == 0 && num_town == 0 && m == 0 && start == 0 && goal == 0)break;

        minimum = 2000000000;

        for(int i = 1; i <= num_town; i++)V[i].clear();

        for(int i = 0; i < m; i++){
            scanf("%d %d %d",&from,&to,&fee);

            V[from].push_back(Data(to,fee));
            V[to].push_back(Data(from,fee));
        }

        Info info;
        for(int i = 1; i <= num_town;i++)info.visited[i] = false;

        info.current_town = start;
        info.visited[start] = true;

        func(info);

        printf("%d\n",minimum);
    }


    return 0;
}

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

typedef pair<int,int> P;

int main(){
  int c,n,m,s,g,a,b,f;
  int cost[101][101];
  int d[11][101];

  while(1){
    scanf("%d %d %d %d %d",&c,&n,&m,&s,&g);
    if(!c && !n && !m && !s && !g)break;

    for(int i=0;i<=n;i++){
      for(int j=0;j<=n;j++){
	if(i-j)cost[i][j] = 1<<29;
	else cost[i][j] = 0;
      }
    }

    for(int i=0;i<m;i++){
      scanf("%d %d %d",&a,&b,&f);
      cost[a][b] = f;
      cost[b][a] = f;
    }

    priority_queue<P,vector<P>,greater<P> > q;
    fill(d[0],d[0]+n+1,1<<29);
    d[0][s] = 0;
    q.push(P(0,s));

    while(q.size()){
      P p=q.top();
      q.pop();
      int v = p.second;
      if(d[0][v] < p.first)continue;
      for(int i=1;i<=n;i++){
	if(d[0][i] > d[0][v] + cost[v][i]){
	  d[0][i] = d[0][v] + cost[v][i];
	  q.push(P(d[0][i],i));
	}
      }
    }

    for(int i=1;i<=c;i++){
      fill(d[i],d[i]+n+1,1<<29);
      d[i][s] = 0;
      q.push(P(0,s));

      while(q.size()){
	P p=q.top();
	q.pop();
	int v = p.second;
	if(d[i][v] < p.first)continue;
	for(int j=1;j<=n;j++){
	  int tmp = min(d[i][v] + cost[v][j],d[i-1][v] + cost[v][j]/2);
	  if(d[i][j] > tmp){
	    d[i][j] = tmp;
	    q.push(P(d[i][j],j));
	  }
	}
      }
    }

    printf("%d\n",d[c][g]);
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
typedef tuple<int, int, int> T;

int dijkstra(int s, int g, int c, vector<vector<P>> &G){
    vector<vector<int>> dist(110, vector<int>(20, 1e9));
    priority_queue<T, vector<T>, greater<T>> que;
    dist[s][0] = 0;
    que.emplace(0, s, 0);

    while(!que.empty()){
        int ccost, cv, cused;
        tie(ccost, cv, cused) = que.top();
        que.pop();

        if(dist[cv][cused] < ccost) continue;

        for(auto x : G[cv]){
            int nv, ncost;
            tie(nv, ncost) = x;

            if(dist[cv][cused] + ncost < dist[nv][cused]){
                dist[nv][cused] = dist[cv][cused] + ncost;
                que.emplace(dist[nv][cused], nv, cused);
            }

            if(cused < c){
                if(dist[cv][cused] + ncost / 2 < dist[nv][cused+1]){
                    dist[nv][cused+1] = dist[cv][cused] + ncost / 2;
                    que.emplace(dist[nv][cused+1], nv, cused + 1);
                }
            }
        }
    }

    int ans = 1e9;
    for(int i=0; i<=c; i++){
        ans = min(ans, dist[g][i]);
    }

    return ans;
}

int main(){
    while(1){
        int c, n, m, s, d; cin>>c>>n>>m>>s>>d;
        s--, d--;
        if(c == 0) return 0;

        vector<vector<P>> G(n);
        for(int i=0; i<m; i++){
            int a, b, f; cin>>a>>b>>f;
            a--, b--;
            G[a].emplace_back(b, f);
            G[b].emplace_back(a, f);
        }

        cout << dijkstra(s, d, c, G) << endl;
    }
}