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 <bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
const ll INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int V;
int d[2000];
vector<vector<edge>> es;

void dijkstra(int s)
{
	fill(ALL(d), INF);
	d[s] = 0;
	priority_queue<P, vector<P>, greater<P>> que;
	que.push(P(0, s));
	while (!que.empty())
	{
		P p = que.top(); que.pop();
		int v = p.second;
		if (d[v] < p.first) continue;
		REP(i, es[v].size())
		{
			edge e = es[v][i];
			if (d[e.to] > d[v] + e.cost)
			{
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int main()
{
	int c, n, m, s, g;
	while (cin >> c >> n >> m >> s >> g, c)
	{
		V = n;
		s--; g--;
		es.clear();
		es.resize(c*n+n);
		REP(i, n)
		{
			int a, b, f;
			cin >> a >> b >> f;
			a--; b--;
			REP(j, c+1) es[a + j*n].push_back(edge{ a + j*n, b + j*n, f });
			REP(j, c) es[a + (j+1)*n].push_back(edge{ a + (j+1)*n, b + j*n, f/2 });
			REP(j, c + 1) es[b + j*n].push_back(edge{ b + j*n, a + j*n, f });
			REP(j, c) es[b + (j + 1)*n].push_back(edge{ b + (j + 1)*n, a + j*n, f / 2 });
		}
		dijkstra(s + c*n);
		int ans = INF;
		REP(i, c + 1) ans = min(ans, d[g + i*n]);
		cout << ans << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int c,n,m,s,d;
int rosen[101][101] = {-1};
int saiki(int p,vector<int> st,vector<bool> u){
    int ans = INF;
    if(p == d){
	sort(RALL(st));
	int tc = min(c,(int)(st.size()));
	REP(i,c){
	    st[i] /= 2;
	}
	int sum = 0;
	REP(i,st.size()){
	    sum += st[i];
	}
	return sum;
    }
    FOR(i,1,n+1){
	if(!u[i] && rosen[p][i] > 0){
	    vector<int> temp = st;
	    vector<bool> utemp = u;
	    temp.PB(rosen[p][i]);
	    utemp[i] = true;
	    ans = min(ans,saiki(i,temp,utemp));
	}
    }
    return ans;
}

int main(){
    while(true){
	cin >> c >> n >> m >> s >> d;
	if(c==0&&n==0&&m==0&&s==0&&d==0) break;
	memset(rosen,-1,sizeof(rosen));
	REP(i,m){
	    int a,b,f;
	    cin >> a >> b >> f;
	    rosen[a][b] = f;
	    rosen[b][a] = f;
	}
	vector<bool>  used(n+1,false);
	vector<int> nanika;
	used[s] = true;
	cout << saiki(s, nanika ,used) << endl;
    }
    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int ijk[200][20];
int v[200][20];
vector<pair<int,int> >g[200];
int main(){
	int a,b,c,d,e;
	while(scanf("%d%d%d%d%d",&a,&b,&c,&d,&e),b){
		d--;e--;
		for(int i=0;i<200;i++)g[i].clear();
		for(int i=0;i<c;i++){
			int p,q,r;
			scanf("%d%d%d",&p,&q,&r);
			p--;q--;
			g[p].push_back(make_pair(q,r));
			g[q].push_back(make_pair(p,r));
		}
		for(int i=0;i<b;i++){
			for(int j=0;j<=a;j++){
				v[i][j]=0;
				ijk[i][j]=999999999;
			}
		}
		ijk[d][a]=0;
		priority_queue<pair<int,pair<int,int> > >Q;
		Q.push(make_pair(0,make_pair(d,a)));
		while(Q.size()){
			int cost=-Q.top().first;
			int at=Q.top().second.first;
			int nk=Q.top().second.second;
			Q.pop();
			if(v[at][nk])continue;
			for(int i=0;i<g[at].size();i++){
				if(!v[g[at][i].first][nk]&&ijk[g[at][i].first][nk]>cost+g[at][i].second){
					ijk[g[at][i].first][nk]=cost+g[at][i].second;
					Q.push(make_pair(-ijk[g[at][i].first][nk],make_pair(g[at][i].first,nk)));
				}
				if(nk&&!v[g[at][i].first][nk-1]&&ijk[g[at][i].first][nk-1]>cost+g[at][i].second/2){
					ijk[g[at][i].first][nk-1]=cost+g[at][i].second/2;
					Q.push(make_pair(-ijk[g[at][i].first][nk-1],make_pair(g[at][i].first,nk-1)));
				}
			}
		}
		int ret=999999999;
		for(int i=0;i<=a;i++)ret=min(ret,ijk[e][i]);
		printf("%d\n",ret);
	}
}