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>
 
using namespace std;
 
#define MAX 120
#define INF 1e9
 
struct Edge {
    int to, cost;
    Edge(int t, int c) : to(t), cost(c) {}
};
 
struct State {
    int cost, v, tic;
    State(int c, int v, int t) : cost(c), v(v), tic(t) {}
    bool operator < (const State &s) const {
	return cost > s.cost;
    }
};
 
int C, V, E, src, dst;
vector<Edge> G[MAX];
 
int dijkstra()
{
    priority_queue<State> Q;
    Q.push(State(0, src, C));
    int dist[MAX][11];
    fill(dist[0], dist[0]+MAX*11, INF);
    dist[src][C] = 0;
   
    while (!Q.empty()) {
	State s = Q.top(); Q.pop();
	int v = s.v,t = s.tic;
	if (dist[v][t] < s.cost) continue;
	if (v == dst) return dist[v][t];
     
	for (int i = 0; i < (int)G[v].size(); i++) {
	    Edge e = G[v][i];
	    if (dist[v][t] + e.cost < dist[e.to][t]) {
		dist[e.to][t] = dist[v][t] + e.cost;
		Q.push(State(dist[e.to][t], e.to, t));
	    }
	    if (t > 0) {
		if (dist[v][t] + e.cost/2 < dist[e.to][t-1]) {
		    dist[e.to][t-1] = dist[v][t] + e.cost/2;
		    Q.push(State(dist[e.to][t-1], e.to, t-1));
		}
	    }
	}
    }
}
 
int main()
{
    while (cin >> C >> V >> E >> src >> dst, C) {
	src--; dst--;
	for (int i = 0; i < V; i++) {
	    G[i].clear();
	}
	for (int i = 0; i < E; i++) {
	    int a,b,c;
	    cin >> a >> b >> c; a--; b--;
	    G[a].push_back(Edge(b, c));
	    G[b].push_back(Edge(a, c));
	}
	cout << dijkstra() << endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

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

class PQ{
public:
	PQ(){
		array = new int[100];
		n = 0;
	}
	void push(int arg_info){
		array[n] = arg_info;
		int tmp = n;
		while(tmp != 0 && array[tmp] < array[(tmp-1)/2]){
			swap(array[tmp], array[(tmp-1)/2]);
			tmp = (tmp-1)/2;
		}
		n++;
	}

	bool empty(){
		return n == 0;
	}

	int get(int i){
		return array[i];
	}

	int pop(){
		int ret = array[0];
		n--;
		swap(array[0],array[n]);
		int tmp = 0;
		while(2*tmp+1 <= n-1){
			if(2*tmp+2 <= n-1){
				if(array[2*tmp+2] < array[tmp] && array[2*tmp+2] <= array[2*tmp+1]){
					swap(array[2*tmp+2],array[tmp]);
					tmp = 2*tmp+2;
				}else if(array[2*tmp+1] < array[tmp] && array[2*tmp+1] <= array[2*tmp+2]){
					swap(array[2*tmp+1],array[tmp]);
					tmp = 2*tmp+1;
				}else{
					break;
				}
			}else{
				if(array[tmp] > array[2*tmp+1]){
					swap(array[tmp],array[2*tmp+1]);
					tmp = 2*tmp+1;
				}else{
					break;
				}
			}
		}
		return ret;
	}

private:
	int* array;
	int n;
};


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

void func(Info info){
	if(info.current_town == goal){

		int sum = 0;

		if(num_ticket >= info.index){
			while(!info.Q.empty()){
				sum += info.Q.pop();
			}
			sum /= 2;
			minimum = min(minimum,sum);
		}else{

			int count = 0,tmp;
			while(!info.Q.empty()){
				tmp = info.Q.pop();
				if(count < num_ticket){
					sum += tmp/2;
				}else{
					sum += tmp;
				}
				count++;
			}
			minimum = min(minimum,sum);
		}
		return;
	}

	for(int i = 1; i <= num_town; i++){
		if(info.visited[i] == false && costTable[info.current_town][i] != -1){

			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;
			new_info.index = info.index+1;

			for(int k = 0; k < info.index; k++){
				new_info.Q.push(info.Q.get(k));
			}

			new_info.Q.push(costTable[info.current_town][i]);
			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;
}

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

struct node{
  int cost;
  int c;
  int now;
  node(int C,int cc,int n):cost(C),c(cc),now(n){}
  bool operator >(const node &e) const{
    return cost > e.cost;
  }
};

int main()
{
  int c,n,m,s,d;

  for(;cin>>c>>n>>m>>s>>d,c;)
    {
      int edge[n][n];
      bool close[c+1][n];
      memset(edge,-1,sizeof(edge));
      memset(close,false,sizeof(close));      
      for(int i=0;i<m;i++)
	{
	  int a,b,f;
	  cin>>a>>b>>f;
	  a--;
	  b--;
	  edge[a][b]=f;
	  edge[b][a]=f;
	}
      s--;
      d--;
      priority_queue<node,vector<node>,greater<node> >qu;
      qu.push(node(0,c,s));
      while(!qu.empty())
	{
	  node tmp = qu.top();
	  //	  cout<<tmp.now<<" "<<tmp.cost<<" "<<tmp.c<<endl;
	  qu.pop();
	  if(close[tmp.c][tmp.now])
	    continue;
	  close[tmp.c][tmp.now]=true;
	  if(tmp.now==d)
	    {
	      cout<<tmp.cost<<endl;
	      break;
	    }
	  for(int i=0;i<n;i++)
	    {
	      if(edge[tmp.now][i]!=-1)
		{
		  if(!close[tmp.c][i])
		    qu.push(node(edge[tmp.now][i]+tmp.cost,tmp.c,i));
		  if(tmp.c>0 && !close[tmp.c-1][i])
		    qu.push(node(edge[tmp.now][i]/2+tmp.cost,tmp.c-1,i));
		}
	    }
	}
    }

}