Problem:
高速バス
A 君は高校の休みを利用して、高速バス(以下、「バス」 )で一人旅をする計画を立てています。まず、A 君は一番行ってみたい町を選んでそこを目的地にしました。次に出発地から目的地までバスを乗り継いでいくルートを決めなければなりません。乗り継ぎをするときは、バスを降りてから別のバスに乗り換えるので、それぞれのバスの乗車券が必要になります。
A 君は親戚のおじさんからバスの割引券を何枚かもらいました。   この券を 1 枚使うと乗車券 1 枚を半額で購入できます。例えば、図 1 の出発地5から目的地1へ行く場合には、5→4→6→2→1と5→3→1の二つの経路が考えられます。割引券が 2 枚あるとすると、交通費を最も安くするには5→4→6→2→1の経路をたどった場合、4→6と6→2の路線に割引を利用し、合計料金は 4600円となります。一方、5→3→1の経路をたどった場合、5→3と3→1の路線に割引を利用し、合計料金は 3750 円となります。
A 君は観光にお金を回したいので、交通費はできるだけ少なくしようと考えています。そこで A 君は、出発地から目的地までの最も安い交通費を求めるプログラムを作成することにしました。
図1
割引券の枚数、バスがつなぐ町の数、バスの路線数、各バスの路線情報を入力とし、出発地から目的地までの最も安い交通費を出力するプログラムを作成してください。各バスは双方向に同一料金で運行しています。また、町の数を
n
とすると、町にはそれぞれ異なる 1 から
n
までの番号が振られています。出発地から目的地までの経路は必ず存在するものとします。
Input
複数のデータセットの並びが入力として与えられます。         入力の終わりはゼロが５つの行で示されます。
各データセットは以下の形式で与えられます。
c
n
m
s
d
a
1
b
1
f
1
a
2
b
2
f
2
:
a
m
b
m
f
m
1 行目に割引券の枚数
c
(1 ≤
c
≤ 10)、バスがつなぐ町の数
n
(2 ≤
n
≤ 100)、バスの路線数
m
(1 ≤
m
≤ 500)、出発地の町番号
s
と目的地の町番号
d
(
s ≠ d
)  が与えられます。
続く
m
行に第
i
のバスの路線情報
a
i
, b
i
, f
i
(1 ≤
a
i
, b
i
≤
n
, 1000 ≤
f
i
≤ 10000) が与えられます。
a
i
, b
i
はバスの路線の始点と終点の町番号、
f
i
はこの路線の料金を表す100 刻みの整数です。
データセットの数は 100 を超えません。
Output
入力データセットごとに、最も安い交通費を１行に出力します。
Sample Input
1 3 3 1 3
1 3 2000
1 2 1000
2 3 1000
2 3 3 1 3
1 3 2300
1 2 1000
2 3 1200
2 6 6 5 1
1 2 1500
1 3 4500
2 6 2000
5 4 1000
6 4 2200
3 5 3000
0 0 0 0 0
Output for the Sample Input
1000
1100
3750

Submitted solutions:

code_snippet: #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;
}
accuracy: 1
time_ms: 40
memory_kb: 3168
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#define INF 1000010;

using namespace std;


