Go: package main

import (
	"container/heap"
	"fmt"
)

type Node struct {
	NowPosition          int
	DiscountTicketTCount int
	Amount               int
}

type PriorityQueueNode []*Node

func (pq PriorityQueueNode) Len() int {
	return len(pq)
}

func (pq PriorityQueueNode) Less(i, j int) bool {
	return pq[i].Amount < pq[j].Amount
}

func (pq PriorityQueueNode) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *PriorityQueueNode) Push(node interface{}) {
	*pq = append(*pq, node.(*Node))
}

func (pq *PriorityQueueNode) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	*pq = old[0 : n-1]
	return item
}

type Point struct {
	NextPoint int
	Fee       int
}

func main() {
	var c, n, m, s, d int
	var a, b, f int

	for {
		fmt.Scan(&c, &n, &m, &s, &d)
		if c == 0 && n == 0 && m == 0 && s == 0 && d == 0 {
			break
		}
		s--
		d--
		pointMap := make([][]Point, n)
		for i := 0; i < n; i++ {
			pointMap[i] = make([]Point, 0)
		}
		for i := 0; i < m; i++ {
			fmt.Scan(&a, &b, &f)
			a--
			b--
			pointMap[a] = append(pointMap[a], Point{NextPoint: b, Fee: f})
			pointMap[b] = append(pointMap[b], Point{NextPoint: a, Fee: f})
		}
		pq := make(PriorityQueueNode, 0)
		start := Node{NowPosition: s, DiscountTicketTCount: c, Amount: 0}
		heap.Push(&pq, &start)
		for len(pq) != 0 {
			now := heap.Pop(&pq).(*Node)
			if now.NowPosition == d {
				fmt.Println(now.Amount)
				break
			}
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				heap.Push(&pq, next)
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					heap.Push(&pq, next)
				}
			}
		}
	}
}

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