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 <functional>
#include <queue>
using namespace std;

const int INF = 10000000;

typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef priority_queue<PP, vector<PP>, greater<PP> > Q;

int C, N, M, S, D;
int m[100][100];
int u[11][100];

bool input() {
  cin >> C >> N >> M >> S >> D;
  if (!C && !N && !M && !S && !D) {
    return false;
  }
  --S, --D;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      m[i][j] = INF;
    }
    m[i][i] = 0;
  }
  for (int i = 0; i < M; ++i) {
    int a, b, f;
    cin >> a >> b >> f;
    --a, --b;
    m[a][b] = f;
    m[b][a] = f;
  }
  return true;
}

void solve() {
  for (int k = 0; k <= C; ++k) {
    for (int i = 0; i < N; ++i) {
      u[k][i] = INF;
    }
  }
  Q q;
  q.push(PP(0, P(0, S)));
  int ans = INF;
  while (!q.empty()) {
    PP pp = q.top();
    q.pop();
    int f = pp.first;
    P p = pp.second;
    int k = p.first;
    int l = p.second;
    if (l == D) {
      ans = f;
      break;
    }
    if (u[k][l] != INF) {
      continue;
    }
    u[k][l] = f;
    for (int i = 0; i < N; ++i) {
      if (l == i || m[l][i] == INF) {
        continue;
      }
      q.push(PP(f + m[l][i], P(k, i)));
      if (k < C) {
        q.push(PP(f + m[l][i]/2, P(k + 1, i)));
      }
    }
  }
  cout << ans << endl;
}

int main() {
  while (input()) {
    solve();
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

struct Node {
    Node(int s, int t, int c) : s(s), t(t), c(c) {}

    int s, t, c;

    bool operator<(const Node &rhs) const {
        return c > rhs.c;
    }
};

int main() {
    int c, n, m, s, d;
    while (cin >> c >> n >> m >> s >> d && n) {
        --s, --d;
        vector<vector<Node> > g(n);

        for (int i = 0; i < m; i++) {
            int a, b, c;
            cin >> a >> b >> c;
            --a, --b;
            g[a].push_back(Node(b, 0, c));
            g[b].push_back(Node(a, 0, c));
            g[a].push_back(Node(b, 1, c / 2));
            g[b].push_back(Node(a, 1, c / 2));
        }
        priority_queue<Node> Q;
        Q.push(Node(s, c, 0));
        bool flg[111][11] = {};
        while (Q.size()) {
            Node q = Q.top();
            Q.pop();
            if (flg[q.s][q.t]) continue;
            if (q.s == d) {
                cout << q.c << endl;
                break;
            }
            flg[q.s][q.t] = true;
            for (auto e : g[q.s]) {
                if (q.t >= e.t) {
                    Q.push(Node(e.s, q.t - e.t, q.c + e.c));
                }
            }
        }
    }
}

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

using namespace std;

struct edge{ int to, cost; };
typedef pair<int, int> P;
const int INF = INT_MAX / 4;

int V;
vector<edge> G[100];
int D[100];
int pre[100];

int c, n, m, s, d;

void dijkstra(int s)
{
	priority_queue<P, vector<P>, greater<P> > que;
	fill(D, D + 100, INF);
	fill(pre, pre + 100, -1);
	D[s] = 0;
	que.push(P(0, s));

	while (!que.empty())
	{
		P p = que.top(); que.pop();
		int v = p.second;
		if (D[v] < p.first) continue;
		for (int i = 0; i < G[v].size(); i++)
		{
			edge e = G[v][i];
			if(D[e.to] > D[v] + e.cost)
			{
				D[e.to] = D[v] + e.cost;
				pre[e.to] = v;
				que.push(P(D[e.to], e.to));
			}
		}
	}
}

int solve()
{
	dijkstra(s - 1);
	vector<int> vec;
	for (int t = d - 1; pre[t] != -1 ; t = pre[t])
	{
		for (int i = 0; i < G[t].size(); i++)
		{
			if(pre[t] == G[t][i].to)
			{
				vec.push_back(G[t][i].cost);
				break;
			}
		}
	}
	sort(vec.begin(), vec.end(), greater<int>());
	for (int i = 0; i < c && i < vec.size(); i++)
	{
		vec[i] = vec[i] / 2;
	}
	return accumulate(vec.begin(), vec.end(), 0);
}

int main()
{
	while (cin >> c >> n >> m >> s >> d && c && n && m && s && d)
	{
		for (int i = 0; i < 100; i++)
		{
			G[i] = vector<edge>();
		}
		for (int i = 0; i < m; i++)
		{
			int a, b, f;
			cin >> a >> b >> f;
			edge e;
			e.to = b - 1;
			e.cost = f;
			G[a - 1].push_back(e);
			e.to = a - 1;
			e.cost = f;
			G[b - 1].push_back(e);
		}
		cout << solve() << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<queue>
#include<functional>
#include<algorithm>
using namespace std;
struct S{int i,c,r;};
struct C:binary_function<S,S,int>{int operator()(S l,S r){return l.c>r.c;}};
int main()
{
	int c,n,m,s,d,i,a,b,f,_=0x3f3f3f3f;
	int g[100][100];
	int x[100][11];
	while(scanf("%d%d%d%d%d",&c,&n,&m,&s,&d),c)
	{
		memset(g,63,sizeof(g));
		memset(x,63,sizeof(x));
		for(i=0;i<m;++i)
		{
			scanf("%d%d%d",&a,&b,&f);
			g[a-1][b-1]=g[b-1][a-1]=f;
		}
		S p,t;
		p.c=0;
		p.r=c;
		p.i=s-1;
		priority_queue<S,vector<S>,C>q;
		q.push(p);
		while(!q.empty())
		{
			p=q.top();q.pop();
			if(x[p.i][p.r]<=p.c)continue;
			x[p.i][p.r]=p.c;
			for(i=0;i<n;++i)
			{
				if(g[p.i][i]==_)continue;
				if(p.r)
				{
					t=p;
					t.r--;
					t.c+=g[t.i][i]/2;
					t.i=i;
					q.push(t);
				}
				t=p;
				t.c+=g[t.i][i];
				t.i=i;
				q.push(t);
			}
		}
		printf("%d\n",*min_element(x[d-1],x[d]));
	}
	return 0;
}