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 <vector>

using namespace std;

#define INF 100000000

struct edge {
	int to, cost;
};

int main() {
	int c, n, m, s, d;
	while (cin >> c >> n >> m >> s >> d) {
		if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) {
			break;
		}
		vector<edge> ff[101];
		vector< vector<int> > ddd(n+1, vector<int>(n+1, INF));
		int a, b, f;
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> f;
			edge e;
			e.to = b;
			e.cost = f;
			ff[a].push_back(e);
			e.to = a;
			ff[b].push_back(e);
			ddd[a][b] = ddd[b][a] = f;
		}
		for (int i = 1; i <= n; i++) {
			ddd[i][i] = 0;
		}
		for (int k = 1; k <= n; k++) {
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= n; j++) {
					ddd[i][j] = min(ddd[i][j], ddd[i][k]+ddd[k][j]);
				}
			}
		}
		vector<int> isd[101];
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				if (ddd[i][j] != INF) {
					isd[i].push_back(j);
				}
			}
		}
		vector< vector< vector<int> > > dp(n+1, vector< vector<int> >(n+1, vector<int>(c+1, INF)));
		for (int i = 1; i <= n; i++) {
			dp[s][i][0] = ddd[s][i];
		}
		for (int k = 1; k <= c; k++) {
			for (int p = 1; p <= n; p++) {
				dp[s][p][k] = dp[s][p][k-1];
				for (int i = 0; i < isd[s].size(); i++) {
					int ii = isd[s][i];
					for (int j = 0; j < isd[ii].size(); j++) {
						int jj = isd[ii][j];
						for (int q = 0; q < ff[jj].size(); q++) {
							int qq = ff[jj][q].to;
							if (ddd[qq][p] == INF) continue;
							if (dp[s][p][k] > dp[s][ii][k-1]+ddd[ii][jj]+(ff[jj][q].cost/2)+ddd[qq][p]) {
								dp[s][p][k] = dp[s][ii][k-1]+ddd[ii][jj]+(ff[jj][q].cost/2)+ddd[qq][p];
							}
						}
					}
				}
			}
		}
		cout << dp[s][d][c] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
typedef long long ll;
using namespace std;

int mindis[101], pre[101];
bool used[101];
int eCost[101][101];

int getPathSum(int c, int p, int g){
	vector<int> edge;
	edge.push_back(eCost[p][g]);
		
	int to = p;
	for(int from = pre[to]; from != 0; from = pre[from]){
		edge.push_back(eCost[from][to]);
		to = from;
	}

	sort(edge.begin(), edge.end());

	int idx = edge.size()-1;
	for(int i = 0; i < c; i++){
		edge[idx--] /= 2;
	}
	
	int ans = 0;
	for(int i = 0; i < edge.size(); i++){
		ans += edge[i];
	}

	return ans;
}

int main(){
	int c, n, m, s, d;
	while(cin >> c >> n >> m >> s >> d){
		if(c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break;

		fill(mindis, mindis+101, 1<<30);
		fill(pre, pre+101, 0);
		fill(used, used+101, false);
		for(int i = 0; i < 101; i++) fill(eCost[i], eCost[i]+101, 1<<30);

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

		mindis[s] = 0;
		vector<int> goals;

		//最短経路(ダイクストラ)
		while(true){
			int v = -1;
			for(int i = 1; i <= n; i++){
				if(!used[i] && (v == -1 || mindis[v] > mindis[i])) v = i;
			}

			if(v == d) break;
			used[v] = true;

			for(int i = 1; i <= n; i++){
				if(mindis[i] > mindis[v]+eCost[v][i]){
					if(i == d){
						//次がゴールならここでチケットを適用した総和を出して最終的な結果にする
						//それをmindisに格納しておき、今後ゴールにたどり着いたルート全てと比較
						goals.push_back(getPathSum(c, v, i));
					}else{
						mindis[i] = mindis[v]+eCost[v][i];
						pre[i] = v;
					}
				}
			}
		}

		int minc = 10000000;
		for(int i = 0; i < goals.size(); i++){
			if(minc > goals[i]) minc = goals[i];
		}

		cout << minc << endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define IREP(it,array) for(vector<int>::iterator it=array.begin(); it!=array.end(); ++it)
#define SREP(it,array) for(vector<string>::iterator it=array.begin(); it!=array.end(); ++it)

#define MP       make_pair
#define PB       push_back
#define ALL(x)   (x).begin(),(x).end()

const int INF = 1<<29;
const double EPS = 1e-9;
double zero(double d){
  return d < EPS ? 0.0 : d;
}

typedef long long LL;
typedef pair<int,int> P;


int main()
{
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  int c,n,m,s,d;
  while( cin >> c >> n >> m >> s >> d , c ){

    int dis[11][101];
    bool used[11][101];
    int cost[101][101];

    REP(i,11){
      fill( dis[i], dis[i]+101, INF );
      fill( used[i], used[i]+101, false );
    }
    dis[0][s] = 0;

    REP(i,101){
      fill( cost[i], cost[i]+101, INF );
      //      cost[i][i] = 0;
    }

    REP(_t,m){
      int a,b,f;
      cin >> a >> b >> f;
      cost[a][b] = cost[b][a] = f;
    }


    while(true){

      // pick minimum distance in unused
      int mini=-1, minj=0;
      REP(i,c+1){
	REP(j,n+1){
	  if( ! used[i][j] ){
	    if( mini==-1 || dis[mini][minj] > dis[i][j] ){
	      mini = i;
	      minj = j;
	    }
	  }
	}
      }

      if( mini == -1)	break;
      used[mini][minj] = true;

      REP(i,m+1){
	dis[mini][i] = min( dis[mini][i], dis[mini][minj] + cost[minj][i] );
	if( mini + 1 <= c )
	  dis[mini+1][i] = min( dis[mini+1][i], dis[mini][minj] + cost[minj][i]/2 );	  
      }
    }

    int res=INF;
    REP(i,c+1)
      res = min( res, dis[i][d] );

    cout << res << endl;
  }

  return 0;
}