Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

var pass [][]int

func main() {
	for scanner.Scan() {
		pass = make([][]int, 200)
		for i := range pass {
			pass[i] = make([]int, 200)
		}
		buf := strings.Split(scanner.Text(), " ")
		c, _ := strconv.Atoi(buf[0])
		n, _ := strconv.Atoi(buf[1])
		m, _ := strconv.Atoi(buf[2])
		s, _ := strconv.Atoi(buf[3])
		d, _ := strconv.Atoi(buf[4])
		if c == 0 && n == 0 && m == 0 && s == 0 && d == 0 {
			break
		}
		s--
		d--
		for i := 0; i < 200; i++ {
			for j := 0; j < 200; j++ {
				pass[i][j] = INF
			}
		}
		for i := 0; i < 200; i++ {
			pass[i][i] = 0
		}
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			a, _ := strconv.Atoi(buf_[0])
			b, _ := strconv.Atoi(buf_[1])
			f, _ := strconv.Atoi(buf_[2])
			a--
			b--
			pass[a][b] = f
			pass[b][a] = f
		}
		fmt.Println(dijkstra(s, d, c, n))
	}
}

func dijkstra(s, g, c, n int) float64 {
	d := make([][]float64, 200)
	used := make([][]bool, 200)
	for i := 0; i < 200; i++ {
		d[i] = make([]float64, 20)
		used[i] = make([]bool, 20)
	}
	for i := range d {
		for j := range d[i] {
			d[i][j] = INF
		}
	}
	d[s][c] = 0
	for {
		v := -1
		w := -1
		for u := 0; u < n; u++ {
			for i := 0; i < c+1; i++ {
				if !used[u][i] && (v == -1 || d[u][i] < d[v][w]) {
					v = u
					w = i
				}
			}
		}
		if v == -1 {
			break
		}
		used[v][w] = true
		for u := 0; u < n; u++ {
			d[u][w] = math.Min(d[u][w], d[v][w]+float64(pass[v][u]))
			if w-1 >= 0 && pass[v][u] != INF {
				d[u][w-1] = math.Min(d[u][w-1], d[v][w]+(float64(pass[v][u]/2.0)))
			}
		}
	}
	ret := float64(INF)
	for i := 0; i < c+1; i++ {
		ret = math.Min(ret, d[g][i])
	}
	return ret
}

C++: #include<bits/stdc++.h>
using namespace std;
struct NODE{
  int to,cost,tick;
  NODE(int to,int cost,int tick) : to(to),cost(cost),tick(tick){}
  NODE(){}
  bool operator < (const NODE &a)const{
    return cost > a.cost;
  }
};
int main(){
  int c,n,m,s,d,a,b,f;
  while(cin>>c>>n>>m>>s>>d&&c&&n&&m&&s&&d){
    vector<NODE> edge[101];
    
    for(int i=0;i<m;i++){
      cin >> a >> b >> f;
      edge[a].push_back(NODE(b,f,-1));
      edge[b].push_back(NODE(a,f,-1));
    }
    bool done[101][20] = {};
    priority_queue<NODE> Q;
    Q.push(NODE(s,0,c));
    while(!Q.empty()){
      NODE q = Q.top();Q.pop();
      if(done[q.to][q.tick] == true)continue;
      done[q.to][q.tick] = true;
      if(q.to == d){
	cout << q.cost << endl;
	break;
      }
      for(int i=0;i<edge[q.to].size();i++){
	Q.push(NODE(edge[q.to][i].to,q.cost + edge[q.to][i].cost,q.tick));
	if(q.tick){
	  Q.push(NODE(edge[q.to][i].to,q.cost + (edge[q.to][i].cost/2),q.tick-1));
	}
      }
    }
  }
}

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

const int MAX_V = 101;
const int INF = 1e+8;

// 頂点は(ノード番号,割引券の数)の組
typedef pair<int,int> P;
// 状態は(コスト,頂点)の組
typedef pair<int,P> State;

struct edge{
	int to, cost;
	edge(int v, int cost_){
		to = v;
		cost = cost_;
	}
};

// 頂点数
int V;
// G[u][i] = ノード u に接続している i 番目の edge が返ってくる
vector<edge> G[MAX_V];

// 初期化
void init(int n){
	V = n;
	for(int i = 0 ; i < MAX_V ; i++ ){
		G[i].clear();
	}
}


