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 <functional>
#include <algorithm>
#include <map>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef pair<P, int> State;

const int N = 100, INF = 1e8;

int edge[N][N];

int dijkstra(int n, int s, int d, int c){
	priority_queue<State, vector<State>, greater<State> > q;
	int visited[N][11] = {}, res;
	for(int i(0);i < N;++i){
		for(int j(0);j <= 10;++j){
			visited[i][j] = INF;
		}
	}
	q.push(State(P(0, s), c));
	while(!q.empty()){
		int c = q.top().first.first;
		int curr = q.top().first.second;
		int dc = q.top().second;
		q.pop();
        if(c > visited[curr][dc]) continue;
		for(int i(0);i < n;++i){
			if(edge[curr][i] != INF){
				int nc = c + edge[curr][i];
				int next = i;
				if(visited[next][dc] > nc){
                    visited[next][dc] = nc;
					q.push(State(P(nc, next), dc));
				}
				nc = c + edge[curr][next] / 2;
				if(dc > 0 && visited[next][dc - 1] > nc){
                    visited[next][dc - 1] = nc;
					q.push(State(P(nc, next), dc - 1));
				}
			}
		}
	}
	res = INF;
	for(int i(0);i < n;++i){
		res = min(res, visited[d][i]);
	}
    return res;
}

int main(){
	int c, n, m, s, d, a, b, f;
	while(cin >> c >> n >> m >> s >> d, c && n && m && s && d){
		for(int i(0);i < N;++i){
			for(int j(i);j < N;++j){
				edge[i][j] = edge[j][i] = INF;
			}
		}
		--s;
		--d;
		for(int i(0);i < m;++i){
			cin >> a >> b >> f;
			--a;
			--b;
			edge[a][b] = edge[b][a] = f;
		}
        cout << dijkstra(n, s, d, c) << endl;
	}
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <utility>
#include <climits>

using namespace std;

int c, n, m, s, d;

struct N {
  int cost, ticket, stop;
  bool operator < (const N &t)const {
    return cost > t.cost;
  }
  N(int cost, int ticket, int stop) :
    cost(cost), ticket(ticket), stop(stop){}
  void print() {
    printf("cost = %d, ticket = %d, stop = %d\n", cost, ticket, stop);
  }
};

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

edge G[100];
int visited[100][11];

const int MAX = INT_MAX;

int solve()
{
  for (int i = 0; i < 100; i++) {
    fill(visited[i], &visited[i][11], MAX);
  }
  priority_queue<N> Q;
  Q.push(N(0, c, s));
      
  //printf("ok\n");  
      
  while (true) {    
    N cur = Q.top();
    Q.pop();

    //cur.print();
    int cc = cur.cost, ct = cur.ticket, cs = cur.stop;
    if (cs == d) {
      return cc;
    }

    for (int i = 0; i < G[cs].size(); i++) {
      int t = G[cs][i].first, ec = G[cs][i].second;
      if (cc + ec < visited[t][ct]) {
        visited[t][ct] = cc + ec;
        Q.push(N(visited[t][ct], ct, t));
      }
      if (ct > 0 && cc + ec / 2 < visited[t][ct - 1]) {
        visited[t][ct - 1] = cc + ec / 2;
        Q.push(N(visited[t][ct - 1], ct - 1, t));
      }
    }
  }                
}

int main()
{
  while (scanf("%d %d %d %d %d ", &c, &n, &m, &s, &d) == 5) {
    if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) {
      break;
    }
    s--, d--;
    for (int i = 0; i < n; i++) {
      G[i].clear();
    }
    for (int i = 0; i < m; i++) {
      int a, b, cost;
      scanf("%d %d %d ", &a, &b, &cost);
      //printf("%d %d %d\n", a, b, cost);
      a--, b--;
      G[a].push_back(make_pair(b, cost));
      G[b].push_back(make_pair(a, cost));
    }
    /*for (int i = 0; i < n; i++) {
      for (int j = 0; j < G[i].size(); j++) {
        printf("%d %d: %d\n", i, G[i][j].first, G[i][j].second);
      }
      }*/
    printf("%d\n", solve());
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define INF (1<<29)
#define all(c) (c).begin(),(c).end()
#define pb(c) push_back(c)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int dis[111][111];
int c, n, m, s, d;
int next[111][11];

void dfs(int cur, int sum, int cost){
  if(next[cur][sum] < cost) return;
  next[cur][sum] = cost;
  for(int i = 0; i < n; i++){
    if(dis[cur][i] == (1<<29)) continue;
    if(sum > 0) dfs(i, sum-1, cost+(dis[cur][i]/2));
    dfs(i, sum, cost+dis[cur][i]);
  }
}

int main(){
  ios::sync_with_stdio(false);

  while(cin >> c >> n >> m >> s >> d && (c||n||m||s||d)){

    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        dis[i][j] = next[i][j] = (1<<29);
      }
    }

    for(int i = 0; i < m; i++){
      int a, b, f;
      cin >> a >> b >> f;
      a--; b--;
      dis[a][b] = f;
      dis[b][a] = f;
    }

    dfs(s-1,c,0);
    int ans = INT_MAX;
    for(int i =0; i < c; i++){
      ans = min(ans, next[d-1][i]);
    }
    cout << ans << endl;
  }


}

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<P> vec[1000]; // F -> pos , S -> cost
int MD[1000][13];
const int INF = 1<<27;

//C++だとなぜかRuntimeErrorになったので仕様変更
//vecにはコストいらないからvector<PP> vec -> vector<P> vec へ


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++){
      P po = vec[v][i]; //vectorのPPは意味が違うので注意！！！
      //if(MD[v][po.S] < po.F.S)continue;
      if(MD[po.F][p.S] > p.F.F + po.S){
	MD[po.F][p.S] = p.F.F+po.S;
	que.push(PP(P(MD[po.F][p.S],po.F),p.S));
      }
      if(p.S > 0 && MD[po.F][p.S-1] > p.F.F + po.S/2 ){
	MD[po.F][p.S-1] = p.F.F+po.S/2;
	que.push(PP(P(MD[po.F][p.S-1],po.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(P(y,co));
      vec[y].push_back(P(x,co));
    }
    dijkstra();
    int ans = INF;
    for(int i=0;i<=c;i++){
      ans = min(ans,MD[d][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

//Version.1