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>
#define INF (1<<29)
#define all(c) (c).begin(),(c).end()
#define pb(c) push_back(c)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int dis[111][111];
int c, n, m, s, d;
int next[111][111];

void dfs(int cur, int sum, int cost){
  if(next[cur][sum] < cost) return;
  next[cur][sum] = cost;
  for(int i = 0; i < n; i++){
    if(dis[cur][i] == (1<<29)) continue;
    if(sum > 0) dfs(i, sum-1, cost+(dis[cur][i]/2));
    dfs(i, sum, cost+dis[cur][i]);
  }
}

int main(){
  ios::sync_with_stdio(false);

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

    for(int i = 0; i < 111; i++){
      for(int j = 0; j < 111; j++){
        dis[i][j] = next[i][j] = (1<<29);
      }
    }

    for(int i = 0; i < m; i++){
      int a, b, f;
      cin >> a >> b >> f;
      a--; b--;
      dis[a][b] = f;
      dis[b][a] = f;
    }

    dfs(s-1,c,0);
    int ans = (1<<29);
    for(int i =0; i < c; i++){
      ans = min(ans, next[d-1][i]);
    }
    cout << ans << endl;
  }


}

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

constexpr int MAX_N=100,INF=(int)1e9;

struct edge {
	int to,cost;
	edge()=default;
	edge(int _t,int _c) : to(_t),cost(_c) {}
};

struct Node{
	int v,num,cost;
	Node()=default;
	Node(int _v,int _n,int _c) : v(_v),num(_n),cost(_c) {}

	bool operator<(const Node& rhs) const{
		return cost==rhs.cost ? v < rhs.v : cost < rhs.cost; 
	}
	bool operator>(const Node& rhs) const{
		return cost==rhs.cost ? v > rhs.v : cost > rhs.cost;
	}
};

int c,n,m,s,g;

std::vector<edge> G[MAX_N];

int d[11][MAX_N];

int dijkstra() {

	for(int i=0;i<=c;++i){
		for(int j=0;j<n;++j)
			d[i][j]=INF;
	}

	std::priority_queue<Node,std::vector<Node>,std::greater<Node>> que;

	d[c][s]=0;
	que.emplace(s,c,0);

	while(!que.empty()){

		Node node=que.top();
		que.pop();

		int v=node.v;

		if(v==g)
			return d[node.num][g];

		if(node.cost>d[node.num][v])
			continue;

		for(int i=0;i<G[v].size();++i){
			edge e=G[v][i];
			if(node.num==0)
				goto a;
			else{
				if(d[node.num-1][e.to]>d[node.num][v]+e.cost/2){
					d[node.num-1][e.to]=d[node.num][v]+e.cost/2;
					que.emplace(e.to,node.num-1,d[node.num-1][e.to]);
				}
				a:;
				if(d[node.num][e.to]>d[node.num][v]+e.cost){
					d[node.num][e.to]=d[node.num][v]+e.cost;
					que.emplace(e.to,node.num,d[node.num][e.to]);
				}
			}
		}
	}
	return -1;
}

int main() {
	while(1){
		std::cin>>c>>n>>m>>s>>g;
		if(c+n+m+s+g==0)
			return 0;
		--s;
		--g;
		int a,b,f;
		for(int i=0;i<m;++i){
			std::cin>>a>>b>>f;
			--a;
			--b;
			G[a].emplace_back(b,f);
			G[b].emplace_back(a,f);
		}
		int ans=dijkstra();
		std::cout<<ans<<std::endl;
		for(int i=0;i<n;++i)
			G[i].clear();
	}
	return 0;
}

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

class State{
public:
  int v, c, t;
  State(int _v, int  _c, int _t)
    :v(_v), c(_c), t(_t) {}
  bool operator < (const State& st) const{
    return this->c > st.c;
  }
};


int main(){

  const int INF = 1 << 27;
 
  int ticket, n, m, start, goal;
  while(cin >> ticket >> n >> m >> start >> goal){
    if((ticket|n|m|start|goal) == 0) break;
    --start;
    --goal;

    int s, dd, score;
    vector<vector<vector<int> > >cost(n, vector<vector<int> >(n, vector<int>(ticket +1, INF*2)));
    for(int i=0; i<m; i++){
      cin >> s >> dd >> score;
      --s;
      --dd;
      cost[s][dd][0] = cost[dd][s][0] = score;
      for(int j=1; j<ticket; j++) cost[s][dd][j] = cost[dd][s][j] = score;      
      //cout << cost[s][dd][0] << endl;
    }

    priority_queue<State> req;
    vector<vector<int> > d(n, vector<int>(ticket+1, INF));
    for(int i=0; i<=ticket; i++){
      d[start][i] = 0;
 
    }
    req.push(State(start, 0, ticket));

    while(! req.empty()){
      State st = req.top();
      req.pop();

      //cout << "v:" << st.v+1 <<" c:" << st.c << " t:" << st.t << endl;

      if(st.v == goal){
	   cout << st.c << endl;
	   break;
      }

      for(int i=0; i<n; i++){
	//use ticket
	if(st.t>0 && d[st.v][st.t] + cost[st.v][i][st.t-1]/2 < d[i][st.t-1]){
	  d[i][st.t-1] = st.c + cost[st.v][i][st.t-1]/2;
	  req.push(State(i, d[i][st.t-1], st.t-1));
	}
	//not use ticket
	if(d[st.v][st.t] + cost[st.v][i][st.t] < d[i][st.t]){
	  d[i][st.t] = st.c + cost[st.v][i][st.t];
	  req.push(State(i, d[i][st.t], st.t));
	}
      }
	
      
    }
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
#define MAX_N 100

typedef pair<int, int> pi;
typedef pair<int, pi> P;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

struct edge{ int to, cost;};

vector<vector<edge> >G(MAX_N);
int c, n, m;
int cost[MAX_N][MAX_N];
int d[MAX_N];

void dijkstra(int s){
  priority_queue<P, vector<P>, greater<P> >q;
  rep(i, n)d[i] = INF;
  d[s] = 0;
  q.push(P(0, pi(s, c)));

  while(!q.empty()){
    P p = q.top(); q.pop();
    //    printf("%d %d %d\n", p.F, p.S.F, p.S.S);
    int v = p.S.F;
    //    if(d[v] < p.first) continue;
    rep(i, G[v].size()){
      edge e = G[v][i];
      if(d[e.to] > d[v] + e.cost){
	d[e.to] = d[v] + e.cost;
	q.push(P(d[e.to], pi(e.to, p.S.S)));
      }
      if(p.S.S && d[e.to] > d[v] + e.cost/2){
	d[e.to] = d[v] + e.cost/2;
	//	d[v][1]--;
	q.push(P(d[e.to], pi(e.to, p.S.S-1)));
      }
    }
  }      
}

int main(){
  int s, g;
  while(scanf("%d%d%d%d%d", &c, &n, &m, &s, &g) && c+n+m+s+g){
    s--; g--;
    G.clear();
    G.resize(n);
    int a, b, f;
    rep(i, m){
      scanf("%d%d%d", &a, &b, &f);a--;b--;
      edge e; e.to = b; e.cost = f;
      G[a].PB(e);
      e.to = a;
      G[b].PB(e);
    }
    dijkstra(s);
    printf("%d\n", d[g]);
  }
  return 0;
}