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<stdio.h>
#define INF 10000000
int c,n,m,s,g;
int e[100][100];
int d[11][100];
int i,j;
bool used[11][100];

int min(int a,int b){return a<b?a:b;}

int main(){
	while(1){
		scanf("%d %d %d %d %d",&c,&n,&m,&s,&g);
		if(c==0)return 0;
		for(i=0;i<n;i++)for(j=0;j<n;j++)e[j][i]=INF;
		for(i=0;i<m;i++){
			int a,b,f;
			scanf("%d %d %d",&a,&b,&f);
			e[a-1][b-1]=f;
			e[b-1][a-1]=f;
		}
		for(i=0;i<n;i++)for(j=0;j<=c;j++){
			d[j][i]=INF;
			used[j][i]=false;
		}
		d[c][s-1]=0;
		while(1){
			int nei=-1;
			int nej;
			for(i=0;i<n;i++)for(j=0;j<=c;j++){
				if(!used[j][i]&&(nei==-1||d[j][i]<d[nej][nei])){
					nei=i;
					nej=j;
				}
			}
			if(nei==-1)break;
			used[nej][nei]=true;
			for(i=0;i<n;i++){
				d[nej][i]=min(d[nej][i],d[nej][nei]+e[nei][i]);
				if(nej>0)d[nej-1][i]=min(d[nej-1][i],d[nej][nei]+e[nei][i]/2);
			}
		}
		int ans=INF;
		for(i=0;i<=c;i++)ans=min(ans,d[i][g-1]);
		printf("%d\n",ans);
	}
}

C++: #include<iostream>
using namespace std;
const int INF = (1<<21);
int G[101][101];
int c,n,m,s,d;

int dij(){
  int D[101][11];
  bool V[101][11];
  for(int i=0;i<n;i++){
    for(int j=0;j<c+1;j++){
      D[i][j]=INF;
      V[i][j]=false;
    }
  }
  D[s][c]=0;
  while(1){
    int minv=INF,u,w;
    for(int i=0;i<n;i++){
      for(int j=0;j<c+1;j++){
	if(!V[i][j] && minv>D[i][j]){
	  minv=D[i][j];
	  u=i;
	  w=j;
	}
      }
    }
    
    V[u][w]=true;
    if(minv == INF) break;
    for(int i=0;i<n;i++){
      if(G[u][i] == INF) continue;
      D[i][w]=min(D[i][w],D[u][w]+G[u][i]);
      if( w ){
	D[i][w-1]=min(D[i][w-1],D[u][w]+G[u][i]/2);
      }
    }
  }

  int minv=INF;
  for(int i=0;i<c+1;i++){
    if(D[d][i]<minv){
      minv=D[d][i];
    }
  }

  return minv;  
}

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

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<climits>

using namespace std;

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

struct Edge{
  int to,cost;
  Edge(int _to, int _cost):to(_to), cost(_cost){};
};

int minCost[101][11];
vector<Edge> edge[101];
static const int INF = INT_MAX;

int c,n,m,s,d;

int dijkstra(void){
  priority_queue<P, vector<P>, greater<P> >que;
  que.push(make_pair(0,make_pair(s,c)));

  for(int i=0;i<101;i++)
    for(int j=0;j<11;j++)
      minCost[i][j]=INF;

  while(!que.empty()){
    P p = que.top();
    que.pop();
    
    for(int i=0;i<edge[p.second.first].size();i++){
      Edge e = edge[p.second.first][i];
      if(p.first+e.cost < minCost[e.to][p.second.second]){
	minCost[e.to][p.second.second] = p.first+e.cost;
	que.push(make_pair(p.first+e.cost,make_pair(e.to,p.second.second)));
      }
    }
    
    if(p.second.second==0)continue;
    
    for(int j=0;j<edge[p.second.first].size();j++){
      Edge e = edge[p.second.first][j];
      if(p.first+e.cost/2 < minCost[e.to][p.second.second-1]){
	minCost[e.to][p.second.second-1] = p.first+e.cost/2;
	que.push(make_pair(p.first+e.cost/2,make_pair(e.to,p.second.second-1)));
      }
    }
  }
  int ans=INT_MAX;
  for(int i=0;i<11;i++)
    ans=min(minCost[d][i],ans);

  return minCost[d][0];
}

int main(void){
  int a,b,f;

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

    for(int i=0;i<=n;i++)edge[i].clear();

    for(int i=0;i<m;i++){
      cin >> a >> b >> f;
      edge[a].push_back(Edge(b,f));
      edge[b].push_back(Edge(a,f));
    }

    cout << dijkstra() << 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[100][100];
bool visit[100];
int dist2[100][100];

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<100; ++i) {
	for(int j=0; j<100; ++j) {
	  dist[i][j] = INF;
	}
	visit[i] = false;
      }

      for(int i=0; i<100; ++i)
	for(int j=0; j<100; ++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();
	if(now.to == d) {
	  cout<<now.weight<<endl;
	  break;
	}

	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]));
	    }
	  }
	}
      }
    }
}