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;
typedef long long ll;
typedef pair<int, int> P;

const int INF = 1 << 25;
int dist[100][11];

struct State {
	int v, c, d;
	bool operator <(const State& x) const {
		return d > x.d;
	}
};

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

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

		vector<P> G[100];
		for(int i = 0; i < m; i++) {
			int a, b, f;
			cin >> a >> b >> f;
			a--, b--;
			G[a].push_back({ b, f });
			G[b].push_back({ a, f });
		}

		fill((int*)begin(dist), (int*)end(dist), INF);
		dist[s][c] = 0;
		priority_queue<State> q;
		q.push({ s, c, 0 });
		while(q.size()) {
			int v = q.top().v, rem = q.top().c, d = q.top().d;
			q.pop();
			if(dist[v][c] < d) continue;
			for(auto e : G[v]) {
				int cost[2] = { e.second, e.second / 2 };
				for(int i = 0; i < 2; i++) {
					int u = e.first;
					if(rem == 0 && i == 1) continue;
					if(dist[u][rem - i] > d + cost[i]) {
						dist[u][rem - i] = d + cost[i];
						q.push({ u, rem - i, d + cost[i] });
					}
				}
			}
		}

		int ans = 1e9;
		for(int i = 0; i <= c; i++) {
			ans = min(ans, dist[g][i]);
		}

		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;

int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};

vector<int> dijk(int s, int v, vector<vector<pair<int, int>>> adjlist){
    priority_queue <pair<int, int>> wait;
    vector<int> result(v, INF);

    result[s] = 0;
    wait.push(make_pair(0,s));

    while(!wait.empty()){
        int nowpoint = wait.top().second;
        int nowcost = -wait.top().first;
        wait.pop();

        if(result[nowpoint] < nowcost) continue;

        for(int i = 0; i < adjlist[nowpoint].size(); i++){
            int nextpoint = adjlist[nowpoint][i].second;
            int nextcost = nowcost + adjlist[nowpoint][i].first;
            if(result[nextpoint] > nextcost){
                result[nextpoint] = nextcost;
                wait.push(make_pair(-nextcost, nextpoint));
            }
        }
    }
    return result;
}



signed main(){
    

    while(1){
    int c, n, m, s, t; cin >> c >> n >> m >> s >> t;
    s--;
    t--;
    if(!n) break;
    vector<vector<int> > toNode(n, vector<int> (c + 1));
    int num = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j <= c; j++){
            toNode[i][j] = num;
            num++;
        }
    }

    vector<vector<pair<int, int>>> adj(num);

    for(int i = 0; i < m; i++){
        int a, b, f; cin >> a >> b >> f;
        a--;
        b--;

        for(int iii = 0; iii < 2; iii++){

            for(int j = 0; j <= c; j++){
                    
                int cur = toNode[a][j];
                // 使わない
                int nxt = toNode[b][j];
                adj[cur].push_back({f, nxt});
                
                nxt = toNode[b][j + 1];
                // 使う
                if(j != c){
                    adj[cur].push_back({f / 2, nxt});
                }
            }
            swap(a, b);
        }
    }

    
    int ans = INF;
    int S = toNode[s][0];
    vector<int> result = dijk(S, num, adj);
    for(int j = 0; j <= c; j++){
        int G = toNode[t][j];
        ans = min(ans, result[G]);
    }

    cout << ans << endl;
    }
    return 0;
}

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

#define INF 1 << 29

using namespace std;

int c,n,m,s,d;
int cost[101][101];

class S{
public:
    int node;
    int tick;
    int cost;

    S(int node,int tick,int cost):
        node(node),tick(tick),cost(cost){}
    S(){}
    bool operator <(const S arg) const{
        return cost > arg.cost;
    }
};

int dijkstra(int src,int dst){
    priority_queue<S> q;
    int memo[n+1][c+1];
    S tmp;
    fill((int *)memo, (int *)memo+(n+1)*(c+1), INF);
    memo[src][c] = 0;
    q.push(S(src,c,0));

    while(!q.empty()){
        tmp = q.top();
        q.pop();
        for(int i=0;i<=n;i++){
            if(cost[tmp.node][i] != INF){
                if(cost[tmp.node][i] + tmp.cost < memo[i][tmp.tick]){
                    q.push(S(i,tmp.tick,cost[tmp.node][i] + tmp.cost));
                    memo[i][tmp.tick] = cost[tmp.node][i] + tmp.cost;
                }
                if(tmp.tick && cost[tmp.node][i]/2 + tmp.cost < memo[i][tmp.tick-1]){
                    q.push(S(i,tmp.tick-1,cost[tmp.node][i]/2 + tmp.cost));
                    memo[i][tmp.tick-1] = cost[tmp.node][i]/2 + tmp.cost;
                }
            }
        }
    }
    int ret = 1<<29;
    for(int i=0;i<=c;i++){
        ret = min(ret, memo[dst][i]);
    }
    return ret;
}

int main(){
    while(cin >> c >> n >> m >> s >> d){
        if((c|n|m|s|d) == 0) break;
        int src,dst,c;
        fill((int *)cost, (int *)cost+101*101, INF);
        for(int i=0;i<m;i++){
            cin >> src >> dst >> c;
            cost[src][dst] = c;
            cost[dst][src] = c;
        }

        cout << dijkstra(s,d) << endl;
    }
}