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<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
using namespace std;

const int INF=10000000;
typedef struct edge{
  int t,c;
  edge(){}
  edge(int t,int c):t(t),c(c){}
};
typedef pair<int ,pair<int,int> > P;


vector<edge> g[105];
int id[11][105];
int c,n,m,s,d;

int main(void){
  while(1){
    scanf("%d %d %d %d %d",&c,&n,&m,&s,&d);
    if(!c && !n && !m && !s && !d) break;
    for(int i=0;i<=n;i++)g[i].clear();

    for(int i=0;i<m;i++){
      int a,b,f;
      scanf("%d %d %d",&a,&b,&f);
      g[a].push_back(edge(b,f));
      g[b].push_back(edge(a,f));
    }

    priority_queue<P,vector<P>,greater<P> > que;
    for(int i=0;i<=c;i++) fill(id[i],id[i]+n+1,INF);
    for(int i=0;i<10;i++)id[i][s]=0;
    que.push(P(0,pair<int,int>(s,c)));
    while(!que.empty()){
      P p=que.top();
      que.pop();
      int v=p.second.first;
      int nc=p.second.second;
      if(v == d) {
	printf("%d\n",id[c-nc][v]);
	break;
      }
      if(id[c-nc][v]<p.first) continue;
      for(int i=0;i<g[v].size();i++){
	edge e = g[v][i];
	if(id[c-nc][e.t]>id[c-nc][v]+e.c){
	  id[c-nc][e.t]=id[c-nc][v]+e.c;
	  que.push(P(id[c-nc][e.t],pair<int,int>(e.t,nc)));
	}
	if(nc>0 && id[c-nc+1][e.t]>id[c-nc][v]+e.c/2){
	  id[c-nc+1][e.t]=id[c-nc][v]+e.c/2;
	  que.push(P(id[c-nc+1][e.t],pair<int,int>(e.t,nc-1)));
	}
      }
    }
    /*
    for(int i=1;i<=n;i++) printf("%d:%d  ",i,id[c][i]);
    puts("");
    printf("%d\n",id[c][d]);
    */
  }
}

C++: #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(A)		(A).begin(),(A).end()
#define DUMP(A)    cout<<#A<<"="<<(A)<< endl
#define SIZE(A)    (int)((A).size())
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> PP;

int main(){
    int c,n,m,s,d;
    for(;;){
        scanf("%d%d%d%d%d",&c,&n,&m,&s,&d);
        if(c==0&&n==0&&m==0&&s==0&&d==0) break;
        s--;d--;
        int path[100][100];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                path[i][j]=-1;
            }
        }
        int fr,to,co;
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&fr,&to,&co);
            fr--;to--;
            path[fr][to]=path[to][fr]=co;
        }
        int dist[100][11];
        for(int i=0;i<n;i++){
            for(int j=0;j<=c;j++){
                dist[i][j]=-1;
            }
        }
        priority_queue<PP> pque;
        pque.push(PP(0,P(0,s)));
        dist[s][0]=0;
        while(!pque.empty()){
            PP now=pque.top();
            pque.pop();
            int ndi=now.first,ncar=now.second.first,nat=now.second.second;
            if(dist[nat][ncar]!=-1 && dist[nat][ncar]<ndi) continue;
            for(int i=0;i<n;i++){
                if(path[nat][i]!=-1 && (dist[i][ncar]==-1 || ndi+path[nat][i]<dist[i][ncar])){
                    pque.push(PP(ndi+path[nat][i],P(ncar,i)));
                    dist[i][ncar]=ndi+path[nat][i];
                }
                if(path[nat][i]!=-1 && ncar+1<=c && (dist[i][ncar+1]==-1 || ndi+path[nat][i]/2 < dist[i][ncar+1])){
                    pque.push(PP(ndi+path[nat][i]/2,P(ncar+1,i)));
                    dist[i][ncar+1]=ndi+path[nat][i]/2;
                }
            }
        }
        int mi=INT_MAX;
        for(int i=0;i<=c;i++){
            if(dist[d][i]!=-1 && dist[d][i]<mi) mi=dist[d][i];
        }
        printf("%d\n",mi);
    }
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;// F -> to, S -> cost
typedef pair<P,int> PP;// F->P(to,cost), S->discount
int c,n,m,s,d;
vector<PP> vec[1000];
int MD[1000][13];
const int INF = 1<<29;

void dijkstra(){
  priority_queue<PP,vector<PP>,greater<PP> > que;
  rep(i,n+1)rep(j,13)MD[i][j] = INF;
  //syokika
  MD[s][c] = 0;
  que.push(PP(P(0,s),c));//dis,start,discount

  while(!que.empty() ){
    PP p = que.top();que.pop(); //priority_queのPPと
    int v = p.F.S;
    
    for(int i=0;i<vec[v].size();i++){
      PP po = vec[v][i]; //vectorのPPは意味が違うので注意！！！
      //if(MD[v][po.S] < po.F.S)continue;
      if(MD[po.F.F][c] > MD[v][c] + po.F.S){
	MD[po.F.F][c] = MD[v][c]+po.F.S;
	que.push(PP(P(MD[po.F.F][c],po.F.F),c));
      }
      if(po.S > 0 && MD[po.F.F][p.S-1] > MD[v][p.S] + po.F.S/2 ){
	MD[po.F.F][p.S-1] = MD[v][p.S]+po.F.S/2;
	que.push(PP(P(MD[po.F.F][p.S-1],po.F.F),p.S-1));
      }
    }
  }
}


int main(){
  int x,y,co;
  while(true){
    cin >> c >> n >> m >> s >> d;
    if(c+n+m+s+d == 0)break;
    rep(i,1000)vec[i].clear();
    rep(i,m){
      cin >> x >> y >> co;
      vec[x].push_back(PP(P(y,co),c));
      vec[y].push_back(PP(P(x,co),c));
    }
    dijkstra();
    int ans = INF;
    for(int i=0;i<=c;i++){
      ans = min(ans,MD[d][i]);
    }
    cout << ans << endl;
  }
  return 0;
}