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 <vector>
#include <queue>
using namespace std;
 
#define MAX 10000000
#define n_MAX 101
#define c_MAX 11

typedef pair<int, pair<int, int> > edge;

int get_cost(const edge& e){
  return e.first;
}

int get_tickets(const edge& e){
  return -(e.second.first);
}

int get_station(const edge& e){
  return e.second.second;
}

inline edge make_edge(int cost, int tickets, int station){
  return make_pair(cost, make_pair(-tickets, station));
}

int main() {
 
  while( true ) {
    
    long long int c, n, m, s, d;
    cin >> c >> n >> m >> s >> d;
    if ( n == 0 ) break;
    
    long long int l[n_MAX][n_MAX] = {0};
    
    for ( long long int i = 0; i < m; i++ ) {
      long long int a, b, f;
      cin >> a >> b >> f;
      l[a][b] = l[b][a] = f;
    }
 
    long long int t[c_MAX][n_MAX];
    for ( long long int j = 0; j <= c; j++ ) {
      for ( long long int i = 1; i <= n; i++ ) {
	t[j][i] = MAX;
      }
    }
    
    priority_queue<edge, vector<edge>, less<edge> > queue;
    queue.push(make_edge(0, c, s));
    t[c][s] = 0;

    int ans = MAX;
    while(!queue.empty()){
      edge e = queue.top(); queue.pop();

      const int cost = get_cost(e);
      const int tickets = get_tickets(e);
      const int station = get_station(e);
      if(t[tickets][station] > cost){
	continue;
      }else if(station == d){
	ans = min(ans, cost);
      }

      for(long long int next = 1; next <= n; next++){
	if(l[station][next] == 0){
	  continue;
	}
	
	const int next_non_use_cost = cost + l[station][next];
	const int next_use_cost     = cost + l[station][next] / 2;
 
	if(tickets > 0 && t[tickets - 1][next] > next_use_cost){
	  t[tickets - 1][next] = next_use_cost;
	  queue.push(make_edge(next_use_cost, tickets - 1, next));
	}
	
	if(t[tickets][next] > next_non_use_cost){
	  t[tickets][next] = next_non_use_cost;
	  queue.push(make_edge(next_non_use_cost, tickets, next));
	}
      }
    }
 
    cout << ans << endl;
  }
 
  return 0;
 
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <map>
#define INF (1 << 26)
#define fr first
#define sc second
using namespace std;

typedef pair<int,int> S;
typedef pair<S,int> P;
struct edge{
    int to,cost; edge(){}
    edge(int to,int cost):to(to),cost(cost){}
};

int c,n,m,s,d,costed[1024][101];
vector< vector<edge> > info(1024);

void init(){
    for(int i=0;i<1024;i++){
        for(int j=0;j<101;j++) costed[i][j] = INF;
    }
    info.resize(0); info.resize(1024);
}

void add_info(){
    int a,b,f;
    cin >> a >> b >> f;
    info[a].push_back(edge(b,f));
    info[b].push_back(edge(a,f));
}

int Dijkstra(){
    priority_queue<P,vector<P>,greater<P> > que;
    que.push(P(S(0,s),c));  costed[s][c] = 0;
    while(!que.empty()){
        P p = que.top(); que.pop();
        int now = p.fr.sc,c = p.fr.fr,tic = p.sc;
        if(now == d) return c;
        for(int i=0;i<info[now].size();i++){
            edge e = info[now][i];
            if(e.cost + c < costed[e.to][tic]){
                que.push(P(S(e.cost+c,e.to),tic));
                costed[e.to][tic] = e.cost + c;
            }
            if(tic != 0){
                if(e.cost/2 + c < costed[e.to][tic-1]){
                    que.push(P(S(c+e.cost/2,e.to),tic-1));
                    costed[e.to][tic-1] = e.cost/2 + c;
                }
            }
        }
    }
}

int main(){
    while(cin>>c>>n>>m>>s>>d,c||n||m||s||d){
        init();
        for(int i=0;i<m;i++) add_info();
        cout << Dijkstra() << endl;
    }
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
struct edge{int to,cost;};
int dp[100][11];
vector<edge> G[100];
int main(){
      int c,n,m,s,d;
      while(cin>>c>>n>>m>>s>>d&&(c||n||m||s||d)){
            s--;d--;
            REP(i,100)REP(j,11)dp[i][j]=INF;
            REP(i,100)G[i].clear();
            REP(i,m){
                  int x,y,z;
                  cin>>x>>y>>z;x--;y--;
                  G[x].PB((edge){y,z});
                  G[y].PB((edge){x,z});
            }
            dp[s][0]=0;
            queue<int> Q;
            Q.push(s);
            while(!Q.empty()){
                  int v=Q.front();Q.pop();
                  //cout<<v<<" "<<G[v].size()<<endl;
                  REP(i,G[v].size()){
                        bool f=false;
                        edge e=G[v][i];
                        int to=e.to;
                        REP(i,c+1){
                              //cout<<dp[v][i]+e.cost<<endl;;
                              if(dp[to][i]>dp[v][i]+e.cost){
                                    dp[to][i]=dp[v][i]+e.cost;
                                    f=true;
                              }
                        }
                        REP(i,c){
                              if(dp[to][i+1]>dp[v][i]+e.cost/2){
                                    dp[to][i+1]=dp[v][i]+e.cost/2;
                                    f=true;
                              }
                        }
                        if(f)Q.push(to);
                  }
            }
            int ans=INF;
            REP(i,c+1)ans=min(ans,dp[d][i]);
            cout<<ans<<endl;
      }
}