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, n)
		}
		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, m)
		start := Node{NowPosition: s, DiscountTicketTCount: c, Amount: 0}
		heap.Push(&pq, &start)
		memo := make(map[Memo]int)
		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}] = now.Amount
			for _, v := range pointMap[now.NowPosition] {
				next := &Node{v.NextPoint, now.DiscountTicketTCount, now.Amount + v.Fee}
				if memoAmount, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok || (ok && memoAmount > next.Amount) {
					memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}] = next.Amount
					heap.Push(&pq, next)
				}
				if now.DiscountTicketTCount > 0 {
					next = &Node{v.NextPoint, now.DiscountTicketTCount - 1, now.Amount + v.Fee/2}
					if memoAmount, ok := memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}]; !ok || (ok && memoAmount > next.Amount) {
						memo[Memo{NowPoint: v.NextPoint, DiscountTicketTCount: now.DiscountTicketTCount}] = next.Amount
						heap.Push(&pq, next)
					}
				}
			}
		}
		//runtime.GC()
	}
}

C++: #include<iostream>
#include<map>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef pair< int ,int > P;
typedef pair< int , P > road;
main(){
  while(1){
    int c,n,m,s,d;
    int a,b,f;
    vector< P > bus[120];
    int min_cost[120];
    queue< road > que;
    road r;
    P p;
    cin>>c>>n>>m>>s>>d;//c waribiki n mati m rosen s start d goal
    if(c==0&&n==0&&m==0&&s==0&&d==0)break;
    for(int i=1;i<=n;i++){
      min_cost[i]=1<<21;
    }
    for(int i=0;i<m;i++){
      cin>>a>>b>>f;
      p.first=b;
      p.second=f;
      bus[a].push_back(p);
      p.first=a;
      p.second=f;
      bus[b].push_back(p);
    }
    r.first=c;
    r.second.first=s;
    r.second.second=0;
    que.push(r);
    while(!que.empty()){
      road now=que.front();
      int size=bus[now.second.first].size();
      for(int i=0;i<size;i++){
	if(bus[now.second.first][i].second+now.second.second<min_cost[bus[now.second.first][i].first]){
	  min_cost[bus[now.second.first][i].first]=bus[now.second.first][i].second+now.second.second;
	  r.first=now.first;
	  r.second.first=bus[now.second.first][i].first;
	  r.second.second=bus[now.second.first][i].second+now.second.second;
	  que.push(r);
	}
	if(now.first!=0&&bus[now.second.first][i].second/2+now.second.second<min_cost[bus[now.second.first][i].first]){
	  min_cost[bus[now.second.first][i].first]=bus[now.second.first][i].second/2+now.second.second;
	  r.first=now.first-1;
	  r.second.first=bus[now.second.first][i].first;
	  r.second.second=bus[now.second.first][i].second/2+now.second.second;
	  que.push(r); 
	}
      }  
      que.pop();
    }
    while(!que.empty()){
      que.pop();
    }
    cout<<min_cost[d]<<endl;
  }
}

C++: #include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define INF 1e+8
class Data {
public:
	int to,cost,num;
	Data(){}
	Data(int to, int cost, int num):to(to),cost(cost),num(num){}
	bool operator<(const Data &s) const {
		if(cost != s.cost) return cost > s.cost;
		return to < s.to;
	}
};

int main() {
	int c,n,m,s,g;
	while(scanf("%d%d%d%d%d",&c,&n,&m,&s,&g),(c|n|m|s|g)) {
		vector<Data> G[128];
		for(int i = 0; i < m; i++) {
			int a,b,f;
			scanf("%d%d%d",&a,&b,&f);
			G[a].push_back(Data(b,f,0));
			G[b].push_back(Data(a,f,0));
		}
		priority_queue<Data> Q;
		Q.push(Data(s,0,c));
		int ans = -1;
		while(!Q.empty()) {
			Data e = Q.top();Q.pop();
			int v = e.to;
			if(v == g) {
				ans = e.cost;
				break;
			}
			for(int i = 0; i < G[v].size(); i++) {
				Data E = G[v][i];
				if(e.num != 0) {
					Q.push(Data(E.to,E.cost/2 + e.cost,e.num-1));
				}
				Q.push(Data(E.to,E.cost + e.cost,e.num));
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int c,n,m,s,d;
vector<P> G[101];
int cost[101][11];
int INF=10000000;

int dijkstra(int s){
	priority_queue<PP, vector<PP>, greater<PP> >que;
	for(int i=0;i<=100;i++){
		for(int j=0;j<=10;j++){
			cost[i][j]=INF;
		}
	}
	cost[s][0]=0;
	que.push(PP(P(s,0),0));
	while(que.size()){
		PP p=que.top();que.pop();
		int v=p.first.first,d=p.first.second,k=p.second;
		if(cost[v][k]<d)continue;
		for(int i=0;i<G[v].size();i++){
			P e=G[v][i];
			if(cost[e.first][k]>cost[v][k]+e.second){
				cost[e.first][k]=cost[v][k]+e.second;
				que.push(PP(P(e.first,cost[e.first][k]),k));
			}
		}
		if(k<c)for(int i=0;i<G[v].size();i++){
			P e=G[v][i];
			if(cost[e.first][k+1]>cost[v][k]+e.second/2){
				cost[e.first][k+1]=cost[v][k]+e.second/2;
				que.push(PP(P(e.first,cost[e.first][k+1]),k+1));
			}
		}
	}

	int ans=INF;
	for(int i=0;i<=10;i++){
		ans=min(ans,cost[d][i]);
	}
	return ans;
}

int main(void){
	while(1){
		scanf("%d%d%d%d%d",&c,&n,&m,&s,&d);
		if(c==0 && n==0 && m==0 && s==0 && d==0)break;
		for(int i=0;i<=100;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(P(b,f));
			G[b].push_back(P(a,f));
		}
		cout << dijkstra(s) << endl;
	}
    return 0;
}