// ノード番号 s を始点, 割引券の枚数を c としたダイクストラ法
int dijkstra(int s, int c, int g){
	// greater<State> を指定することで コスト が小さい順に取り出せる
	priority_queue<State, vector<State>, greater<State> > q;
	
	// d[c][v] := 割引券 c 枚でノード番号 v にたどり着く最小コスト
	int d[11][101];
	for(int i=0 ; i < 11 ; i++ ){
		for(int j=0 ; j < 101 ; j++ ){
			d[i][j] = INF;
		}
	}
	
	d[c][s] = 0;
	State start( 0 , P(s,c) );
	q.push( start );
	
	int result = INF;
	while( !q.empty() ){
		int now_cost = q.top().first;
		int v = q.top().second.first;
		int c =  q.top().second.second;
		q.pop();
		
		if( v == g ){
			result = now_cost;
			break;
		}
		
		if( d[c][v] < now_cost ) continue;
		
		for(int i=0 ; i < G[v].size() ; i++ ){
			edge e = G[v][i];
			
			int next_cost = d[c][v] + e.cost;
			if( next_cost < d[c][e.to] ){
				d[c][e.to] = next_cost;
				State next( next_cost , P(e.to,c) );
				q.push( next );
			}
			if( c > 0 ){
				next_cost = d[c][v] + e.cost/2 ;
				if( next_cost < d[c-1][e.to] ){
					d[c-1][e.to] = next_cost;
					State next( next_cost , P(e.to,c-1) );
					q.push( next );
				}
			}
		}
	}
	return result;
}

// 重み cost の辺 (u,v) の追加
void add_edge(int u, int v, int cost){
	edge e( v , cost );
	G[u].push_back( e );
	e.to = u;
	G[v].push_back( e );
}


int main(){
	int c,n,m,s,g;
	while( cin >> c >> n >> m >> s >> g, c || n || m || s || g ){
		init(n);
		s--; g--;
		for(int i=0 ; i < m ; i++ ){
			int u, v, cost;
			cin >> u >> v >> cost;
			u--; v--;
			add_edge(u,v,cost);
		}
		int ans = dijkstra( s , c , g );
		cout << ans << endl;
	}
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

using namespace std;

int minimum,num_ticket,num_town,m,start,goal,costTable[101][101];


struct Info{
    Info(){
        current_town = 0;
        index = 0;
    }
    bool visited[102];
    int current_town,index,costs[102],visited_town[101];

};

void func(Info info){

    int sum = 0,limit;

    if(info.current_town == goal || minimum != 2000000000)
    {

    	limit = min(num_ticket,info.index);

    	for(int i = 0; i < limit; i++){
    		sum += info.costs[i]/2;
    	}

    	for(int i = limit; i < info.index; i++){
    		sum += info.costs[i];
    	}

		if(info.current_town == goal){
			minimum = min(minimum,sum);
			return;
		}else{
			if(sum >= minimum){
				return;
			}
		}

    }

    int calc;
    for(int i = 1; i <= num_town; i++){
        if(costTable[info.current_town][i] != -1 && info.visited[i] == false){
            Info new_info;
            for(int k = 1; k <= num_town; k++)new_info.visited[k] = info.visited[k];
            new_info.visited[i] = true;
            new_info.current_town = i;

            if(costTable[info.current_town][i] > info.costs[0]){
            	new_info.costs[0] = costTable[info.current_town][i];
            	for(int k = 1; k <= info.index; k++){
            		new_info.costs[k] = info.costs[k-1];
            	}
            }else if(costTable[info.current_town][i] <= info.costs[info.index-1]){
            	for(int k = 0; k < info.index; k++){
            		new_info.costs[k] = info.costs[k];
            	}
            	new_info.costs[info.index] = costTable[info.current_town][i];
            }else{
            	for(calc = 0; info.costs[calc] >= costTable[info.current_town][i];calc++)new_info.costs[calc] = info.costs[calc];
				new_info.costs[calc] = costTable[info.current_town][i];
				calc++;
				for(;calc-1 < info.index;calc++)new_info.costs[calc] = info.costs[calc-1];
            }
            new_info.index = info.index+1;
            func(new_info);

        }
    }
}

int main(){

    int from,to,fee;

    while(true){
        scanf("%d %d %d %d %d",&num_ticket,&num_town,&m,&start,&goal);
        if(num_ticket == 0 && num_town == 0 && m == 0 && start == 0 && goal == 0)break;

        minimum = 2000000000;

        for(int i = 1; i <= num_town; i++){
            for(int k = 1; k <= num_town; k++)costTable[i][k] = -1;
        }

        for(int i = 0; i < m; i++){
            scanf("%d %d %d",&from,&to,&fee);
            costTable[from][to] = fee;
            costTable[to][from] = fee;
        }

        Info info;
        for(int i = 1; i <= num_town;i++)info.visited[i] = false;

        info.current_town = start;
        info.visited[start] = true;

        func(info);

        printf("%d\n",minimum);
    }


    return 0;
}