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 <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class Edge
{
public:
    int to, cost;
    Edge(){};
    Edge(int to0, int cost0){to = to0; cost = cost0;}
};

void shortestPath(const vector<vector<Edge> >& edges, int start, vector<int>& dist)
{
    dist.assign(edges.size(), INT_MAX);
    dist[start] = 0;
    priority_queue<pair<int,int> ,vector<pair<int,int> >, greater<pair<int,int> > > q;
    q.push(make_pair(0, start));

    while(!q.empty()){
        pair<int, int> p = q.top();
        q.pop();
        int v = p.second;
        if(dist[v] < p.first)
            continue;
        for(unsigned i=0; i<edges[v].size(); ++i){
            Edge e = edges[v][i];
            if(dist[v] + e.cost < dist[e.to]){
                dist[e.to] = dist[v] + e.cost;
                q.push(make_pair(dist[e.to], e.to));
            }
        }
    }
}

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

        vector<vector<Edge> > edges((c+1) * n);
        for(int i=0; i<m; ++i){
            int a, b, f;
            cin >> a >> b >> f;
            -- a;
            -- b;

            for(int j=0; j<=c; ++j){
                edges[a+j*n].push_back(Edge(b+j*n, f));
                edges[b+j*n].push_back(Edge(a+j*n, f));
            }
            for(int j=0; j<c; ++j){
                edges[a+j*n].push_back(Edge(b+(j+1)*n, f/2));
                edges[b+j*n].push_back(Edge(a+(j+1)*n, f/2));
            }
        }

        vector<int> dist;
        shortestPath(edges, s, dist);
        int ret = INT_MAX;
        for(int i=0; i<=c; ++i)
            ret = min(ret, dist[d+i*n]);
        cout << ret << endl;
    }
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX 100000
#define MAX_NODE_ID 100
#define MAX_TICKET 10
class Node{
public:
	int id;
	int cost;
	int ticket;
	Node(int i,int c,int t){
		id=i;
		cost=c;
		ticket=t;
	}
	bool operator < (const Node &o) const{
		return cost > o.cost;
	}
};
typedef priority_queue<Node> PQ;
int table[MAX_NODE_ID][MAX_NODE_ID];
bool visited[MAX_NODE_ID][MAX_TICKET];
int main(){

	for(int c,n,m,s,d;cin>>c>>n>>m>>s>>d&&(c||n||m||s||d);){
		for(int i=0;i<MAX_NODE_ID;i++)for(int j=0;j<MAX_NODE_ID;j++)table[i][j]=MAX;
		for(int i=0,a,b,f;i<m&&cin>>a>>b>>f;i++){
			table[a][b]=f;
			table[b][a]=f;
//			cout<<"table["<<a<<"]["<<b<<"]"<<table[a][b]<<endl;
//			cout<<"table["<<b<<"]["<<a<<"]"<<table[b][a]<<endl;
		}
		PQ pq;
		Node start(s,0,c);
		pq.push(start);
		while(!pq.empty()){
			int current_node=pq.top().id;
			int current_cost=pq.top().cost;
			int current_ticket=pq.top().ticket;
//			cout<<"current:"<<current_node<<endl;
			if(current_node==d)break;
			if(visited[current_node][current_ticket]);
			else{
				for(int i=1;i<=n;i++){
					if(current_node==i)continue;
					if(table[current_node][i]==MAX)continue;
	//				cout<<"["<<current_node<<"]["<<i<<"]"<<"cost:"<<table[current_node][i]<<endl;
					Node node1(i,current_cost+table[current_node][i],current_ticket);
					pq.push(node1);
					if(current_ticket>0){
						Node node2(i,current_cost+(table[current_node][i]/2),current_ticket-1);
						pq.push(node2);
					}
					visited[current_node][i]=true;
				}
			}
			pq.pop();
		}
		cout<<pq.top().cost<<endl;
	}


	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int c,n,m,s,t;
int dp[11][100];
main()
{
	while(cin>>c>>n>>m>>s>>t,c)
	{
		for(int i=0;i<=c;i++)for(int j=0;j<n;j++)dp[i][j]=1e9;
		vector<vector<pair<int,int> > >G(n);
		for(int i=0;i<m;i++)
		{
			int u,v,C;cin>>u>>v>>C;
			u--,v--;
			G[u].push_back(make_pair(v,C));
			G[v].push_back(make_pair(u,C));
		}
		s--,t--;
		priority_queue<pair<int,pair<int,int> > >P;
		P.push(make_pair(0,make_pair(s,c)));
		dp[c][s]=0;
		while(!P.empty())
		{
			int d=-P.top().first,u=P.top().second.first,s=P.top().second.second;
			P.pop();
			for(int i=0;i<G[u].size();i++)
			{
				int cost=G[u][i].second;
				int v=G[u][i].first;
				if(s>0)
				{
					if(dp[s-1][v]>d+cost/2)
					{
						dp[s-1][v]=d+cost/2;
						P.push(make_pair(-dp[s-1][v],make_pair(v,s-1)));
					}
				}
				if(dp[s][v]>d+cost)
				{
					dp[s][v]=d+cost;
					P.push(make_pair(-dp[s][v],make_pair(v,s)));
				}
			}
		}
		int ans=1e9;
		for(int i=0;i<=c;i++)ans=min(ans,dp[i][t]);
		cout<<ans<<endl;
	}
}

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

using namespace std;

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

struct Edge{
  int to,cost;
  Edge(int _to, int _cost):to(_to), cost(_cost){};
};

int minCost[101][11];
vector<Edge> edge[101];
static const int INF = INT_MAX;

int c,n,m,s,d;

int dijkstra(void){
  priority_queue<P, vector<P>, greater<P> >que;
  que.push(make_pair(0,make_pair(s,c)));

  for(int i=0;i<=n;i++)
    for(int j=0;j<=n;j++)
      minCost[i][j]=INF;

  while(!que.empty()){
    P p = que.top();
    que.pop();

    if(p.second.first==d)return p.first;
    
    if(minCost[p.second.first][p.second.second] <= p.first)continue;
    minCost[p.second.first][p.second.second] = p.first;
    
    for(int i=0;i<edge[p.second.first].size();i++){
      Edge e = edge[p.second.first][i];
      que.push(make_pair(p.first+e.cost,make_pair(e.to,p.second.second)));
    }

    if(p.second.second==0)continue;
    
    for(int j=0;j<edge[p.second.first].size();j++){
      Edge e = edge[p.second.first][j];
      que.push(make_pair(p.first+e.cost/2,make_pair(e.to,p.second.second-1)));
    }
  }
  return -1;
}

int main(void){
  int a,b,f;

  while(cin >> c >> n >> m >> s >> d,c|n|m|s|d){

    for(int i=0;i<=n;i++)edge[i].clear();

    for(int i=0;i<m;i++){
      cin >> a >> b >> f;
      edge[a].push_back(Edge(b,f));
      edge[b].push_back(Edge(a,f));
    }

    cout << dijkstra() << endl;
  }

  return 0;
}