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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <ctime>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <functional>
#include <fstream>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
class Timer
{
  private:
    double time_limit;
    //unsigned long long int cycle_per_sec = 2500000000; // Topcoder
    unsigned long long int cycle_per_sec = 2777777777; // AtCoder
    unsigned long long int beginCycle;
    unsigned long long int getCycle()
    {
        unsigned int low, high;
        __asm__ volatile("rdtsc"
                         : "=a"(low), "=d"(high));
        return ((unsigned long long int)low) | ((unsigned long long int)high << 32);
    }

  public:
    Timer(double time_limit) : time_limit(time_limit)
    {
        beginCycle = getCycle();
    }

    double elapsedTime()
    {

        return (double)(getCycle() - beginCycle) / cycle_per_sec;
    }
    bool isTLE()
    {
        return elapsedTime() >= time_limit;
    }
};

Timer gtimer(2.85);

struct NODE
{
    int to, cost, ticket;
    NODE(int to, int cost, int ticket) : to(to), cost(cost), ticket(ticket) {}
};
bool operator<(const NODE &a, const NODE &b)
{
    return a.cost > b.cost;
}
int main()
{
    int c, n, m, s, d;
    while (cin >> c >> n >> m >> s >> d && c)
    {
        vector<NODE> g[110];

        for (int i = 0; i < m; i++)
        {
            int a, b, f;
            cin >> a >> b >> f;
            --a, --b;
            g[a].push_back(NODE(b, f, 0));
            g[b].push_back(NODE(a, f, 0));
            g[a].push_back(NODE(b, f / 2, 1));
            g[b].push_back(NODE(a, f / 2, 1));
        }
        --s, --d;
        priority_queue<NODE> Q;
        Q.push(NODE(s, 0, c));
        int done[110][11] = {};
        while (Q.size())
        {
            auto q = Q.top();
            Q.pop();
            if (q.ticket < 0)
                continue;
            if (done[q.to][q.ticket]++)
                continue;
            if (q.to == d)
            {
                cout << q.cost << endl;
                break;
            }
            for (auto e : g[q.to])
            {
                Q.push(NODE(e.to, q.cost + e.cost, q.ticket - e.ticket));
            }
        }
    }

    std::cout.flush();
    return 0;
}

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

using namespace std;

struct node {
	int to, cost, ticket;

	node(int to, int cost) : to(to), cost(cost) { this -> ticket = 0; }
	node(int to, int cost, int ticket) : to(to), cost(cost), ticket(ticket) { }
	node() { this -> to = this -> cost = this -> ticket = 0; }
};

bool operator < (const node &a, const node &b) {
	return a.cost < b.cost;
}

bool operator > (const node &a, const node &b) {
	return a.cost > b.cost;
}

int main() {
	int c, n, m, s, d;
	while (cin >> c >> n >> m >> s >> d, c || n || m || s || d) {
		--s, --d;

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

		int rest = c;
		vector< vector<int> > cost(n, vector<int>(c+1, -1));
		vector<bool> used(n, -1);
		priority_queue< node, vector<node>, greater<node> > que; que.push( node(s, 0, c) );
		while ( !que.empty() ) {
			node next = que.top(); que.pop();

			if (cost[next.to][next.ticket] >= 0) continue;
			cost[next.to][next.ticket] = next.cost;

			if (next.to == d) rest = min(rest, next.ticket);

			for (int i = 0; i < v[next.to].size(); ++i) {
				que.push( node(v[next.to][i].to, next.cost + v[next.to][i].cost, next.ticket) );
				if (next.ticket) que.push( node(v[next.to][i].to, next.cost + v[next.to][i].cost/2, next.ticket-1) );
			}
		}

		cout << cost[d][rest] << endl;
	}
}

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

#define INF 1010000000
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)
#define sint(i) scanf("%d",&i);
#define sintt(i,j) scanf("%d%d",&i,&j);
#define sinttt(i,j,k) scanf("%d%d%d",&i,&j,&k);

using namespace std;

class S{
	public:
	int p;
	int v;
	int n;
	
	S(int p,int v,int n):p(p),v(v),n(n){}
	
	bool operator<(const S&a)const{
		return v>a.v;
		return v==a.v ? v>a.v : n <a.n;
	}
};

int main(){
	while(1){
		int c,n,m,s,g;
		scanf("%d%d%d%d%d",&c,&n,&m,&s,&g);
		if(c==0&&n==0&&m==0&&s==0&&g==0)break;
		
		int root[101][101];
		rep(i,101)rep(j,101)root[i][j]=INF;
		
		rep(i,101)root[i][i]=0;
		
		rep(i,m){
			int a,b,d;
			sinttt(a,b,d);
			root[a][b]=d;
			root[b][a]=d;
		}
		
		priority_queue<S> qui;
		qui.push(S(s,0,0));
		int ans=0;
		
		int visit[101][11];
		rep(i,101)rep(j,11)visit[i][j]=0;
		
		while(1){
			if(qui.empty()==1){
				ans=-1;
				break;
			}
			
			S u=qui.top();qui.pop();
			if(visit[u.p][u.n]==1){continue;}
			
			printf("%d %d %d\n",u.p,u.v,u.n);
			if(u.p==g){
				ans=u.v;
				break;
			}
			
			visit[u.p][u.n]=1;
			
			reps(i,n){
				if(root[u.p][i]!=INF){
					if(visit[i][u.n]==0){
						qui.push(S(i,u.v+root[u.p][i],u.n));
					}
					if(visit[i][u.n+1]==0 && visit[u.p][u.n+1]==0 && u.n+1 <= c){
						qui.push(S(i,u.v+root[u.p][i]/2,u.n+1));
					}
				}
			}
		}
		printf("%d\n",ans);
	}
}