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<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);
					}
				}
			}
			pq.pop();
		}
		cout<<pq.top().cost<<endl;
	}


	return 0;
}

C++: #include<iostream>
#include<queue>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
	int c,n,m,s,d; while(cin>>c>>n>>m>>s>>d,c){
		s--,d--;
		int a,b,f;
		int ad[n][n],cost[c+1][n];
		rep(i,n)rep(j,n)ad[i][j]=0;
		rep(i,c+1)rep(j,n)cost[i][j]=1<<20;
		rep(i,m)cin>>a>>b>>f,a--,b--,ad[a][b]=ad[b][a]=f;
		queue<pair<int,int> > F; F.push(make_pair(c,s));
		cost[c][s]=0;
		while(!F.empty()){
			int cnode=F.front().second,cc=F.front().first; F.pop();
			rep(i,n)if(ad[cnode][i]){
				if(cost[cc][i]>cost[cc][cnode]+ad[cnode][i])
				cost[cc][i]=cost[cc][cnode]+ad[cnode][i],
				F.push(make_pair(cc,i));
				if(cc>0&&cost[cc-1][i]>cost[cc][cnode]+ad[cnode][i]/2)
				cost[cc-1][i]=cost[cc][cnode]+ad[cnode][i]/2,
				F.push(make_pair(cc-1,i));
			}
		}
		int mi,mn=1<<20;rep(i,c+1)if(cost[i][d]<mn)mi=i,mn=cost[i][d];
		cout<<mn<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <climits>
#include <vector>
using namespace std;
typedef pair<pair<int,int>,int> NODE;
#define cost first.first
#define ticket first.second 
#define pos second
#define ppp(a,b,c) make_pair(make_pair(a,b),c)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF (1<<21)

int info[100][100];
int n;

int dijk(int a,int b,int c){
	priority_queue<NODE,vector<NODE>,greater<NODE> >  Q;
	int memo[100][11];
	rep(i,100)rep(j,11)memo[i][j] = INF;
	rep(j,11)memo[a][j] = 0;
	
	Q.push(ppp(0,c,a));
	while( !Q.empty() ){
		NODE q = Q.top();Q.pop();
		for(int i=0;i<n;i++){
			if( info[q.pos][i]+q.cost < memo[i][q.ticket]){
				 memo[i][q.ticket] = info[q.pos][i]+q.cost;
				Q.push(ppp(info[q.pos][i]+q.cost ,q.ticket,i));
			}
			if( q.ticket && info[q.pos][i]/2+q.cost < memo[i][q.ticket-1] ){
					Q.push( ppp( info[q.pos][i]/2+q.cost , q.ticket-1, i));
				 memo[i][q.ticket-1] = info[q.pos][i]/2+q.cost;
			}
		}
	}
	int mi = (1<<21);
	rep(j,11)mi = min(memo[b][j],mi);
	return mi;
}
int main(){
	int c,m,s,d,a,b,f;
	while(cin >> c >> n >> m >> s >> d ,c ){
		s--;d--;
		rep(i,100)rep(j,100)info[i][j]=INF*2;
		
		for(int i=0;i<m;i++){
			cin >> a >> b >> f;
			a--;b--;
			info[a][b] = info[b][a] = f;
		}
		cout << dijk(s,d,c) << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define INF 1000000000;
#define dis first
#define mati second.first
#define sta  second.second
using namespace std;
typedef pair <int ,pair <int,int> > P;
vector <P> mp[102];
int c,n,m,start,goal;

int bit_count(int a) {
  a = (a & 0x55555555)+((a & 0xAAAAAAAA) >> 1 );
  a = (a & 0x33333333)+((a & 0xCCCCCCCC) >> 2 );
  a = (a & 0x0F0F0F0F)+((a & 0xF0F0F0F0) >> 4 );
  a = (a & 0x00FF00FF)+((a & 0xFF00FF00) >> 8 );
  a = (a & 0x0000FFFF)+((a & 0xFFFF0000) >> 16);
  return a;
}

void henkan(int n,int kai) {
  if(kai <= 10) {
    henkan(n/2,kai+1);  
  cout << n % 2 ;
  if(kai == 0) cout << endl;
  }
}

int dijkstra() {
  int flg[1<<10][102] = {},dis[1<<10][102];
  for(int j=0;j<1<<n;j++)
    for(int i=0;i<=n;i++) dis[j][i] = -INF;

  for(int j=1;j<1<<n;j++)flg[j][start] = 1;

  priority_queue <P> Q;
  P t;
  t.dis = 0, t.mati = start, t.sta = 0;
  Q.push(t);

  while(!Q.empty()) {
    t = Q.top(),Q.pop();
    if(flg[t.sta][t.mati] == 1) continue;
    flg[t.sta][t.mati] = 1;
    for(int i=0;i<mp[t.mati].size();i++){
      int ndis = mp[t.mati][i].dis;
      int nmati = mp[t.mati][i].mati;
      int nsta = t.sta | (1<<(nmati-1));
      P tt;
      if(dis[t.sta][nmati] < t.dis-ndis && flg[t.sta][nmati] == 0) {
	tt.dis = t.dis-ndis,tt.mati = nmati,tt.sta = t.sta;
	dis[t.sta][nmati] = t.dis-ndis;
	Q.push(tt);
      }
      if(bit_count(nsta)<= c&&dis[nsta][nmati]<t.dis-ndis/2 && flg[nsta][nmati]==0){
	tt.dis = t.dis - ndis/2 , tt.mati = nmati, tt.sta = nsta;
	dis[nsta][nmati] = t.dis-ndis/2;
	Q.push(tt);
      }
    }
  }

  int ans = -INF;
  for(int j=0;j<1<<n;j++)ans = max(ans,dis[j][goal]);
   
  return -ans;
}


int main() {

  while(1) {
    cin >> c >> n >> m >> start >> goal;
    if(c==0&&n==0&&m==0&&start==0&&goal==0) break;

    for(int i=0;i<=n;i++) mp[i].clear();
    for(int i=0;i<m;i++) {
      int a,b,f;
      cin >> a >> b >> f;
      P t;
      t.dis = f,  t.mati = b , t.sta = 0;
      mp[a].push_back(t);
      t.mati = a;
      mp[b].push_back(t);
    }

    cout << dijkstra() << endl;
  }

  return 0;
}