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]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 <queue>
#include <vector>
#include <functional>
#include <algorithm>
#define MAX_V 100
#define INF 1000000
using namespace std;

struct edge{
  int to,cost;
};

typedef pair<int,int> P; //firstは最短経路,secondは頂点番号

vector<edge> G[MAX_V];
int vertexes,edges;
int d[MAX_V][MAX_V];


void dijkstra(int start,int tickets){
  priority_queue<P,vector<P>,greater<P> > que;
  for(int i=0;i<vertexes;i++) d[tickets][i] = INF;
  d[tickets][start] = 0;
  que.push(P(0,start));

  while(!que.empty()){
    P p = que.top();
    que.pop();
    int v = p.second; 
    if(d[tickets][v] < p.first) continue; //異なる2経路のコストで高い方は無視
    for(int i=0;i<G[v].size();i++){
      edge e = G[v][i];
      int minimum = (tickets==0 ? d[tickets][v] + e.cost: min(d[tickets][v] + e.cost, d[tickets-1][v] + e.cost/2));
      if(d[tickets][e.to] > minimum){
        //確定ノードまでのコスト+edgeのコスト
        //がそのノードの現在値より小なら…
        d[tickets][e.to] = minimum;
        que.push( P(d[tickets][e.to],e.to) );
      }
    }
  }
}


int main(){
  int discounts,start,destination;
  while(cin >> discounts >> vertexes >> edges >> start >> destination){
    for(int i=0;i<edges;i++){
      int a,b,fee;
      cin >> a >> b >> fee;
      a--; b--; 
      edge E;
      E.to = b;
      E.cost = fee;
      G[a].push_back(E);

      E.to = a;
      E.cost = fee;
      G[b].push_back(E);
    }
    start--; destination--;
    for(int i=0;i<=discounts;i++) dijkstra(start,discounts);
    cout << d[discounts][destination] << endl;
    for(int i=0;i<MAX_V;i++) G[i].clear();
  }
  return 0;
}

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

const int INF = 100000000;
const int C_MAX = 10;
const int N_MAX = 100;

//街の情報 <残りの割引券, 街の番号>
typedef pair<int, int> T;
//地点情報 <最小コスト, <残りの割引券, 街の番号>>
typedef pair<int, T> P;

//c: チケットの枚数(1 <= c <= 10)
//n: バスをつなぐ町の数(2 <= n <= 100)
//m: バスの路線数(1 <= m <= 500)
//s: 出発地点
//d: 目的地
int c, n, m, s, d;
//cost[町の番号][残りの割引券] = 費用
int cost[N_MAX][C_MAX + 1];

//すでにコストの最適値が決定済みかどうか
bool flag[N_MAX][C_MAX + 1];

//町間の料金
int line[N_MAX][N_MAX];

typedef priority_queue<P, vector<P>, greater<P> > Q;
Q dp_q;

int dijkstra(){
	while(!dp_q.empty()){
		P p = dp_q.top(); dp_q.pop();
		T &t = p.second;

		//p.first: 最小コスト
		//t.first: 残りの割引券
		//t.second: 町番号

		//既に最適値なのかどうかを調べる
		if(flag[t.second][t.first])	continue;
		flag[t.second][t.first] = true;

		if(t.second == d){
			return p.first;
		}

		//すべての頂点に対して割引券を使ったか使ってないかを決める
		for(int i = 0; i < n; i++){
			//割引券を使わなかった場合のiのコスト
			//(t.secondでの最小コスト + t.second -> iの料金)が割引券がt.firstの時の町iのコストより小さい
			//時にc[i][t.first]を更新
			if(cost[i][t.first] > p.first + line[t.second][i]){
				cost[i][t.first] = p.first + line[t.second][i];
				dp_q.push(P(p.first + line[t.second][i], T(t.first, i)));
			}

			//割引券を使った場合のiのコスト
			//(t.secondでの最小コスト + t.second -> iの料金の半分)が
			//割引券がt.first - 1の時の町iのコストより小さい時に
			//c[i][t.first - 1]を更新
			if(t.first > 0 && cost[i][t.first - 1] > p.first + line[t.second][i] / 2){
				cost[i][t.first-1] = p.first + line[t.second][i] / 2;
				dp_q.push(P(p.first + line[t.second][i] / 2, T(t.first - 1, i)));
			}
		}
	}

	return INF;
}

void solve(){
	for(int i = 0; i < N_MAX; i++){
		for(int j = 0; j < C_MAX; j++){
			cost[i][j] = INF;
			flag[i][j] = false;
		}
	}

	dp_q.push(P(0, T(c, s)));

	cout << dijkstra() << endl;
}

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

	while(true){
		cin >> c >> n >> m >> s >> d;
		if(!c && !n && !m && !s && !d)	break;
		s--; d--;
		
		for(int i = 0; i < N_MAX; i++){
			for(int j = 0; j < N_MAX; ++j){
				line[i][j] = INF;
			}
		}

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

		solve();
	}
  return 0;
}

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