int main(void){
  long c,n,m,s,d;
  while(true){
    cin>>c>>n>>m>>s>>d;
    if(c==0 and n==0 and m==0 and s==0 and d==0)break;
    d--;
    s--;

    long a,b,f;
    map<long,long> costs[n];
    for(int i=0;i<m;i++){
      cin>>a>>b>>f;
      a--;
      b--;
      costs[a][b]=f;
      costs[b][a]=f;
    }
    map<long,long> dist[n];
    for(long i=0;i<n;i++){
      for(long j=0;j<=c;j++){
        dist[i][j]=INF;
      }
    }
    dist[s][0]=0;

    vector<long> next,_next;
    next.push_back(s);
    while(next.empty()==false){
      _next.clear();
      for(auto iter=next.begin();iter!=next.end();iter++){
        long city=*iter;
        for(auto iter2=costs[city].begin();iter2!=costs[city].end();iter2++){
          long next_city=iter2->first;
          long fee=iter2->second;
          for(long ticket=0;ticket<=c;ticket++){
            if(dist[next_city][ticket]>dist[city][ticket]+fee){
              dist[next_city][ticket]=dist[city][ticket]+fee;
              _next.push_back(next_city);
            }
          }
          for(long ticket=1;ticket<=c;ticket++){
            if(dist[next_city][ticket]>dist[city][ticket-1]+fee/2){
              dist[next_city][ticket]=dist[city][ticket-1]+fee/2;
              _next.push_back(next_city);
            }
          }
        }
      }

      sort(_next.begin(),_next.end());
      _next.erase(unique(_next.begin(),_next.end()),_next.end());
      next.clear();
      copy(_next.begin(),_next.end(),back_inserter(next));

    }
    long retval=INF;
    for(long ticket=0;ticket<=c;ticket++){
        retval=min(dist[d][ticket],retval);
    }
    cout<<retval<<endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3240
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define INF 1000000

int main() {
    while (true) {
        int c, n, m, s, d;
        cin >> c >> n >> m >> s >> d;
        
        if (c==0 && n==0 && m==0 && s==0 && d==0) {
            break;
        }
        
        s -= 1;
        d -= 1;
        
        vector<P> adj_list[n*(c+1)];
    
        for (int i=0; i<m; i++) {
            int ai, bi, fi;
            cin >> ai >> bi >> fi;
            ai -= 1;
            bi -= 1;
        
            for (int j=0; j<=c; j++) {
                adj_list[ai*(c+1)+j].push_back(P(bi*(c+1)+j, fi));
                adj_list[bi*(c+1)+j].push_back(P(ai*(c+1)+j, fi));
            
                if (j>0) {
                    adj_list[ai*(c+1)+j].push_back(P(bi*(c+1)+j-1, fi/2));
                    adj_list[bi*(c+1)+j].push_back(P(ai*(c+1)+j-1, fi/2));
                }
            }
        }
        
        priority_queue<P, vector<P>, greater<P>> pq;
        int dist[n*(c+1)];
        
        for (int i=0; i<n*(c+1); i++) {
            dist[i] = INF;
        }
        
        dist[s*(c+1)+c] = 0;
        pq.push(P(0, s*(c+1)+c));
        
        while (pq.size()) {
            P p = pq.top();
            pq.pop();
            
            int d = p.first;
            int v = p.second;
            
            if (dist[v]<d) {
                continue;
            }
            
            for (int i=0; i<adj_list[v].size(); i++) {
                int nv = adj_list[v][i].first;
                int w = adj_list[v][i].second;
                
                if (dist[nv]>dist[v]+w) {
                    dist[nv] = dist[v]+w;
                    pq.push(P(dist[nv], nv));
                }
            }
        }
        
        int ans = INF;
        
        for (int i=0; i<=c; i++) {
            ans = min(ans, dist[d*(c+1)+i]);
        }
        
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3240
final_rank: 5

code_snippet: /**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using P = pair<ll, ll>;

constexpr double PI = 3.14159265358979323846;
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());

class HighwayExpressBus {
public:
    void solve(istream& cin, ostream& cout) {
      SPEED;
      while(true) {
        int c, n, m, s, d; cin >> c >> n >> m >> s >> d;
        s--; d--;
        if (c==0) return;

        vector<P> graph[(c+1)*n];
        int cost[(c+1)*n];
        rep(i, (c+1)*n) {
          cost[i] = INT_MAX/2;
        }

        while(m--) {
          int a, b, f; cin >> a >> b >> f;
          a--; b--;
          rep(i, c+1) {
            graph[a+i*n].emplace_back(b+i*n, f);
            graph[b+i*n].emplace_back(a+i*n, f);
          }
          rep(i, c) {
            graph[a+i*n].emplace_back(b+(i+1)*n, f/2);
            graph[b+i*n].emplace_back(a+(i+1)*n, f/2);
          }
        }

        priority_queue<P, vector<P>, greater<>> que;
        que.emplace(0, s);
        cost[s] = 0;
        while(!que.empty()) {
          auto p = que.top(); que.pop();
          if (p.first > cost[p.second]) continue;
          for(const auto& e: graph[p.second]) {
            if (cost[e.first] > p.first + e.second) {
              cost[e.first] = p.first + e.second;
              que.emplace(cost[e.first], e.first);
            }
          }
        }

        int ans = INT_MAX;
        rep(i, c+1) {
          chmin(ans, cost[d+n*i]);
        }
        cout << ans << endl;
      }

    }
};

signed main() {
  HighwayExpressBus solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3320
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <numeric>

using namespace std;
using ll = long long;
using ull = unsigned long long;

#define REP(i,n) for(long long i = 0; i < (n); i++)
#define FOR(i, m, n) for(long long i = (m);i < (n); ++i)
#define ALL(obj) (obj).begin(),(obj).end()
#define SPEED cin.tie(0);ios::sync_with_stdio(false);

template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;

const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ll HIGHINF = (ll)1e18;
const ll LOWINF = (ll)1e15;
const long double PI = 3.1415926535897932384626433;

template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max<T>(a,b);}
template <class T> void chmin(T& a, const T b){a=min<T>(a,b);}
void YN(bool flg) {cout << ((flg) ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << ((flg) ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << ((flg) ? "yes" : "no") << endl;}

template<class T> class Dijkstra {
public:
    int N;
    T inf;
    vector<T> cost;
    vector<vector<pair<T, int>>> edge;
    
    Dijkstra(const int N, T inf) : N(N), inf(inf),cost(N), edge(N) {
    }
    
    void make_edge(int from, int to, T w) {
        edge[from].push_back({ w,to });
    }
    
    void solve(int start) {
        for(int i = 0; i < N; ++i) cost[i] = inf;
        
        priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> pq;
        cost[start] = 0;
        pq.push({ 0,start });
        
        while (!pq.empty()) {
            T v = pq.top().first;
            int from = pq.top().second;
            pq.pop();
            for (auto u : edge[from]) {
                T w = v + u.first;
                int to = u.second;
                if (w < cost[to]) {
                    cost[to] = w;
                    pq.push({ w,to });
                }
            }
        }
        return;
    }
};

int main() {
    while(1){
        ll c,n,m,s,d;
        cin >> c >> n >> m >> s >> d;
        if(c==0){
            return 0;
        }
        s--,d--;
        Dijkstra<ll> dijk((c+1)*n,LOWINF);
        for(int i = 0; i < m; ++i){
            int a,b; cin >> a >> b;
            a--,b--;
            ll w; cin >> w;
            for(int i = 0; i < c; ++i){
                dijk.make_edge(i*n+a, (i+1)*n+b, w/2);
                dijk.make_edge(i*n+b, (i+1)*n+a, w/2);
            }
            for(int i = 0; i <= c; ++i){
                dijk.make_edge(i*n+a, i*n+b, w);
                dijk.make_edge(i*n+b, i*n+a, w);
            }
        }
        dijk.solve(s);
        ll ans = LOWINF;
        for(int i = 0; i <= c; ++i) chmin(ans,dijk.cost[i*n+d]);
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3392
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9+7)
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(a,n) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
void init(){
  cin.tie(0);
  ios::sync_with_stdio(false);
}



template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WG = vector< Edges< T > >;
using UG = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;



template< typename T >
vector<T> dijkstra(WG<T> &g,int s){
  const auto lim = numeric_limits<T>::max();
  vector<T> dist(g.size(),lim);
  using Pi = pair<T,int>;
  priority_queue<Pi,vector<Pi>,greater<Pi>> q;
  dist[s] = 0;
  q.emplace(dist[s],s);
  while(!q.empty()){
    T cost;
    int idx;
    tie(cost,idx) = q.top();
    q.pop();
    if(dist[idx] < cost) continue;
    for( auto &e : g[idx]){
      auto next_cost = cost + e.cost;
      if(dist[e.to] <= next_cost) continue;
      dist[e.to] = next_cost;
      q.emplace(dist[e.to],e.to);
    }
  }
  return dist;
}


int c,n,m,s,d;

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

    s--;d--;
    WG<int> es(n*(c+1));

    rep(k,m){
      int a,b,f;
      cin>>a>>b>>f;
      a--;b--;
      rep(i,c+1){
        es[a*(c+1)+i].PB(edge<int>(b*(c+1)+i,f));
        es[b*(c+1)+i].PB(edge<int>(a*(c+1)+i,f));
      }
      FOR(i,1,c+1){
        es[a*(c+1)+i].PB(edge<int>(b*(c+1)+i-1,f/2));
        es[b*(c+1)+i].PB(edge<int>(a*(c+1)+i-1,f/2));
      }
    }

    vector<int> res=dijkstra(es,s*(c+1)+c);

    int ans=INF;
    rep(i,c+1){
      //cout<<res[d*(c+1)+i]<<endl;
      ans=min(ans,res[d*(c+1)+i]);
    }

    cout<<ans<<endl;

  }
}
accuracy: 1
time_ms: 10
memory_kb: 3396
final_rank: 9

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<queue>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
typedef pair<int,int> pii;
typedef pair<int,pii> pip;

class Dijkstra{
private:
    struct edge {int to,cost;};
    vector<vector<edge> > g;
    int n;

public:
    Dijkstra(int size){
        g.resize(size);
        n = size;
    }
    vector<vector<int> > dist;
    void add(int a, int b, int c){
        edge e1 = {b,c}, e2 = {a,c};
        g[a].push_back(e1);
        g[b].push_back(e2);
    }
    int calc(int s,int t,int ma){
        priority_queue<pip,vector<pip>,greater<pip> > q;
        int n = g.size();
        dist = vector<vector<int> > (n, vector<int>(ma+1,1e9));
        dist[s][0] = 0;
        q.push(pip(0,pii(s,0)));
        while(!q.empty()){
            pip p = q.top();
            q.pop();
            int c = p.first;
            pii node = p.second;
            if(dist[node.first][node.second] < c)continue;
            rep(i,g[node.first].size()){
                edge e = g[node.first][i];
                if(dist[e.to][node.second] > dist[node.first][node.second] + e.cost){
                    dist[e.to][node.second] = dist[node.first][node.second] + e.cost;
                    q.push(pip(dist[e.to][node.second],pii(e.to,node.second)));
                }
                if(node.second < ma){
                    if(dist[e.to][node.second+1] > dist[node.first][node.second] + e.cost/2){
                        dist[e.to][node.second+1] = dist[node.first][node.second] + e.cost/2;
                        q.push(pip(dist[e.to][node.second+1],pii(e.to,node.second+1)));
                    }
                }
            }
        }
        int ret = 1e9;
        rep(i,ma+1)ret = min(ret, dist[t][i]);
        return ret;
    }
};

int main(){
    int c,n,m,s,d;
    while(cin >> c >> n >> m >> s >> d, n){
        Dijkstra dij(n);
        rep(i,m){
            int a,b,f;
            cin >> a >> b >> f;
            dij.add(a-1,b-1,f);
        }
        cout << dij.calc(s-1,d-1,c) << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3184
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct line {
    ll a, b, f;
};
using pllll = pair<ll, ll>;
using pl6 = pair<ll, pair<ll, ll>>;
struct edge {
    ll dstb, dstc, cost;
};
int main() {
    
    ll c, n, m, s, d;
    while (cin >> c >> n >> m >> s >> d, c) {

        s--; d--;

        vector<line> l(m, line{0, 0, 0});
        for (ll i = 0; i < m; i++) {
            cin >> l[i].a >> l[i].b >> l[i].f;
            l[i].a--; l[i].b--;
            if (l[i].a > l[i].b) swap(l[i].a, l[i].b);
        }

        sort(l.begin(), l.end(), [](line &i, line &j) -> bool {
                 if (i.a != j.a) return i.a < j.a;
            else if (i.b != j.b) return i.b < j.b;
            else return i.f < j.f;
        });

        // 被った路線はなかったことに
        for (ll i = m; --i; ) {
            if (l[i].a == l[i-1].a && l[i].b == l[i-1].b) {
                l.erase(l.begin() + i);
            }
        }
        m = l.size();

        
        // 無向グラフを構築
        vector<vector<vector<edge>>> g(n, vector<vector<edge>>(c+1));
        for (ll i = m; i--; ) {
            ll a = l[i].a;
            ll b = l[i].b;
            ll f = l[i].f;
            for (ll c0 = c; c0 >= 0; c0--) {
                g[a][c0].push_back(edge{b, c0, f});
                g[b][c0].push_back(edge{a, c0, f});
                if (c0 > 0) {
                    g[a][c0].push_back(edge{b, c0-1, f/2});
                    g[b][c0].push_back(edge{a, c0-1, f/2});
                }
            }
        }

        // 拡張Dijkstra
        vector<vector<ll>> dp(n, vector<ll>(c+1, 0x7FFFFFFFLL));
        vector<vector<bool>> determined(n, vector<bool>(c+1, false));
        priority_queue<pl6, vector<pl6>, greater<pl6>> q;
        q.push(pl6(0, pllll(s, c)));
        dp[s][c] = 0;
        while (!q.empty()) {
            pl6 cv = q.top(); q.pop();
            pllll v = cv.second;
            ll vf = dp[v.first][v.second];
            if (determined[v.first][v.second]) continue;
            determined[v.first][v.second] = true;
            for (edge &ed : g[v.first][v.second]) {
                if (determined[ed.dstb][ed.dstc]) continue;
                if (vf + ed.cost < dp[ed.dstb][ed.dstc]) {
                    q.push(pl6(vf + ed.cost, pllll(ed.dstb, ed.dstc)));
                    dp[ed.dstb][ed.dstc] = vf + ed.cost;
                }
            }
        }

        ll ans = 0x7FFFFFFFLL;
        for (ll i = 0; i <= c; i++) {
            ans = min(ans, dp[d][i]);
        }
        cout << ans << endl;

    }

}
accuracy: 1
time_ms: 10
memory_kb: 3620
final_rank: 11

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <tuple>
#include <queue>

using namespace std;

#define int long long

class Solver {
};

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

		vector<vector<vector<tuple<int, int, int>>>> graph(c + 1,
			vector<vector<tuple<int, int, int>>>(n));

		for (int i = 0; i < m; i++) {
			int a, b, f;
			cin >> a >> b >> f;
			a--;
			b--;
			for (int i = 0; i <= c; i++) {
				graph[i][a].emplace_back(make_tuple(i, b, f));
				graph[i][b].emplace_back(make_tuple(i, a, f));
				if (i < c) {
					graph[i][a].emplace_back(make_tuple(i + 1, b, f/2));
					graph[i][b].emplace_back(make_tuple(i + 1, a, f/2));
				}
			}
		}

		priority_queue<tuple<int, int, int>> que;
		vector<vector<int>> dist(c + 1, vector<int>(n, 1e9));
		que.emplace(make_tuple(0, s, 0));
		while (que.size() > 0) {
			int cnt = get<0>(que.top());
			int pos = get<1>(que.top());
			int distance = get<2>(que.top());
			que.pop();
			if (dist[cnt][pos] <= distance)continue;

			dist[cnt][pos] = distance;
			for (int i = 0; i < graph[cnt][pos].size(); i++) {
				int cnt_t = get<0>(graph[cnt][pos][i]);
				int pos_t = get<1>(graph[cnt][pos][i]);
				int distance_t = get<2>(graph[cnt][pos][i]);
				distance_t += distance;

				if (dist[cnt_t][pos_t] <= distance_t)continue;

				que.emplace(make_tuple(cnt_t, pos_t, distance_t));
			}
		}

		int res = 1e9;
		for (int i = 0; i <= c; i++)res = min(res, dist[i][d]);
		cout << res << endl;
	}
}
accuracy: 1
time_ms: 190
memory_kb: 3868
final_rank: 13

code_snippet: #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;
}
accuracy: 1
time_ms: 10
memory_kb: 3452
final_rank: 10

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <stack>
#include <list>

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,b,n) for(int i=b;i<n;i++)
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

const float PI = 3.1415926;
static const int INFTY = (1<<30);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int MAX = 100;

using namespace std;

typedef long long ll;
typedef long double ld;

int c, n, m, s, d;
int C[101][101], D[101][101];
int DP[101][16];

// co=割引券, cost=現在の値段
void solve(int st, int co, int cost) {
  if (st == d-1) {
    return;
  }
  // 次の町探し
  rep(i, n) {
    if (i == st || C[st][i] == INFTY) {
      continue;
    }
    int tmp = cost + C[st][i];
    int tmp2 = cost + C[st][i]/2;
    if (DP[i][co] > tmp) {
      DP[i][co] = tmp;
      solve(i, co, tmp);
    }
    if (co < c && DP[i][co+1] > tmp2) {
      DP[i][co+1] = tmp2;
      solve(i, co+1, tmp2);
    }
  }
}


int main() {
  while (cin >> c >> n >> m >> s >> d, c) {
    rep(i, n) {
      rep(j, n) {
        C[i][j] = INFTY;
        D[i][j] = INFTY;
      }
      rep (q, c+1) {
        DP[i][q] = INFTY;
      }
    }
    rep(i, m) {
      int a,b,f;
      cin >> a >> b  >> f;
      C[a-1][b-1] = f;
      C[b-1][a-1] = f;
    }
    solve(s-1, 0, 0);
    int res = INFTY;
    rep(i, c+1) {
      res = min(res, DP[d-1][i]);
    }
    cout << res << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 3168
final_rank: 17

code_snippet: #include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;

const int INF = 1<<30;
int c, n, m, s, d, a, b, f;
int path[101][101];
int dp[101][11] = {};

void solve(){
    // { cost, {source, ticket} }
    priority_queue<pair<int, pair<int,int>>> pq;
    pq.push({0, {s, c}});
    dp[s][c] = 0;

    while(!pq.empty()){
        auto p = pq.top();  pq.pop();
        int cost = -p.first;
        int source = p.second.first;
        int ticket = p.second.second;

        for(int dst = 0; dst < n; dst++){
            if(path[source][dst] == 0)  continue;

            if(dp[dst][ticket] > cost + path[source][dst]){
                dp[dst][ticket] = cost + path[source][dst];
                pq.push({-dp[dst][ticket], {dst, ticket}});
            }

            if(ticket > 0 && dp[dst][ticket-1] > cost + path[source][dst]/2){
                dp[dst][ticket-1] = cost + path[source][dst]/2;
                pq.push({-dp[dst][ticket-1], {dst, ticket-1}});
            }
        }
    }
}

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

        s--; d--;
        // initialize
        memset(path, 0, sizeof(path));
        for(int i = 0; i < 101; i++){
            for(int j = 0; j < 11; j++){
                dp[i][j] = INF;
            }
        }

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

        solve();

        int ans = INF;
        for(int ticket = 0; ticket <= c; ticket++)  ans = min(ans, dp[d][ticket]);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3140
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

#define to first
#define dist second

const int INF = 1e9;

int main() {
    int c, n, m, s, d;
    while (cin >> c >> n >> m >> s >> d, c) {
        s--, d--;
        vector <vector <pair <int, int>>> G(n);
        rep (i, m) {
            int a, b, f;
            cin >> a >> b >> f;
            a--, b--;
            G[a].emplace_back(b, f);
            G[b].emplace_back(a, f);
        }

        // state -> (dist, now, num of ticket)
        using state = tuple <int, int, int>;
        priority_queue <state> que;
        vector <vector <int>> dp(n, vector <int> (c + 1, INF));
        dp[s][0] = 0;
        que.emplace(0, s, 0);

        while (!que.empty()) {
            state p = que.top(); que.pop();
            int dist, now, numOfTicket;
            tie(dist, now, numOfTicket) = p;

            for (auto & next : G[now]) {
                if (numOfTicket < c && dp[next.to][numOfTicket + 1] > dist + next.dist / 2) {
                    dp[next.to][numOfTicket + 1] = dist + next.dist / 2;
                    que.emplace(dp[next.to][numOfTicket + 1], next.to, numOfTicket + 1);
                }
                if (dp[next.to][numOfTicket] > dist + next.dist) {
                    dp[next.to][numOfTicket] = dist + next.dist;
                    que.emplace(dp[next.to][numOfTicket], next.to, numOfTicket);
                }
            }
        }
        int ans = INF;
        rep (i, c + 1) ans = min(ans, dp[d][i]);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 3148
final_rank: 15

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <tuple>
#include <unordered_map>
#include <list>
#include <numeric>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <ctime>
#include <cassert>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long>vll;
typedef pair<int,int> pi;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ddx[8]={-1,-1,0,1,1,1,0,-1};
int ddy[8]={0,1,1,1,0,-1,-1,-1};
bool debug=false;

/*---------------------------------------------------*/

/* 現在地 コスト 使った枚数 */
typedef tuple<int,int,int> tp;
typedef pair<ll,ll> P;

/* 町 使った割引券の数 */
ll dist[105][15];
vector<P> G[105];
const ll MAX=LLONG_MAX/10;

void init(){
  for(int i=0;i<105;i++){
    G[i].clear();
    for(int j=0;j<15;j++){
      dist[i][j]=MAX;
    }
  }
}

void solve(int c,int n,int m,int s,int d){
  init();
  int a,b,f;
  for(int i=0;i<m;i++){
    cin>>a>>b>>f;
    G[a].push_back(mp(b,f));
    G[b].push_back(mp(a,f));
  }

  /* 現在地 使った枚数 */
  priority_queue<tp,vector<tp>,greater<tp> > que;
  que.push(make_tuple(s,0,0));
  dist[s][0] = 0;
  while(que.size()){
    ll now,cost,num;
    tie(now,cost,num)=que.top();que.pop();
    if(dist[now][num]<cost)continue;
    for(auto x : G[now]){      
      /* 割引券を使わない */
      if(cost+x.second<dist[x.first][num]){
        dist[x.first][num] = cost+x.second;
        que.push(make_tuple(x.first,dist[x.first][num],num));
      }
      /* 割引券が残っていたら使う */
      if(num<c&&cost+x.second/2<dist[x.first][num+1]){
          dist[x.first][num+1] = cost+x.second/2;
          // cout<<now<<" "<<x.first<<" = "<< dist[x.first][num+1] << endl;
          que.push(make_tuple(x.first,dist[x.first][num+1],num+1));
        }
    }
  }

  ll ans=LLONG_MAX;
  for(int i=0;i<15;i++)
      ans=min(ans,dist[d][i]);
  ans=min(ans,dist[d][c]);
  
  cout<<ans<<endl;
}


int main(){
  int c,n,m,s,d;
  while(cin>>c>>n>>m>>s>>d,c|n|m|s|d){
    solve(c,n,m,s,d);
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3120
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define M 100000007
#define ff first.first
#define fs first.second;
typedef pair<int,int> P;
typedef pair<P,int> Pi;
int main(void){
	int c,n,m,s,d;
	while(1){
		cin>>c>>n>>m>>s>>d;
		if(n==0)break;
		vector<P> cost[n+1];
		for(int i=0;i<m;i++){
			int a,b,l;
			cin>>a>>b>>l;
			cost[a].push_back(P(b,l)); cost[b].push_back(P(a,l));
		}
		int leng[c+1][n+1];
		priority_queue<Pi> que;
		for(int i=0;i<=c;i++) fill(leng[i],leng[i]+n+1,M);
		for(int i=0;i<=c;i++) leng[i][s]=0;
		que.push(Pi(P(0,0),s));
		while(!que.empty()){
			Pi p=que.top(); que.pop();
			int v=p.second,w=p.fs;
			if(leng[w][v]<p.ff) continue;
			for(int i=0;i<cost[v].size();i++){
				P x=cost[v][i];
				int t=leng[w][v]+x.second;
				if(leng[w][x.first]>t){
					leng[w][x.first]=t;
					que.push(Pi(P(t,w),x.first));
				}
				t-=x.second/2;
				if(w<c&&leng[w+1][x.first]>t){
					leng[w+1][x.first]=t;
					que.push(Pi(P(t,w+1),x.first));
				}
			}
		}
		int mi=M;
		for(int i=0;i<=c;i++) mi=min(mi,leng[i][d]);
		cout<<mi<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 3116
final_rank: 16

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long; using pii = pair<int, int>;
const int MOD = (int)1e9 + 7, INF = (1 << 27); const ll INFLL = (1LL << 55);
#define FOR(i,a,b) for(int (i)=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
template<typename T> void dbg(T x) { cout << x << "\n"; }
template<typename T, typename U> static void chmin(T &x, U y) { if (y < x) x = y; }
template<typename T, typename U> static void chmax(T &x, U y) { if (x < y) x = y; }


int n, m;

// O(n^2)
using Dist = int;
const int VMAX = 110;
Dist cost[VMAX][VMAX];
Dist d[12][VMAX];
void dijkstra(int s, int a) {
    d[a][s] = 0;
    vector<int> used(n, 0);
    while (1) {
        int v = -1;
        for (int to = 0; to < n; to++) {
            if (used[to]) continue;
            if (v == -1 || d[a][to] < d[a][v]) v = to;
        }
        if (v == -1) break;
        used[v] = 1;
        for (int to = 0; to < n; to++) {
            Dist nd = d[a][v] + cost[v][to];
            if (a > 0) chmin(nd, d[a - 1][v] + cost[v][to] / 2);
            if (nd < d[a][to]) d[a][to] = nd;
        }
    }
}

void init() {
    fill_n(*d, 12 * VMAX, INF);
    fill_n(*cost, VMAX * VMAX, INF);
}

int main() {
    int c, s, t;
    while (cin >> c >> n >> m >> s >> t, c) {
        init();
        s--; t--;
        rep(i, m) {
            int a, b, f; cin >> a >> b >> f;
            a--; b--;
            cost[a][b] = cost[b][a] = f;
        }
        rep(a, c + 1) dijkstra(s, a);
        cout << d[c][t] << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3104
final_rank: 1

code_snippet: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <numeric>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 10007;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);

typedef std::pair<int, int> P;
typedef pair<int, P> pp;

const int smax = 100100;

vector<P> edge[smax];
int c, n, m, s, d; // c...??????????????° n...?????? m...??? s...????????? d...?????? 
LL v[15][smax]; //15....max of tickets

int solve() {
	v[c][s] = 0; //?????????
	priority_queue<pp, vector<pp>, greater<pp> > q;
	q.push(pp(0, P(s, c))); //cost,now,ticket(now)
	while (!q.empty()) {
		pp V = q.top();
		q.pop();
		int now_v = V.second.first;
		int now_t = V.second.second;
		if (v[now_t][now_v] < V.first) continue; //v.first(?????????)???v[now_t][now_v]???????????§????????£???????????????????????§?????????

												 //not use tickets
		for (int i = 0; i<edge[now_v].size(); i++) {
			if (v[now_t][edge[now_v][i].first] > v[now_t][now_v] + edge[now_v][i].second) {
				v[now_t][edge[now_v][i].first] = v[now_t][now_v] + edge[now_v][i].second;
				q.push(pp(v[now_t][now_v] + edge[now_v][i].second, P(edge[now_v][i].first, now_t)));
			}
		}

		// use tickets
		if (now_v > 0) {
			for (int i = 0; i<edge[now_v].size(); i++) {
				P p = edge[now_v][i];
				int next_c = p.second / 2;
				if (now_t == 0)continue;
				if (v[now_t - 1][p.first] > v[now_t][now_v] + next_c) {
					v[now_t - 1][p.first] = v[now_t][now_v] + next_c;
					q.push(pp(v[now_t - 1][p.first], P(p.first, now_t - 1)));
				}
			}
		}
	}
	LL ans = INF;
	for (int i = 0; i<c + 1; i++) {
		ans = min(ans, v[i][d]);
	}
	return ans;
}

int main(void) {
	while (cin >> c >> n >> m >> s >> d && c | n | m | s | d) {
		for (int i= 0; i < smax;i++) edge[i].clear();
		fill(v[0], v[0] + 15 * smax, INF);
		for (int i = 0; i < m;i++) {
			int a, b, f;
			cin >> a >> b >> f;
			edge[a].push_back(P(b, f));
			edge[b].push_back(P(a, f));
		}
		cout << solve() << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 17192
final_rank: 20

code_snippet: #include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(void) {
	while(true) {
		ll c, n, m, s, d;
		cin>>c>>n>>m>>s>>d;
		if(c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break;
		--s;
		--d;
		vector<vector<P> > p(n);
		vector<vector<ll> > dir(n, vector<ll>(c + 1, INF * INF));
		REP(i, m) {
			int a, b, f;
			cin>>a>>b>>f;
			--a;
			--b;
			p[a].pb(P(b, f));
			p[b].pb(P(a, f));
		}
		priority_queue<pair<P, ll> > que;
		que.push(pair<P, ll>(P(0, s), c));
		dir[s][c] = 0;
		while(!que.empty()) {
			pair<P, ll> np = que.top();
			que.pop();
			if(dir[np.first.second][np.second] != np.first.first) continue;
			REP(i, p[np.first.second].size()) {
				if(dir[p[np.first.second][i].first][np.second] > dir[np.first.second][np.second] + p[np.first.second][i].second) {
					dir[p[np.first.second][i].first][np.second] = dir[np.first.second][np.second] + p[np.first.second][i].second;
					que.push(pair<P, ll>(P(dir[p[np.first.second][i].first][np.second], p[np.first.second][i].first), np.second));
				}
				if(np.second > 0 && dir[p[np.first.second][i].first][np.second - 1] > dir[np.first.second][np.second] + p[np.first.second][i].second / 2) {
					dir[p[np.first.second][i].first][np.second - 1] = dir[np.first.second][np.second] + p[np.first.second][i].second / 2;
					que.push(pair<P, ll>(P(dir[p[np.first.second][i].first][np.second - 1], p[np.first.second][i].first), np.second - 1));
				}
			}
		}
		ll res = INF * INF;
		REP(i, c + 1) {
			res = min(res, dir[d][i]);
		}
		cout<<res<<endl;
	}
}
accuracy: 1
time_ms: 310
memory_kb: 3188
final_rank: 18

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

const int INF = 1000000000;

struct Edge{
  int to,cost;
};

struct Node{
  int val,num;
};

struct P{
  int val,num,c;
  bool operator<(const P &r)const{
    return val < r.val;
  }
};




int main(){
  int c,n,m,s,d;
  
  while(1){
  cin >> c >> n >> m >> s >> d;
  s--;d--;
  if(c == 0 && n == 0 && m == 0 && s == -1 && d == -1)break;
  vector<Edge>edge[105];
  Node node[11][105];
  
  priority_queue<P> que;
  for(int i = 0; i < m; i++){
    int a,b,c;
    cin >> a >> b >> c;
    a--;b--;
    edge[a].push_back((Edge){b,c});
    edge[b].push_back((Edge){a,c});
  }


  for(int i = 0; i < 11; i++){
    for(int j = 0; j < n; j++){
      node[i][j].val = INF;
      node[i][j].num = j;
    }
  }

  node[c][s].val = 0;
  
  que.push((P){0,s,c});

  while(!que.empty()){
    int val = que.top().val;
    int now = que.top().num;
    int ticket = que.top().c;
    que.pop();
  
    if(ticket){
      for(int i = 0; i < edge[now].size(); i++){
	int next = edge[now][i].to;
	int cost = edge[now][i].cost;

	if(node[ticket - 1][next].val > node[ticket][now].val + cost / 2){
	  node[ticket - 1][next].val = node[ticket][now].val + cost / 2;
	  que.push((P){node[ticket - 1][next].val,next,ticket - 1});
	}
      } 
    }

    for(int i = 0; i < edge[now].size(); i++){
      int next = edge[now][i].to;
      int cost = edge[now][i].cost;
      
      if(node[ticket][next].val > node[ticket][now].val + cost){
	node[ticket][next].val = node[ticket][now].val + cost;
	que.push((P){node[ticket][next].val,next,ticket});
      }
    }
  }
  
  int ans = INF;

  for(int i = 0; i < c; i++){
    ans = min(ans,node[i][d].val);
  }

  cout << ans << endl;
  }
}
accuracy: 1
time_ms: 270
memory_kb: 3036
final_rank: 14

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

using namespace std;

int minimum,min_past_num,num_ticket,num_town,m,start,goal;


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

struct Data{
	Data(int arg_to,int arg_cost){
		to = arg_to;
		cost = arg_cost;
	}
	int to,cost;
};

vector<Data> V[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 = 0; i < V[info.current_town].size(); i++){
        if(info.visited[V[info.current_town][i].to] == false){
            Info new_info;
            for(int k = 1; k <= num_town; k++)new_info.visited[k] = info.visited[k];
            new_info.visited[V[info.current_town][i].to] = true;
            new_info.current_town = V[info.current_town][i].to;

            if(info.index == 0 || V[info.current_town][i].cost > info.costs[0]){
            	new_info.costs[0] = V[info.current_town][i].cost;
            	for(int k = 1; k <= info.index; k++){
            		new_info.costs[k] = info.costs[k-1];
            	}
            }else if(V[info.current_town][i].cost <= 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] = V[info.current_town][i].cost;
            }else{
            	for(calc = 0; info.costs[calc] >= V[info.current_town][i].cost;calc++)new_info.costs[calc] = info.costs[calc];
				new_info.costs[calc] = V[info.current_town][i].cost;
				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++)V[i].clear();

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

            V[from].push_back(Data(to,fee));
            V[to].push_back(Data(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;
}
accuracy: 1
time_ms: 880
memory_kb: 2900
final_rank: 19