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 <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
#define INF 100000000
#define MAXN 110
#define MAXM 512
#define MAXC 11
typedef long long ll;

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

edge es[2*MAXM];

int c, n, m, s, d;
int dist[MAXN][MAXN];
int xdist[MAXC][MAXN];

int main(void) {
    while (1) {
        cin >> c >> n >> m >> s >> d;
        if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                dist[i][j] = INF;
            }
            dist[i][i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a, b, f;
            cin >> a >> b >> f;
            dist[a][b] = dist[b][a] = f;
            es[2*i].from = a; es[2*i].to = b; es[2*i].cost = f;
            es[2*i+1].from = b; es[2*i+1].to = a; es[2*i+1].cost = f;
        }
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            xdist[0][i] = dist[i][d];
        }
        for (int i = 1; i <= c; i++) {
            for (int j = 1; j <= n; j++) {
                xdist[i][j] = xdist[i-1][j];
                for (int k = 0; k < 2*m; k++) {
                    edge e = es[k];
                    xdist[i][j] = min(xdist[i][j], dist[j][e.from] + e.cost / 2 + xdist[i-1][e.to]);
                }
            }
        }
        cout << xdist[c][s] << endl;
    }
    return 0;
}

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

struct town {
	int n,tn[500],m[500],mn,mm;
};

town to[100];

int mm,c,n,m,s,d,p[100],p2[100];

void ans(int k,int h,int g) {
	int i,a=0;
	if (g>=mm*2) return;
	if (h==d) { 
		for (i=0;i<k;i++) p2[i]=p[i];
		sort(p2,p2+k,greater<int>());
		for (i=0;i<k;i++) if (i<c) a+=p2[i]/2; else a+=p2[i];
		if (a<mm) mm=a;
		return;
	 }
	for (i=0;i<to[h].n;i++) {
		p[k]=to[h].m[i];
		if (to[to[h].tn[i]].mn==1) continue;
		to[to[h].tn[i]].mn=1;
		ans(k+1,to[h].tn[i],g+p[k]);
		to[to[h].tn[i]].mn=0;
	}   
   }

int main() {
	int i,a,b,x;
	while(cin >> c >> n >> m >> s >> d && n>0) {
		s--; d--;
		for (i=0;i<n;i++) { to[i].n=0; to[i].mn=0;}
		for (i=0;i<m;i++) {
			cin >> a >> b >> x; a--; b--;
			to[a].tn[to[a].n]=b;  to[b].tn[to[b].n]=a;
			to[a].m[to[a].n]=to[b].m[to[b].n]=x;
			to[a].n++; to[b].n++;
		}
		mm=99999999;
		to[s].mn=1;
		ans(0,s,0);
		cout << mm << endl;
    }		 
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;// F -> to, S -> cost
typedef pair<P,int> PP;// F->P(to,cost), S->discount
int c,n,m,s,d;
vector<PP> vec[1000];
int MD[1000][13];
const int INF = 1<<29;

void dijkstra(){
  priority_queue<PP,vector<PP>,greater<PP> > que;
  rep(i,n+1)rep(j,13)MD[i][j] = INF;
  //syokika
  MD[s][c] = 0;
  que.push(PP(P(0,s),c));//dis,start,discount

  while(!que.empty() ){
    PP p = que.top();que.pop(); //priority_queのPPと
    int v = p.F.S;
    
    for(int i=0;i<vec[v].size();i++){
      PP po = vec[v][i]; //vectorのPPは意味が違うので注意！！！
      //if(MD[v][po.S] < po.F.S)continue;
      if(MD[po.F.F][c] > p.F.F + po.F.S){
	MD[po.F.F][c] = p.F.F+po.F.S;
	que.push(PP(P(MD[po.F.F][c],po.F.F),c));
      }
      if(po.S > 0 && MD[po.F.F][p.S-1] > p.F.F + po.F.S/2 ){
	MD[po.F.F][p.S-1] = p.F.F+po.F.S/2;
	que.push(PP(P(MD[po.F.F][p.S-1],po.F.F),p.S-1));
	vec[v][i].S--;
      }
    }
  }
}


int main(){
  int x,y,co;
  while(true){
    cin >> c >> n >> m >> s >> d;
    if(c+n+m+s+d == 0)break;
    rep(i,1000)vec[i].clear();
    rep(i,m){
      cin >> x >> y >> co;
      vec[x].push_back(PP(P(y,co),c));
      vec[y].push_back(PP(P(x,co),c));
    }
    dijkstra();
    int ans = INF;
    for(int i=0;i<=c;i++){
      ans = min(ans,MD[d][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

class Node{
public:
	int num;
	int c;
	int fee;

	Node(){ num = 0; c = 0; fee = 0;}
	Node(int num, int c, int fee) : num(num), c(c), fee(fee){}

};

bool operator< (const Node &n1, const Node &n2){
	return n1.fee < n2.fee;
}

bool operator>(const Node &n1, const Node &n2){
	return n1.fee > n2.fee;
}

int main(void){

	int c, n, m, s, d;
	while (cin >> c >> n >> m >> s >> d, c, n, m, s, d){
		vector<vector <int> > path(n + 1);
		vector<vector <int> > cost(n + 1);
		bool checked[101][11] = { false };

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

		priority_queue<Node, vector<Node>, greater<vector<Node>::value_type> > que;

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

		while (!que.empty()){
			Node buff = que.top();
			que.pop();
			if (checked[buff.num][buff.c]) continue;
			checked[buff.num][buff.c] = true;
			if (buff.num == d){
				cout << buff.fee << endl;
				break;
			}

			int nnum = buff.num;
			for (int i = 0; i < path[nnum].size(); i++){
				que.push(Node(path[nnum][i], buff.c, buff.fee + cost[nnum][i]));
				if (buff.c > 0) que.push(Node(path[nnum][i], buff.c - 1, buff.fee + cost[nnum][i] / 2));
			}
		}
	}
	return 0;
}