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<cstring>
#include<queue>
#include<algorithm>

using namespace std;

int c,n,m,s,d;
vector<pair<int,int> >  v[101];
int dp[101][11];

int inf = 1111111111;

typedef pair<pair<int,int>,int> p;

void dijkstra(int s,int ticket){
  priority_queue<p,vector<p>,greater<p> > que;
  
  fill((int *)dp,(int *)dp+101*11,inf);
  dp[s][0] = 0;
  que.push(make_pair(make_pair(0,s),0));

  while(!que.empty()){
    p x = que.top();
    que.pop();
    int V = (x.first).second;
    if(dp[V][x.second] < (x.first).first)
      continue;
    for(int i = 0;i<v[V].size();i++){
      pair<int,int> tmp = v[V][i];
      if(dp[tmp.first][x.second] > dp[V][x.second] + tmp.second){
	dp[tmp.first][x.second] = dp[V][x.second] + tmp.second;
	que.push(make_pair(make_pair(dp[tmp.first][x.second],tmp.first),x.second));
      }

      if(x.second < ticket && dp[tmp.first][x.second+1] > dp[V][x.second] + tmp.second/2) {
	//cout<<"a"<<endl;
	dp[tmp.first][x.second+1] = dp[V][x.second] + tmp.second/2;
	que.push(make_pair(make_pair(dp[tmp.first][x.second+1],tmp.first),x.second+1));
      }		

    }

  }
}


int main(){

  while(cin>>c>>n>>m>>s>>d){
    if(c==0&&n==0&&m==0&&s==0&&d==0)
      break;
  
    for(int i = 0;i<101;i++){
      v[i].clear();
    }
    s--;
    d--;
    for(int i = 0;i<m;i++){
      int a,b,f;
      cin>>a>>b>>f;
      a--;b--;
      v[a].push_back(make_pair(b,f));
      v[b].push_back(make_pair(a,f));
    }
    dijkstra(s,c);
    int ans = inf;
    for(int i = 0;i<=c;i++){
      ans = min(ans,dp[d][i]);
    }
    
    /*
    for(int i = 0;i<=c;i++){
      for(int j = 0;j< n;j++){
	cout<<dp[j][i]<<" ";
      }
      cout<<endl;
    }
    */
    cout<<ans<<endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;

const int INF = 1e7;
int Cost[101][101], DpMoney[11][101];
bool Used[11][101];


void solve(int ticket, int n, int start) {
	int u, v;

	for (int i = 0; i < 11; ++i) {
		fill(DpMoney[i], DpMoney[i] + 101, INF);
		fill(Used[i], Used[i] + 101, false);
	}

	DpMoney[0][start] = 0;
	while (true) {
		u = -1;
		for (int i = 0; i <= ticket; ++i) {
			for (int j = 1; j <= n; ++j) {
				if (!Used[i][j] && (u == -1 || DpMoney[i][j] < DpMoney[u][v])) {
					u = i;
					v = j;
				}
			}
		}
		if (u == -1) {
			break;
		}
		Used[u][v] = true;

		for (int k = 1; k <= n; ++k) {
			DpMoney[u][k] = min(DpMoney[u][k], DpMoney[u][v] + Cost[v][k]);
			if (u + 1 <= ticket) {
				DpMoney[u + 1][k] = min(DpMoney[u + 1][k], DpMoney[u][v] + Cost[v][k] / 2);
			}
		}
	}
	
}

int main() {
	int c, n, m, s, d, a, b, f;

	while (cin >> c >> n >> m >> s >> d, c) {
		for (int i = 0; i < 101; ++i) {
			fill(Cost[i], Cost[i] + 101, INF);
		}
		for (int i = 0; i < m; ++i) {
			cin >> a >> b >> f;
			Cost[a][b] = f;
			Cost[b][a] = f;
		}
		solve(c, n, s);
		f = INF;
		for (int i = 0; i <= c; ++i) {
			f = min(f, DpMoney[i][d]);
		}
		cout << f << endl;
	}

	return 0;
}

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

#define INF 1010000000
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)
#define sint(i) scanf("%d",&i);
#define sintt(i,j) scanf("%d%d",&i,&j);
#define sinttt(i,j,k) scanf("%d%d%d",&i,&j,&k);

using namespace std;

class S{
	public:
	int p;
	int v;
	int n;
	
	S(int p,int v,int n):p(p),v(v),n(n){}
	
	bool operator<(const S&a)const{
		return v>a.v;
		return v==a.v ? v>a.v : n <a.n;
	}
};

int main(){
	while(1){
		int c,n,m,s,g;
		scanf("%d%d%d%d%d",&c,&n,&m,&s,&g);
		if(c==0)break;
		
		int root[101][101];
		rep(i,101)rep(j,101)root[i][j]=INF;
		
		rep(i,m){
			int a,b,d;
			sinttt(a,b,d);
			root[a][b]=d;
			root[b][a]=d;
		}
		
		priority_queue<S> qui;
		qui.push(S(s,0,0));
		int ans=0;
		
		int visit[101][11];
		rep(i,101)rep(j,11)visit[i][j]=0;
		
		while(1){
			if(qui.empty()==1){
				ans=-1;
				break;
			}
			
			S u=qui.top();qui.pop();
			
			if(u.p==g){
				ans=u.v;
				break;
			}
			
			printf("%d %d %d\n",u.p,u.v,u.n);
			visit[u.p][u.n]=1;
			
			reps(i,n){
				if(root[u.p][i]!=INF){
					if(visit[i][u.n]==0){
						qui.push(S(i,u.v+root[u.p][i],u.n));
					}
					if(visit[i][u.n+1]==0 && visit[u.p][u.n+1]==0 && u.n+1 <= c){
						qui.push(S(i,u.v+root[u.p][i]/2,u.n+1));
					}
				}
			}
		}
		printf("%d\n",ans);
	}
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int c,n,m,s,d;
int rosen[101][101] = {-1};
int dp[101];
int saiki(int p,vector<int> st,vector<bool> &u){
    //if(dp[p]>0) return dp[p];
    int ans = INF;
    if(p == d){
	sort(RALL(st));
	int tc = min(c,(int)(st.size()));
	REP(i,tc){
	    st[i] /= 2;
	}
	int sum = 0;
	REP(i,st.size()){
	    sum += st[i];
	}
	return sum;
    }
    FOR(i,1,n+1){
	if(!u[i] && rosen[p][i] > 0){
	    vector<int> temp = st;
	    //vector<bool> utemp = u;
	    temp.PB(rosen[p][i]);
	    //utemp[i] = true;
	    u[i] = true;
	    //ans = min(ans,saiki(i,temp,utemp));
	    ans = min(ans,saiki(i,temp,u));
	    u[i] = false;
	}
    }
    return dp[p] = ans;
}

int main(){
    while(true){
	scanf("%d%d%d%d%d",&c,&n,&m,&s,&d);
	if(c==0||n==0||m==0||s==0||d==0) break;
	memset(rosen,-1,sizeof(rosen));
	memset(dp,-1,sizeof(dp));
	REP(i,m){
	    int a,b,f;
	    cin >> a >> b >> f;
	    rosen[a][b] = f;
	    rosen[b][a] = f;
	}
	vector<bool>  used(n+1,false);
	vector<int> nanika;
	used[s] = true;
	cout << saiki(s, nanika ,used) << endl;
    }
    return 0;
}