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 <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <climits>

using namespace std;

#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n) 

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

struct Edge
{
	int src, dst;
	int weight;
	Edge(int src, int dst, int weight) : src(src), dst(dst), weight(weight) {}

	bool operator< (const Edge& e)const
	{
		return weight != e.weight ? weight > e.weight :
			src != e.src ? src < e.src : dst < e.dst;
	}
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void Dijkstra(const Graph& g, int s, vi& dist, vi& prev)
{
	int n = g.size();
	dist.assign(n, INT_MAX);
	dist[s] = 0;
	prev.assign(n, -1);
	
	priority_queue<Edge> Q;
	Q.push(Edge(-2, s, 0));

	while(!Q.empty()){
		Edge e = Q.top();
		Q.pop();
		if(prev[e.dst] != -1)
			continue;
		prev[e.dst] = e.src;

		rep(i, g[e.dst].size()){
			int pw = e.weight + g[e.dst][i].weight;
			if(dist[g[e.dst][i].dst] > pw){
				dist[g[e.dst][i].dst] = pw;
				Q.push(Edge(g[e.dst][i].src, g[e.dst][i].dst, pw));
			}
		}
	}
}


vi buildPath(const vi& prev, int t)
{
	vi path;
	for(int u = t; u >= 0; u = prev[u])
		path.push_back(u);
	reverse(path.begin(), path.end());
	return path;
}

int main()
{
	int c, n, m, s, d;
	while(scanf("%d%d%d%d%d", &c, &n, &m, &s, &d), c){
		--s; --d;
		Graph g(n*(c+1));
		rep(i, m){
			int a, b, f;
			scanf("%d%d%d", &a, &b, &f);
			--a; --b;

			rep(j, c+1){
				g[j*n+a].push_back(Edge(j*n+a, j*n+b, f));
				g[j*n+b].push_back(Edge(j*n+b, j*n+a, f));
				if(j){
					g[(j-1)*n+a].push_back(Edge((j-1)*n+a, j*n+b, f/2));
					g[(j-1)*n+b].push_back(Edge((j-1)*n+b, j*n+a, f/2));
				}
			}
		}

		vi dist, prev;
		Dijkstra(g, s, dist, prev);

		int ans = INT_MAX;
		rep(j, c+1){
			ans = min(ans, dist[j*n+d]);
		}
		printf("%d\n", ans);
	}

	return 0;
}

C++: #include<iostream>
#include<utility>
#include<vector>
#include<cstdio>
#include<queue>
#include <map>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
//int in(){int x;cin>>x;return x;}

int main ()
{
	int c,n,m,s,d;
	while(cin>>c>>n>>m>>s>>d&&c&&n&&m&&s&&d)
	{
		int flg[110]={};
		int ans =0;
		priority_queue < PP, vector<PP>, greater<PP> > q;
		vector< pair< int,int > > way[110];
		for(int i=0;i<m;i++)
		{
			int a,b,f;
			cin>>a>>b>>f;
			way[a].push_back(pair<int,int>(b,f));
			way[b].push_back(pair<int,int>(a,f));
		}
		pair< int , pair<int,int> > next;
		next.first=0;next.second.first=s;next.second.second=c;
		q.push(next);
		while(!q.empty())
		{
			pair< int , pair<int,int> > now=q.top();q.pop();
			flg[now.second.first]=1;
			if(now.second.first==d)
			{
				ans=now.first;
				break;
			}
			
			for(int i=0;i<way[now.second.first].size();i++)
			{
				if(flg[way[now.second.first][i].first]==1)continue;
				next.first=now.first+way[now.second.first][i].second;
				next.second.first=way[now.second.first][i].first;
				q.push(next);
				next.first=now.first+way[now.second.first][i].second/2;
				next.second.first=way[now.second.first][i].first;
				next.second.second=now.second.second-1;
				q.push(next);
			}
			
		}
		cout<<ans<<endl;
	}
	
	
	return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

using namespace std;

int minimum,num_ticket,num_town,m,start,goal,costTable[101][101];


struct Info{
	Info(){
		for(int i = 1; i <= num_town; i++){
			visited[i] = false;
		}
		current_town = 0;
		index = 0;
	}
	bool visited[102];
	int current_town,index,costs[102];

};

void func(Info info){

	//?¢?????????????????¨??????????<???info.costs????????????????????§???????????¨????????????>
	int sum = 0;

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

	if(info.current_town == goal){ //?????¨????¢????????????????????°????????¶????????????????????????????
		minimum = min(minimum,sum);
		return;
	}else{
		if(sum >= minimum){
			return;
		}
	}

	for(int i = 1; i <= num_town; i++){
		if(costTable[info.current_town][i] != -1 && info.visited[i] == false){ //??????????????¨?????????????¨????????????¨???????????´???
			Info new_info;
			for(int k = 1; k <= num_town; k++)new_info.visited[k] = info.visited[k];
			new_info.visited[i] = true;
			new_info.current_town = i;
			for(int k = 0; k < info.index;k++)new_info.costs[k] = info.costs[k];

			//?????\?????????
			if(info.index == 0 || info.costs[info.index-1] <= costTable[info.current_town][i]){
				new_info.costs[info.index] = costTable[info.current_town][i];
				new_info.index = info.index+1;
				func(new_info);
			}else{
				for(int k = 0; k < info.index; k++){
					if(new_info.costs[k] < costTable[info.current_town][i]){
						for(int p = info.index-1; p >= k; p--){
							new_info.costs[p+1] = new_info.costs[p];
						}
						new_info.costs[k] = costTable[info.current_town][i];
						break;
					}
				}
				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++){
			for(int k = 1; k <= num_town; k++)costTable[i][k] = -1;
		}

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

		Info info;

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

		func(info);

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


	return 0;
}