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<map>
#include<vector>
#include<queue>
using namespace std;

#define fr first
#define sc second
typedef pair<int,int> Pi;
typedef pair<Pi,int> Pii;
struct edge{
  int to,cost;
};
typedef vector< vector< edge > > Graph;

int Dijkstr(Graph& info,int s,int g,int c){
  vector< vector<int> > min_cost;
  priority_queue<Pii,vector<Pii>,greater<Pii> > que;
  min_cost.resize(c+1,vector<int>(info.size(),-1)); //min_costの確保と共に、全てに-1を代入。
  
  que.push( Pii(Pi(0,s),c) ); //fr:現在額。sc:現在地。
  min_cost[c][s] = 0;
  while(!que.empty()){
    Pii p = que.top();//fr.fr:現在額。fr.sc:現在地。sc:割引券。
    que.pop();
    if(p.fr.sc == g) return p.fr.fr;
    if(p.fr.fr > min_cost[p.sc][p.fr.sc]) continue; //最も小さな値より大きい時はまわらない。
    for(int i=0;i<info[p.fr.sc].size();i++){ //p.scから伸びる道の数だけまわる。
      edge& e = info[p.fr.sc][i];//p.scから伸びる道の一つ。
      if(min_cost[p.sc][e.to] == -1 || p.fr.fr+e.cost < min_cost[p.sc][e.to]){//まだ行っていないか、いままでで一番安いか。
	min_cost[p.sc][e.to] = p.fr.fr+e.cost;//min_costを更新。
	que.push( Pii(Pi(min_cost[p.sc][e.to],e.to),p.sc));//移動後を現在額と現在値として記録。
      }
      if(p.sc > 0 && (min_cost[p.sc-1][e.to] == -1 || p.fr.fr+(e.cost/2) < min_cost[p.sc-1][e.to])){
	min_cost[p.sc-1][e.to] = p.fr.fr+e.cost/2;//min_costを更新。
	que.push( Pii(Pi(min_cost[p.sc-1][e.to],e.to),p.sc-1));//移動後を現在額と現在値として記録。
      }
    }
  }
  
  return -1;
}


int main(){
  int c,n,m,s,d;
  while(1){
    Graph info;
    cin >> c >> n >> m >> s >> d;
    if(c==0 && n==0 && m==0 && s==0 && d==0) break;
    info.resize(m + 1);
    int a,b,f;
    for(int i=0;i<m;i++){
      cin >> a >> b >> f;
      info[a].push_back((edge){b,f});
      info[b].push_back((edge){a,f});
    }
    
    //経路探索
    cout << Dijkstr(info,s,d,c) << endl;
  }
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge{int to,from,cost;};

#ifdef DEBUGLOCAL
#define debug cout
#else
stringstream __ss__;
#define debug __ss__
#endif

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) debug << *i << " "; debug << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

#define rep(i,n) for(int i=0;i<(n);++i)
#define repd(i,n) for(int i=(n)-1;i>=0;i--)
#define repn(i,m,n) for(int i=(m);i<=(n);++i)
#define repnd(i,m,n) for(int i=(n)-1;i>=(m);i--)
#define rep0(i,n) for(i=0;i<(n);++i)
#define all(n) n.begin(),n.end()
#define sz(n) ((int)(n).size())
#define IL for(;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug<<"LINE:"<<__LINE__<<endl;

const int INF = 2147483647/3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[]={1,-1,0,0,1,-1,1,-1,0};
const int dy[]={0,0,1,-1,1,-1,-1,1,0};

int c,n,m,s,d;
int edge[1111][1111];
int cost[111][1111];

int main() {
	IL{
		cin>>c>>n>>m>>s>>d;
		--s,--d;
		if(c==0)break;
		int fr,to,co;
		rep(i,111)rep(j,111)edge[i][j]=INF;
		rep(i,m){
			cin>>fr>>to>>co;
			--fr,--to;
			chmin(edge[fr][to],co);
			chmin(edge[to][fr],co);
		}
		rep(i,11)rep(j,111)cost[i][j]=INF;
		priority_queue<pair<int,pint>,vector<pair<int,pint> >,greater<pair<int,pint> > > que;
		que.push(MP(0,MP(0,s)));
		while(!que.empty()){
			pair<int,pint> p;
			p=que.top();
			que.pop();
			if(p.second.first>c)continue;
			if(cost[p.second.first][p.second.second]<=p.first)continue;
			cost[p.second.first][p.second.second]=p.first;
			rep(i,n){
				if(edge[p.second.second][i]==INF)continue;
				pair<int,pint> next=p;
				next.first+=edge[p.second.second][i]/2;
				++next.second.first;
				next.second.second=i;
				que.push(next);
				next.first+=edge[p.second.second][i]/2;
				--next.second.first;
				que.push(next);
			}
		}
		int res=INF;
		repn(i,0,c){
			chmin(res,cost[i][d]);
		}
		cout<<res<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>

#define MAX 1000000000
#define Debug(X) debug( __LINE__, X )
template<class T>
void debug( int line, T dbg ){
	std::cout << line << ": " << dbg << std::endl;
}

class State{
public:
	int p, s, c;
	State():p(0), s(0), c(0){}
	State(int P, int S, int C):p(P), s(S), c(C){}
	bool operator < (const State &s) const{
		return c > s.c;
	}
};

int c, n, m, s, d;
int route[101][101];
std::priority_queue<State> que;
bool used[101];


int main(){
	while( std::cin >> c >> n >> m >> s >> d, c * n * m * s * d ){
		for( int i = 0; i < 101 * 101; ++i )
			route[i / 101][i % 101] = MAX;
		
		memset( used, 0, sizeof( used ) );
		que = std::priority_queue<State>();

		int tmp[3];
		while( m-- ){
			int cnt = 3;
			while( cnt-- ) std::cin >> tmp[2 - cnt];

			route[tmp[0]][tmp[1]] = tmp[2];
			route[tmp[1]][tmp[0]] = tmp[2];
		}

		que.push(State( c, s, 0 ));

		while( true ){
			State data = que.top(); que.pop();

			if( used[data.s] )continue;
			used[data.s] = 1;

			if( data.s == d ){
				std::cout << data.c << std::endl;
				break;
			}

			for( int i = 1; i <= n; ++i ){
				if( i == data.s || route[data.s][i] == MAX )continue;

				if( data.p > 0 )
					que.push( State( data.p - 1, i, data.c + route[data.s][i] / 2 ) );

				que.push( State( data.p, i, data.c + route[data.s][i] ) );				
			}

		}

	}
}