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
}

type Memo struct {
	NowPoint             int
	DiscountTicketTCount 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)
		memo := make(map[Memo]struct{})
		for len(pq) != 0 {
			now := heap.Pop(&pq).(*Node)
			if now.NowPosition == d {
				fmt.Println(now.Amount)
				break
			}
			memo[Memo{NowPoint: now.NowPosition, DiscountTicketTCount: now.DiscountTicketTCount}] = struct{}{}
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				if _, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok {
					heap.Push(&pq, next)
				}
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					if _, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok {
						heap.Push(&pq, next)
					}
				}
			}
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

#define loop(i, n) for (int i = 0; i < n; i++)
#define INF 1010001000

typedef pair<int, int> i_i;
typedef struct data{int pos, tick, cost;}data;

int dp(vector<vector<i_i > > &g);

int c, n, m, s, d;

int main(void)
{
    while (cin >> c >> n >> m >> s >> d, c) {
        n++;
        vector<vector<i_i > > g(n);
        loop(i, m) {
            int a, b, f;
            cin >> a >> b >> f;
            g[a].push_back(i_i(f, b));
            g[b].push_back(i_i(f, a));
        }
        cout << dp(g) << endl;
    }
    return 0;
}

int dp(vector<vector<i_i > > &g)
{
    vector<vector<int> > table(c+1, vector<int>(n, INF));
    queue<data> a;
    data dt = {s, c, 0};
    a.push(dt);
    while (!a.empty()) {
        data tmp = a.front(); a.pop();
        for (i_i i : g[tmp.pos]) {
            if (table[tmp.tick][i.second] > tmp.cost + i.first) {
                table[tmp.tick][i.second] = tmp.cost + i.first;
                dt.pos = i.second; dt.tick = tmp.tick; 
                dt.cost = tmp.cost + i.first;
                a.push(dt);
            }
            if (tmp.tick && 
                    table[tmp.tick-1][i.second] > tmp.cost + i.first) {
                table[tmp.tick-1][i.second] = tmp.cost + i.first / 2;
                dt.pos = i.second; dt.tick = tmp.tick-1; 
                dt.cost = tmp.cost + i.first / 2;
                a.push(dt);
            }
        }
    }
    int ret = INF;
    for (int i = 0; i < c+1; i++) {
        ret = min(ret, table[i][d]);
    }
    return ret;
}

C++: #include <iostream>
#include <vector>

using namespace std;

#define INF 100000000

struct edge {
	int to, cost;
};

int main() {
	int c, n, m, s, d;
	while (cin >> c >> n >> m >> s >> d) {
		if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) {
			break;
		}
		vector<edge> ff[101];
		vector< vector<int> > ddd(n+1, vector<int>(n+1, INF));
		int a, b, f;
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> f;
			edge e;
			e.to = b;
			e.cost = f;
			ff[a].push_back(e);
			e.to = a;
			ff[b].push_back(e);
			ddd[a][b] = ddd[b][a] = f;
		}
		for (int i = 1; i <= n; i++) {
			ddd[i][i] = 0;
		}
		for (int k = 1; k <= n; k++) {
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= n; j++) {
					ddd[i][j] = min(ddd[i][j], ddd[i][k]+ddd[k][j]);
				}
			}
		}
		vector<int> isd[101];
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				if (ddd[i][j] != INF) {
					isd[i].push_back(j);
				}
			}
		}
		vector< vector< vector<int> > > dp(n+1, vector< vector<int> >(n+1, vector<int>(c+1, INF)));
		for (int i = 1; i <= n; i++) {
			dp[s][i][0] = ddd[s][i];
		}
		for (int k = 1; k <= c; k++) {
			for (int p = 0; p < isd[s].size(); p++) {
				int pp = isd[s][p];
				dp[s][pp][k] = dp[s][pp][k-1];
				for (int i = 0; i < isd[s].size(); i++) {
					int ii = isd[s][i];
					for (int j = 0; j < isd[ii].size(); j++) {
						int jj = isd[ii][j];
						for (int q = 0; q < ff[jj].size(); q++) {
							int qq = ff[jj][q].to;
							if (ddd[qq][pp] == INF) continue;
							if (dp[s][pp][k] > dp[s][ii][k-1]+ddd[ii][jj]+(ff[jj][q].cost/2)+ddd[qq][pp]) {
								dp[s][pp][k] = dp[s][ii][k-1]+ddd[ii][jj]+(ff[jj][q].cost/2)+ddd[qq][pp];
							}
						}
					}
				}
			}
		}
		cout << dp[s][d][c] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<string.h>
using namespace std;

const int INF = 1<<30;

struct Node{
  int use,to,weight;
  Node() {;}
  Node(int use,int to,int weight) : use(use), to(to), weight(weight) {;}
};
bool operator<(const Node &e,const Node &f) {
  return e.weight > f.weight;
}

int dist[110][110];
bool visit[110];
int dist2[110][110];

int main() {
  int c,n,m,s,d,a,b,f;
    while(cin>>c>>n>>m>>s>>d, c|n|m|s|d) {
      s--,d--;

      for(int i=0; i<110; ++i) {
	for(int j=0; j<110; ++j) {
	  dist[i][j] = INF;
	}
	visit[i] = false;
      }

      for(int i=0; i<110; ++i)
	for(int j=0; j<110; ++j)
	  dist2[i][j] = INF;

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

      priority_queue<Node> Q;
      Q.push(Node(0,s,0));
      visit[s] = true;
      while(!Q.empty()) {
	Node now = Q.top(); Q.pop();

	visit[now.to] = true;
	for(int i=0; i<n; ++i) {
	  if(!visit[i] && dist[now.to][i] != INF) {
	    if(dist2[now.use+1][i]
	       > now.weight + dist[now.to][i]/2 && now.use < c) {
	      dist2[now.use+1][i] = now.weight + dist[now.to][i]/2;
	      Q.push(Node(now.use+1, i, dist2[now.use+1][i]));
	    }
	    if(dist2[now.use][i]
	       > now.weight + dist[now.to][i]) {
	      dist2[now.use][i] = now.weight + dist[now.to][i];
	      Q.push(Node(now.use, i, dist2[now.use][i]));
	    }
	  }
	}
      }
      int sum = INF;
      for(int i=0; i<=c; ++i) {
	//cout<<dist2[i][d]<<" : "<<i<<endl;
	sum = min(sum , dist2[i][d]);
      }
      cout<<sum<<endl;
    }
}