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 <queue>
using namespace std;

struct State {
    int no_;
    int cost_;
    int ticket_;
    State(int no, int cost, int ticket) 
        : no_(no), cost_(cost), ticket_(ticket) {} 
    
};

struct Edge {
    int to_;
    int cost_;
    Edge(int to, int cost) 
        : to_(to), cost_(cost) {}
};

int c, n, m, s, d;

auto dijkstra(vector<vector<Edge>> &g, vector<vector<int>> &dm) -> int {
    auto comp = [](auto& lhs, auto& rhs){return lhs.cost_ > rhs.cost_;};
    priority_queue<State, vector<State>, decltype(comp)> que(comp);

    que.push(State(s, 0, c));
    for (auto &e : dm[s]) {
        e = 0;
    }
    while (!que.empty()) {
        State s = que.top();
        que.pop();
        int current_no = s.no_;
        int current_cost = s.cost_;
        int current_ticket = s.ticket_;
        for (auto &e : g[current_no]) {
            int to = e.to_;
            int cost = e.cost_;
            if (current_ticket > 0) {
                // not use ticket
                if (dm[to][current_ticket] > dm[current_no][current_ticket] + cost) {
                    que.push(State(to, dm[current_no][current_ticket] + cost, current_ticket));
                    dm[to][current_ticket] = dm[current_no][current_ticket] + cost;
                }
                // use ticket
                if (dm[to][current_ticket-1] > dm[current_no][current_ticket] + cost/2) {
                    que.push(State(to, dm[current_no][current_ticket] + cost/2, current_ticket-1));
                    dm[to][current_ticket-1] = dm[current_no][current_ticket-1] + cost/2;
                }
            }else {
                if (dm[to][current_ticket] > dm[current_no][current_ticket] + cost) {
                    que.push(State(to, dm[current_no][current_ticket] + cost, current_ticket));
                    dm[to][current_ticket] = dm[current_no][current_ticket] + cost;
                }
            }
        }
    }

    int ans = 1e8;
    for (auto &e : dm[d]) {
        ans = min(ans, e);
    }
    return ans;
}

auto main(int argc, char const *argv[]) -> int {
    constexpr int INF = 1e8;

    while(cin>>c>>n>>m>>s>>d, c||n||m||s||d) {
        vector<vector<Edge>> g(n+1);
        vector<vector<int>> dm(n+1);
        for (auto &e : dm) {
            for (int i = 0; i < c + 1; ++i) {
                e.push_back(INF);
            }
        }

        for (int i = 0; i < m; ++i) {
            int a, b, f;
            cin>>a>>b>>f;
            g[a].push_back(Edge(b, f));
            g[b].push_back(Edge(a, f));
        }
        cout<<dijkstra(g, dm)<<endl;
    }
    return 0;
}

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

struct Data{
    int cost,p,tk;
    Data(){}
    Data(int _cost,int _p,int _tk){
        cost=_cost; p=_p; tk=_tk;
    }
    bool operator<(const Data &a)const{
        return cost>a.cost;
    }
};

struct Way{
    int to,cost;
    Way(){}
    Way(int _to,int _cost){
        to=_to; cost=_cost;
    }
};

int main(){
    int C,N,M,S,D,A,B,F;
    while(cin>>C>>N>>M>>S>>D,C||N||M||S||D){
        vector<Way> way[501];
        for(int i=0;i<M;i++){
            cin>>A>>B>>F;
            way[A].push_back(Way(B,F));
            way[B].push_back(Way(A,F));
        }

        priority_queue<Data> q;
        q.push(Data(0,S,0));
        Data q_c;
        bool memo[101][11]={};
        while(!q.empty()){
            q_c = q.top(); q.pop();
            if(q_c.p==D){
                printf("%d\n",q_c.cost);
                break;
            }
            if(memo[q_c.p][q_c.tk]) continue;
            memo[q_c.p][q_c.tk]=true;
            for(int i=0;i<way[q_c.p].size();i++){
                if(q_c.tk<C)
                    q.push(Data(q_c.cost+(way[q_c.p][i].cost)/2,way[q_c.p][i].to,q_c.tk+1));
                q.push(Data(q_c.cost+way[q_c.p][i].cost,way[q_c.p][i].to,q_c.tk));
            }
        }
    }
    return 0;
}