Problem:
E : Arojam's Mask / 仮面
問題文
あなたは勇者である。勇者が旅をしている世界は
N
個の街と、異なる街同士を結ぶ
M
本の道からなる。道
i
は街
a_i
と街
b_i
を結び、双方向に移動可能である。
街
S
から出発し、街
T
に移動することが勇者の目的である。
S,T
は異なる街である。勇者は
0
日目に街を出発し、ちょうど
1
日かけて
1
本の道を通って移動することができる。道を使った移動には日数の制限
R
があり、
R
日目を超えることはできない。
また、勇者は任意の街で「オカリナを吹く」こともできる。オカリナを吹くと、時は
0
日目に、位置は街
S
に戻される。つまり、
R
回を超える移動を行う場合は、最低一度はオカリナを吹かなければならない。
さらに、勇者がある街に位置すると「イベント」が発生することがある。イベントは
E
種類あり、
i
番目のイベントは、街
c_{i}
で発生する。イベントは勇者がそこにいると自動的に発生し、新たに道
a’_i
と
b’_i
を結ぶ道が生じる。この道はオカリナを吹いても消えることはなく、初めから存在する道とも、別のイベントによって追加される道とも重複しない。ある町で起こるイベントは高々1つである。
さて、勇者の今日の仕事は、街
T
に到達するまでに必要な移動の回数とオカリナを吹く回数の和の最小値を求めるプログラムを書くことである。
入力
入力は以下の形式で与えられる。
N
M
E
S
T
R
a_{0}
b_{0}
…
a_{M−1}
b_{M−1}
a’_{0}
b’_{0}
c_{0}
…
a’_{E−1}
b’_{E−1}
c_{E−1}
制約
入力はすべて整数である
2 \≤ N \≤ 100
1 \≤ M+E \≤ N(N−1)
/
2
0 \≤ E \≤ 4
0 \≤ R \≤ 100
0 \≤ S,T,a_i,b_i,a’_i,b’_i,c_i \≤ N−1
S ≠ T
a_{i} ≠ b_{i}, a’_{i} ≠ b’_{i}
a_{i}
と
b_{i}
、
a’_{i}
と
b’_{i}
の全ての組は重複しない
i ≠ j
なら
c_{i} ≠ c_{j}
出力
答えを一行で出力せよ。どうしても
T
へ到達できない場合は
−1
を出力せよ。
サンプル
サンプル入力1
8 5 2 0 5 5
0 1
0 3
0 6
1 2
6 7
3 4 7
4 5 2
サンプル出力1
9
例えば以下のように移動すればよい。イベントを起こすまでは道(4,5), (3,4)は存在しないことに注意せよ。
街2に行くと4から5へ移動できるようになる
オカリナを使用し0へ戻る
街7に行くと3から4へ移動できるようになる
オカリナを使用し0へ戻る
0から5へまっすぐ進む
サンプル入力2
7 5 1 0 6 8
0 1
1 2
2 3
3 4
4 5
3 6 5
サンプル出力2
8
オカリナを1度も吹かない経路が最適である。
サンプル入力3
4 1 4 1 2 3
3 1
3 2 0
0 1 3
0 3 1
0 2 2
サンプル出力3
5
街
S
でイベントが起こることもある。

Submitted solutions:

code_snippet: /*＿＿＿＿＿＿
　　 ||　　　　  　|
　　 ||ACはこちらへ|
　　 ||　　　　  　|
　　 ||￣￣￣￣￣￣ 
∧_∧||
(`･ω･||
（ つ||０
　uｰu*/
#include <bits/stdc++.h>
using namespace std;
//output and debug
template <class T> using V = vector<T>;
template <class T, class U> 
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << "P(" << p.first << ", " << p.second << ")";
}
template <class T> ostream& operator<<(ostream& os,const V<T>& v) {
    os << "[";
    bool f = false;
    for (auto d : v) {
        if (f) os << ", ";
        f = true;
        os << d;
    }
    return os << "]";
}
template <class T> ostream& operator<<(ostream& os,const set<T>& s) {
    os << "{";
    bool f = false;
    for (auto d : s) {
        if (f) os << ", ";
        f = true;
        os << d;
    }
    return os << "}";
}
template <class T> ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << "{";
    bool f = false;
    for (auto d : s) {
        if (f) os << ", ";
        f = true;
        os << d;
    }
    return os << "}";
}
template <class T, class U>
ostream& operator<<(ostream& os, const map<T, U>& s) {
    os << "{";
    bool f = false;
    for (auto p : s) {
        if (f) os << ", ";
        f = true;
        os << p.first << ": " << p.second;
    }
    return os << "}";
}
template<class T, class... Ts>
void o(const T& a, const Ts&... b){
    cout << a;
    (cout << ... << (cout << ' ', b));
    cout << '\n';
}
#ifdef LOCAL
#define dbg(...)                                            \
    do {                                                    \
        cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \
        o(__VA_ARGS__);                                  \
        cerr << endl;                                       \
    } while (false);
#else
#define dbg(...)
#endif
// input
template<class... T>
void in(T&... a){
    (cin >> ... >> a);
}
template<class T> void in(vector<T>& a){ for(auto&& i : a) in(i); }
template<class T, class L> void in(pair<T, L>& p){ in(p.first); in(p.second); }
template<class T> void in(T& a){ cin >> a; }

#define rep1(n)          for(ll i = 0; i < n; ++i)
#define rep2(i, n)       for(ll i = 0; i < n; ++i)
#define rep3(i, j, n)    for(ll i = j; i < n; ++i)
#define overload3(a, b, c,  e, ...) e
#define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(i,n) for(ll i = n-1;i>=0;--i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
using ll = long long ;
using ld = long double;
using pa = pair<ll,ll>;
using t2 = tuple<ll,ll>;
using tu = tuple<ll,ll,ll>;
using vi = vector<ll>;
using vs = vector<string>;
using vp = vector<pa>;
using vvi = vector<vi>;
using vvp = vector<vp>;
using vvvi = vector<vvi>;
using v4 = vector<vvvi>;
using vt = vector<tu>;
const ll INF = (1ll << 29);
const ll inf = INF*INF;
const int dh[4]={1,-1,0,0};
const int dw[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
const int dx[4]={0,0,1,-1};
const string mv = "DURL";
void Yes(bool i = true){ return o(i?"Yes":"No"); }

//reversed priority_queue
template<class T>
class prique :public std::priority_queue<T, std::vector<T>, std::greater<T>> {};
const ll mod = 998244353;
//const ll mod = 1000000007;


int main(){
	ll n,m,e,s,t,r;
	cin >> n >> m >> e >> s >> t >> r;
	vvi g(n);
    bool ch = false;
	rep(i,m){
		ll u,v;
		cin >> u >> v;
        if(u == s && v == t) ch = true;
        if(u == t && v == s) ch = true;
		g[u].push_back(v);
		g[v].push_back(u);
	}
    if(ch){
        o(1);
        return 0;
    }
    vi ev(e),eu(e),ec(e);
    rep(i,e){
        cin >> ev[i] >> eu[i] >> ec[i];
    }
    ec.push_back(t);
    ll ee = (1LL<<(e+1));
    vector<vvvi> dist(n,vvvi(ee,vvi(e+1,vi(r+1,inf))));
    auto bfs = [&](ll x){
    	queue<tuple<ll,ll,ll,ll,ll>> q;
    	q.push({0,x,0,0,0});
    	while(!q.empty()){
			ll v,c,sou,o,now;
        	tie(c,v,sou,o,now) = q.front();
        	q.pop();
        	rep(i,e+1) if(v == ec[i]) sou |= (1LL<<i);
            if(dist[v][sou][o][now] <= c) continue;
        	dist[v][sou][o][now] = c;
        	++c;
            ++now;
            if(now == r+1) continue;
        	for(auto u:g[v]){
            	if(dist[u][sou][o][now] <= c) continue;
            	q.push({c,u,sou,o,now});
        	}
            rep(i,e) if(sou >> i & 1){
                ll u = inf;
                if(ev[i] == v) u = eu[i];
                if(eu[i] == v) u = ev[i];
                if(u == inf) continue;
                if(dist[u][sou][o][now]<=c)continue;
                q.push({c,u,sou,o,now}); 
            }
            ++o;
            ll u = s;
            if(o == e+1) continue;
            now = 0;
            if(dist[u][sou][o][now]<=c)continue;
            q.push({c,u,sou,o,now}); 
    	}
    return ;
	};
    bfs(s);
    ll ans = inf;
    rep(bit,ee) if(bit >> e & 1){
        rep(i,e+1)rep(x,r+1) ans = min(ans,dist[t][bit][i][x]);
    }
    if(ans == inf) o(-1);
    else o(ans);
    //dbg(dist);
}
accuracy: 1
time_ms: 30
memory_kb: 7732
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#define rep(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define all(v) (v).begin(),(v).end()
using namespace std;
using ll = long long;

template<typename T>
bool chmin(T &a, const T &b){
    if (a <= b) return false;
    a = b;
    return true;
}


template<typename T>
bool chmax(T &a, const T &b){
    if (a >= b) return false;
    a = b;
    return true;
}

using vi=vector<int>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using P=pair<int,int>;
using vp=vector<P>;
using vvp=vector<vp>;
const int inf=1<<30;

int main(){
    cin.tie(0)->sync_with_stdio(0);

    ll n,m,e,s,t,r;
    cin>>n>>m>>e>>s>>t>>r;

    vvp g(n);
    rep(i,0,m){
        int u,v;
        cin>>u>>v;
        g[u].emplace_back(v,-1);
        g[v].emplace_back(u,-1);
    }
    vi a(n,-1);
    rep(i,0,e){
        int u,v,w;
        cin>>u>>v>>w;
        a[w]=i;
        g[u].emplace_back(v,i);
        g[v].emplace_back(u,i);
    }

    vvvi dp(n,vvi(r+1,vi(1<<e,inf)));
    dp[s][0][(a[s]>=0?1<<a[s]:0)]=0;
    queue<tuple<int,int,int>> q;
    q.push({s,0,(a[s]>=0?1<<a[s]:0)});
    while(!q.empty()){
        auto [v,ct,S]=q.front();q.pop();
        auto f=[&](int u,int dt,int T){
            if(dp[u][dt][T]==inf){
                dp[u][dt][T]=dp[v][ct][S]+1;
                q.push({u,dt,T});
            }
        };
        f(s,0,S);
        if(ct==r) continue;
        for(auto [u,w]:g[v]){
            if(w>=0&&!(S&1<<w)) continue;
            f(u,ct+1,S|(a[u]>=0?1<<a[u]:0));
        }
    }
    int rs=inf;
    rep(v,0,n)rep(j,0,r+1)rep(S,0,1<<e)if(v==t) chmin(rs,dp[v][j][S]);
    if(rs==inf) rs=-1;
    cout<<rs<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4216
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)
#define TT template<typename T>
TT using vec = vector<T>;
TT using minheap = priority_queue<T, vector<T>, greater<T>>;
TT using maxheap = priority_queue<T>;

struct io_setup {
    io_setup() {
        ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        cout << fixed << setprecision(15);
    }
} io_setup;


TT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }
TT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }

TT int LB(vector<T>& v, T x) { return lower_bound(all(v), x) - begin(v);}
TT int UB(vector<T>& v, T x) { return upper_bound(all(v), x) - begin(v);}

const int inf = 1001001001;
const ll INF = 2002002002002002002;


struct dsu {
    using vi = vector<int>;   
    using vvi = vector<vector<int>>;
    private:
    vi par;
    vi sz;
    int cc;
    vi es;

    public:

    dsu(int n) {
        par.resize(n);
        sz.resize(n, 1);
        es.resize(n, 0);
        cc = n;
        rep(i, 0, n) {
            par[i] = i;
        }
    }
  
    int leader(int u) {
        if (par[u] != u) {
            return par[u] = leader(par[u]);
        }    
         return u;
    }
    
    bool same(int a, int b) {
        return leader(a) == leader(b);
    }
    
    bool merge(int a, int b) {
        a = leader(a), b = leader(b);
        if(sz[a] < sz[b]) swap(a, b);

        if(a == b) {
            ++es[a];
            return false;
        }
        else {
            cc--;
            par[b] = leader(a);
            sz[a] += sz[b];
            es[a] += es[b] + 1;
            return true;
        }
    }

    int size(int u) {
        return sz[leader(u)];
    }

    
    int componentcnt() {
        return cc;
    }


    
    int edgecnt(int u) {
        return es[leader(u)];
    }

    vvi groups() {
        int n = par.size();
        vvi ms(n);
        rep(v, 0, n) {
            ms[leader(v)].push_back(v);
        }

        vvi res;
        rep(i, 0, n) if(ms[i].size() > 0) {
            res.push_back(ms[i]);
        }

        return res;
    }

};

/*
@brief dsu
@docs doc/dsu.md
*/

int main() {
	ll N, M, E, S, T, R;
	cin >> N >> M >> E >> S >> T >> R;
	vec<vec<int>> G(N);
	vec<ll> A(M), B(M);
	rep(i, 0, M) {
		cin >> A[i] >> B[i];
		G[A[i]].push_back(B[i]);
		G[B[i]].push_back(A[i]);
	}


	vec<ll> AA(E), BB(E), CC(E);
	rep(i, 0, E) cin >> AA[i] >> BB[i] >> CC[i];

	rep(i, 0, E) {
		if(CC[i] == S) {
			G[AA[i]].push_back(BB[i]);
			G[BB[i]].push_back(AA[i]);
		}
	}
	

	ll ans = LLONG_MAX;
	
	vec<ll> PERMU(E);
	iota(all(PERMU), 0);

	auto cal = [&](vec<vec<int>>& g, int s, int t) {
		vec<int> dist(N, inf);
		dist[s] = 0;
		queue<int> que;
		que.push(s);

		while(!que.empty()) {
			int now = que.front();
			que.pop();

			for(int to : g[now]) {
				if(dist[to] > dist[now] + 1) {
					dist[to] = dist[now] + 1;
					que.push(to);
				}
			}
		}
		return dist[t];
	};



	rep(c, 0, E+1) {
		vec<ll> permu = PERMU;
		do {
			rep(t, 0, (1LL << c)) {

				ll sum = 0;
			    int now = S;
				vec<vec<int>> ng = G;
				ll r = 0;



				rep(i, 0, c) {//移動する。
				    ll d = cal(ng, now, CC[permu[i]]);
					r += d;
					if(r > R) sum += inf;
					sum += d;//イベントが起こるのはC[t]
					if(t >> i & 1) {
						sum++;
						now = S;
						r = 0;
					}
					else {
						now = CC[permu[i]];
					}
					ng[AA[permu[i]]].push_back(BB[permu[i]]);
					ng[BB[permu[i]]].push_back(AA[permu[i]]);
				}

				ll d = cal(ng, now, T);
				r += d;
				if(r > R) sum += inf;
				sum += d;
				chmin(ans, sum);

			}
		}while(next_permutation(all(permu)));
		
	}
    if(ans >= inf) {
		cout << -1 << endl;
		return 0;
	}
	cout << ans << endl;
	
	
}
accuracy: 1
time_ms: 10
memory_kb: 3504
final_rank: 4

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

#define REP(i, n) for(ll i = 0;i < n;i++)
#define REPR(i, n) for(ll i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define FORR(i, m, n) for(ll i = m;i >= n;i--)
#define REPO(i, n) for(ll i = 1;i <= n;i++)
#define ll long long
#define INF (ll)1ll << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(),n.end()
#define MOD (ll)1000000007
#define P pair<ll, ll>

ll N, M, E, S, T, R;
#define PP pair<ll, P>
#define PPP pair<ll, PP>
ll d[110][17][110]; // 結果
struct edge { ll to, cost; };
vector<edge> g[110]; //隣接リスト to, cost
vector<PP> ag;

void dijkstra(ll a){
    priority_queue<PPP, vector<PPP>, greater<PPP> > q; // cost, to, 条件

    d[a][0][0] = 0;
    q.push(PPP(0, PP(a, P(0, 0))));
    while (!q.empty()) {
        PPP p = q.top();
        q.pop();
        ll v = p.second.first, fl = p.second.second.first, st = p.second.second.second;
        if (d[v][fl][st] < p.first) continue;
        REP(i, g[v].size()) {
            edge e = g[v][i];

            // normal
            if (st < R and d[e.to][fl][st + 1] > d[v][fl][st] + e.cost) {
                d[e.to][fl][st + 1] = d[v][fl][st] + e.cost;
                q.push(PPP(d[e.to][fl][st + 1], PP(e.to, P(fl, st + 1))));
            }
        }
        //okarina
        if (d[a][fl][0] > d[v][fl][st] + 1) {
            d[a][fl][0] = d[v][fl][st] + 1;
            q.push(PPP(d[a][fl][0], PP(a, P(fl, 0))));
        }
        //event
        REP(i, E){
            ll ca = ag[i].second.second, fr = ag[i].first, to = ag[i].second.first;
            if(v == ca and !(fl & (1 << i))){
                if(d[v][fl | (1 << i)][st] > d[v][fl][st]){
                    d[v][fl | (1 << i)][st] = d[v][fl][st];
                    q.push(PPP(d[v][fl | (1 << i)][st], PP(v, P(fl | (1 << i), st))));
                }
            }

            if(st < R and v == fr and fl & (1 << i)){
                if(d[to][fl][st + 1] > d[v][fl][st] + 1){
                    d[to][fl][st + 1] = d[v][fl][st] + 1;
                    q.push(PPP(d[to][fl][st + 1], PP(to, P(fl, st + 1))));
                }
            }
            if(st < R and v == to and fl & (1 << i)){
                if(d[fr][fl][st + 1] > d[v][fl][st] + 1){
                    d[fr][fl][st + 1] = d[v][fl][st] + 1;
                    q.push(PPP(d[fr][fl][st + 1], PP(fr, P(fl, st + 1))));
                }
            }
        }
    }
}
int main(){
    cin >> N >> M >> E >> S >> T >> R;
    REP(i, 110)REP(j, 17)REP(k, 110) d[i][j][k] = INF;
    REP(i, M){
        ll a, b;
        cin >> a >> b;
        g[a].push_back({b, 1});
        g[b].push_back({a, 1});
    }
    REP(i, E){
        ll a, b, c;
        cin >> a >> b >> c;
        ag.push_back(PP(a, P(b, c)));
    }
    dijkstra(S);
    ll ans = INF;
    REP(i, 17)REP(j, R + 1)ans = min(ans, d[T][i][j]);
    cout << (ans == INF ? -1 : ans) << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 5208
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
using ll = long long;
using ld = long double;
const ll INF = (1ll<<61) - 1;
template<class T> bool chmax(T &a,T b){
    if(a<b){
        a=b;
        return true;
    }
    return false;
}

template<class T> bool chmin(T &a,T b){
    if(a>b){
        a=b;
        return true;
    }
    return false;
}
#define all(p) p.begin(),p.end()
const int mod = 1'000'000'007;
int main(){
    int N,M,E,S,T,R;
    cin>>N>>M>>E>>S>>T>>R;
    struct edge{
        int to;
        int ty;
    };
    vector<vector<edge>> G(N);
    rep(i,0,M){
        int a,b;
        cin>>a>>b;
        G[a].push_back({b,-1});
        G[b].push_back({a,-1});
    }
    vector<vector<int>> H(N);
    rep(i,0,E){
        int a,b,c;
        cin>>a>>b>>c;
        G[a].push_back({b,(int)i});
        G[b].push_back({a,(int)i});
        H[c].push_back(i);
    }
    vector dp(1<<E,vector(N,vector<ll>(R+1,INF)));
    vector<tuple<int,int,int>> order={{0,S,0}};;
    dp[0][S][0]=0;
    rep(rp,0,order.size()){
        int sta,ind,day;
        tie(sta,ind,day)=order[rp];
        ll cost=dp[sta][ind][day]+1;
        for(auto x:H[ind]) sta=(sta|(1<<x));
        if(day!=R){
            for(auto e:G[ind]){
                if(e.ty!=-1&&(sta&(1<<e.ty))==0) continue;
                if(chmin(dp[sta][e.to][day+1],cost)){
                    order.push_back({sta,e.to,day+1});
                }
            }
        }
        if(chmin(dp[sta][S][0],cost)){
            order.push_back({sta,S,0});
        }
    }
    ll ans=INF;
    rep(i,0,1<<E) rep(k,0,R+1) chmin(ans,dp[i][T][k]);
    cout<<(ans==INF?(ll)(-1):ans)<<"\n";
}
accuracy: 1
time_ms: 10
memory_kb: 5792
final_rank: 14

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

// template {{{
#define  range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)
#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

// constexpr i32 mod   = 998244353;
constexpr i32 mod   = 1e9 + 7;
constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

constexpr int dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr int dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};

struct IoSetup { IoSetup(int x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}

void solver() {
    int n, m, e, s, t, r;
    cin >> n >> m >> e >> s >> t >> r;

    auto G = make_vector(n, 0, 0);
    range(i, 0, m) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    vector< int > as(e), bs(e), cs(e);
    range(i, 0, e) {
        cin >> as[i] >> bs[i] >> cs[i];
    }

    struct type {
        int dist, cnt, bit, day, v;
        type(int dist, int cnt, int bit, int day, int v) : dist(dist), cnt(cnt), bit(bit), day(day), v(v) {}
    };

    auto cmp = [](const type a, const type b) {
        return a.dist > b.dist;
    };

    priority_queue< type, vector< type >, decltype(cmp) > pq{cmp};
    int sbit = 0;
    range(i, 0, e) {
        if (cs[i] == s) sbit = sbit | (1 << i);
    }
    pq.emplace(0, 0, sbit, 0, s);
    auto d = make_vector(5, 1 << e, r + 1, n, inf);
    d[0][sbit][0][s] = 0;

    while (!pq.empty()) {
        auto p = pq.top();
        pq.pop();

        int cnt = p.cnt;
        int bit = p.bit;
        int day = p.day;
        int v = p.v;

        if (p.dist > d[cnt][bit][day][v]) continue;

        if (cnt < 4 && chmin(d[cnt + 1][bit][0][s], d[cnt][bit][day][v] + 1)) {
            pq.emplace(d[cnt + 1][bit][0][s], cnt + 1, bit, 0, s);
        }

        if (day == r) continue;
            
        for (auto u: G[v]) {
            int nbit = bit;
            range(i, 0, e) {
                if (u == cs[i]) nbit = nbit | (1 << i);
            }

            if (chmin(d[cnt][nbit][day + 1][u], d[cnt][bit][day][v] + 1)) {
                pq.emplace(p.dist + 1, cnt, nbit, day + 1, u);
            }
        }

        range(i, 0, e) {
            if ((bit & (1 << i)) == 0) continue;
            if (as[i] == v) {
                int nbit = bit;
                range(j, 0, e) {
                    if (bs[i] == cs[j]) nbit = nbit | (1 << j);
                }
                if (chmin(d[cnt][nbit][day + 1][bs[i]], p.dist + 1)) {
                    pq.emplace(p.dist + 1, cnt, nbit, day + 1, bs[i]);
                }
            }
            if (bs[i] == v) {
                int nbit = bit;
                range(j, 0, e) {
                    if (as[i] == cs[j]) nbit = nbit | (1 << j);
                }
                if (chmin(d[cnt][nbit][day + 1][as[i]], p.dist + 1)) {
                    pq.emplace(p.dist + 1, cnt, nbit, day + 1, as[i]);
                }
            }
        }

    }

    int ans = inf;
    range(cnt, 0, 5) range(bit, 0, (1 << e)) range(day, 0, r + 1) {
        chmin(ans, d[cnt][bit][day][t]);
    }

    if (ans == inf) {
        cout << -1 << endl;
    } else {
        cout << ans << endl;
    }
}

signed main(int argc, char *argv[]) {
    solver();
}
accuracy: 1
time_ms: 240
memory_kb: 5888
final_rank: 18

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

typedef pair<int,int> pii;
int dp[101][101][1<<4];

int main(){
    memset(dp, 0x3f, sizeof(dp));
    int n, m, e, s, t, r;
    cin >> n >> m >> e >> s >> t >> r;
    vector<pii> v[n];
    while(m--){
        int a, b;
        cin >> a >> b;
        v[a].push_back({b,-1});
        v[b].push_back({a,-1});
    }
    vector<int> event(n, -1);
    for(int i = 0; i < e; i++){
        int a, b, c;
        cin >> a >> b >> c;
        event[c] = i;
        v[a].push_back({b, i});
        v[b].push_back({a, i});
    }
    int initStat = 0;
    if(event[s] != -1)  initStat += 1<<event[s];
    dp[s][0][initStat] = 0;
    queue<pair<pii,int>> q;
    q.push({{s,0},initStat});
    while(!q.empty()){
        auto p = q.front(); q.pop();
        int pos = p.first.first, dist = p.first.second, stat = p.second;
        // play ocarina
        if(dp[s][0][stat] > dp[pos][dist][stat]+1){
            dp[s][0][stat] = dp[pos][dist][stat]+1;
            q.push({{s,0},stat});
        }
        if(dist == r)   continue;
        // move
        for(pii next : v[pos]){
            int npos = next.first;
            int roadStat = next.second;
            int nstat = stat;
            if(roadStat != -1 && (stat>>roadStat&1) == 0) continue;
            if(event[npos] != -1)    nstat |= 1<<event[npos];
            if(dp[npos][dist+1][nstat] > dp[pos][dist][stat]+1){
                dp[npos][dist+1][nstat] = dp[pos][dist][stat]+1;
                q.push({{npos,dist+1},nstat});
            }
        }
    }
    int ans = 1<<30;
    for(int i = 0; i <= r; i++){
        for(int j = 0; j < 1<<e; j++){
            ans = min(ans, dp[t][i][j]);
        }
    }
    cout << (ans > 1e9 ? -1 : ans) << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3928
final_rank: 7

code_snippet: #include "iostream"
#include "random"
#include "string"
#include "bitset"
#include "algorithm"
#include "map"
#include "queue"
#include "list"
#include "set"
#include "climits"
#include "iomanip"
#include "utility"
#include "functional"

using namespace std;
using ll = long long int;
using V = tuple<int, int, int>;
using P = pair<ll, V>;

int N, M, E, S, T, R;
int A[10000];
int B[10000];
int eA[10000];
int eB[10000];
int eC[10000];
vector<int> G[100];
ll dist[100][100][1 << 4];
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N >> M >> E >> S >> T >> R;
	for (int i = 0; i < M; i++) {
		cin >> A[i] >> B[i];
		G[A[i]].push_back(B[i]);
		G[B[i]].push_back(A[i]);
	}
	int z = 0;
	for (int i = 0; i < E; i++) {
		cin >> eA[i] >> eB[i] >> eC[i];
		if (eC[i] == S) {
			G[eA[i]].push_back(eB[i]);
			G[eB[i]].push_back(eA[i]);
			z |= (1 << i);
		}
	}
	fill((ll*)dist, (ll*)(dist + 100), 1LL << 60);
	dist[S][0][z] = 0;
	priority_queue<P, vector<P>, greater<P>> Q;
	Q.emplace(0, V(S, 0, z));
	while (!Q.empty()) {
		P p = Q.top(); Q.pop();
		V v = p.second;
		int x = get<0>(v), y = get<1>(v), z = get<2>(v);
		if (dist[x][y][z] < p.first) continue;
		if (y + 1 <= R) {
			for (int to : G[x]) {
				int nz = z;
				for (int i = 0; i < E; i++) {
					if (eC[i] == to) nz |= (1 << i);
				}
				if (dist[to][y + 1][nz] > p.first + 1) {
					dist[to][y + 1][nz] = p.first + 1;
					Q.emplace(p.first + 1, V(to, y + 1, nz));
				}
			}
			for (int i = 0; i < E; i++) {
				if (!(z&(1 << i))) continue;
				if (eA[i] == x) {
					int nz = z;
					for (int j = 0; j < E; j++) if (eC[j] == eB[i]) nz |= (1 << j);
					if (dist[eB[i]][y + 1][nz]>p.first + 1) {
						dist[eB[i]][y + 1][nz] = p.first + 1;
						Q.emplace(p.first + 1, V(eB[i], y + 1, nz));
					}
				}
				else if (eB[i] == x) {
					int nz = z;
					for (int j = 0; j < E; j++) if (eC[j] == eA[i]) nz |= (1 << j);
					if (dist[eA[i]][y + 1][nz]>p.first + 1) {
						dist[eA[i]][y + 1][nz] = p.first + 1;
						Q.emplace(p.first + 1, V(eA[i], y + 1, nz));
					}

				}
			}
		}
		if (dist[S][0][z] > p.first + 1) {
			dist[S][0][z] = p.first + 1;
			Q.emplace(p.first + 1, V(S, 0, z));
		}
	}
	ll ans = 1LL << 60;
	for (int i = 0; i <= R; i++) {
		for (int bit = 0; bit < (1 << E); bit++) {
			ans = min(ans, dist[T][i][bit]);
		}
	}
	if (ans == 1LL << 60) cout << -1 << endl;
	else cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4612
final_rank: 12

code_snippet: #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int>P;
typedef pair<P,P>P2;

int dp[111][111][1<<5];
int n,m,e,s,t,r;

vector<P>v[100000];

signed main(){
  r(i,111)r(j,1<<5)r(k,111)dp[i][k][j]=1e9;
  cin>>n>>m>>e>>s>>t>>r;

  r(i,m){
    int a,b;
    cin>>a>>b;
    v[a].push_back(P(b,-1));
    v[b].push_back(P(a,-1));
  }

  map<int,int>M;
  int state=0;

  r(i,e){
    int a,b,c;
    cin>>a>>b>>c;
    v[a].push_back(P(b,i));
    v[b].push_back(P(a,i));

    M[c] = i;
    if(c==s)state|=(1<<i);
  }

  priority_queue<P2,vector<P2>,greater<P2> > q;

  q.push(P2(P(0,s),P(r,state))); /// warning

  dp[s][r][state]=0;

  while(!q.empty()){
    P2 PP=q.top();q.pop();

    int now=PP.first.second;
    int cost=PP.first.first;
    int R = PP.second.first;
    int S = PP.second.second;

    // cout<<cost<<' '<<now<<' '<<R<<' '<<S<<endl;

    if(dp[now][R][S] < cost)continue;

    // cout<<cost<<' '<<now<<' '<<R<<' '<<S<<endl;

    if(R){
      r(i,v[now].size()){
	int nex = v[now][i].first;
	int flag = v[now][i].second;

	int f=flag%100;
	int nR=R-1;

	int nS = S;
	if(M.count(nex))nS= (nS|(1<<M[nex]));

	if(flag == -1){

	  if(dp[nex][nR][nS] <= cost+1)continue;
	  dp[nex][nR][nS] = cost+1;
	  q.push(P2(P(cost+1,nex),P(nR,nS)));
	}
	else{
	  if(!(S&(1<<f)))continue;

	  if(dp[nex][nR][nS] <= cost+1)continue;
	  dp[nex][nR][nS] = cost+1;
	  q.push(P2(P(cost+1,nex),P(nR,nS)));
	}
      }
    }

    if(dp[s][r][S] <= cost+1)continue;
    dp[s][r][S] = cost+1;
    q.push(P2(P(cost+1,s),P(r,S)));
  }


  int ans= 1e9;

  r(i,101)r(j,(1<<5)){
    ans=min(ans,dp[t][i][j]);
  }

  if(ans==1e9)cout<<-1<<endl;
  else cout<<ans<<endl;

  
  
  
}
accuracy: 1
time_ms: 70
memory_kb: 7116
final_rank: 17

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

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b)a=b;};
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b)a=b;};

const Int INF = 1e15;
Int dp[1<<4][111][111];

signed main(){
  Int n,m,e,s,t,r;
  cin>>n>>m>>e>>s>>t>>r;
  vector<vector<Int>> G(n),H(n);
  for(Int i=0;i<m;i++){
    Int a,b;
    cin>>a>>b;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }

  vector<Int> as(e),bs(e),cs(e);
  for(Int i=0;i<e;i++) cin>>as[i]>>bs[i]>>cs[i];

  for(Int b=0;b<16;b++)
    for(Int i=0;i<111;i++)
      for(Int j=0;j<111;j++)
        dp[b][i][j]=INF;

  using T = tuple<Int, Int, Int>;
  deque<T> que;
  dp[0][s][0]=0;
  que.emplace_back(0,s,0);

  while(!que.empty()){
    Int b,v,d;
    tie(b,v,d)=que.front();que.pop_front();


    // event
    for(Int i=0;i<e;i++){
      if(v==cs[i]){
        Int nb=b|(1<<i);
        Int nv=v;
        Int nd=d;	
        if(dp[nb][nv][nd]>dp[b][v][d]+0){
          dp[nb][nv][nd]=dp[b][v][d]+0;
          que.emplace_front(nb,nv,nd);
        }
      }
    }

    // okarina
    {
      Int nb=b;
      Int nv=s;
      Int nd=0;      
      if(dp[nb][nv][nd]>dp[b][v][d]+1){
        dp[nb][nv][nd]=dp[b][v][d]+1;
        que.emplace_back(nb,nv,nd);
      }
    }
    
    if(d==r) continue;
    
    // normal route
    for(Int u:G[v]){
      Int nb=b;
      Int nv=u;
      Int nd=d+1;      
      if(dp[nb][nv][nd]>dp[b][v][d]+1){
        dp[nb][nv][nd]=dp[b][v][d]+1;
        que.emplace_back(nb,nv,nd);
      }
    }

    // event route
    for(Int i=0;i<e;i++){
      if((~b>>i)&1) continue;
      if(bs[i]==v) swap(as[i],bs[i]);
      if(as[i]!=v) continue;
      // a -> b
      Int nb=b;
      Int nv=bs[i];
      Int nd=d+1;      
      if(dp[nb][nv][nd]>dp[b][v][d]+1){
        dp[nb][nv][nd]=dp[b][v][d]+1;
        que.emplace_back(nb,nv,nd);
      }
    }
  }  
  
  Int ans=INF;  
  for(Int b=0;b<16;b++)
    for(Int j=0;j<=r;j++)
      chmin(ans,dp[b][t][j]);
  if(ans==INF) ans=-1;
  
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4828
final_rank: 11

code_snippet: #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int>P;
typedef pair<P,P>P2;

int dp[111][111][1<<5];
int n,m,e,s,t,r;

vector<P>v[100000];

signed main(){
  r(i,111)r(j,1<<5)r(k,111)dp[i][k][j]=1e9;
  cin>>n>>m>>e>>s>>t>>r;

  r(i,m){
    int a,b;
    cin>>a>>b;
    v[a].push_back(P(b,-1));
    v[b].push_back(P(a,-1));
  }

  map<int,int>M;
  int state=0;

  r(i,e){
    int a,b,c;
    cin>>a>>b>>c;
    v[a].push_back(P(b,i));
    v[b].push_back(P(a,i));

    M[c] = i;
    if(c==s)state|=(1<<i);
  }

  priority_queue<P2,vector<P2>,greater<P2> > q;

  q.push(P2(P(0,s),P(r,state))); /// warning

  dp[s][r][state]=0;

  while(!q.empty()){
    P2 PP=q.top();q.pop();

    int now=PP.first.second;
    int cost=PP.first.first;
    int R = PP.second.first;
    int S = PP.second.second;

    // cout<<cost<<' '<<now<<' '<<R<<' '<<S<<endl;

    if(dp[now][R][S] < cost)continue;

    // cout<<cost<<' '<<now<<' '<<R<<' '<<S<<endl;

    if(R){
      r(i,v[now].size()){
	int nex = v[now][i].first;
	int flag = v[now][i].second;

	int f=flag%100;
	int nR=R-1;

	int nS = S;
	if(M.count(nex))nS= (nS|(1<<M[nex]));

	if(flag == -1){

	  if(dp[nex][nR][nS] <= cost+1)continue;
	  dp[nex][nR][nS] = cost+1;
	  q.push(P2(P(cost+1,nex),P(nR,nS)));
	}
	else{
	  if(!(S&(1<<f)))continue;

	  if(dp[nex][nR][nS] <= cost+1)continue;
	  dp[nex][nR][nS] = cost+1;
	  q.push(P2(P(cost+1,nex),P(nR,nS)));
	}
      }
    }

    if(dp[s][r][S] <= cost+1)continue;
    dp[s][r][S] = cost+1;
    q.push(P2(P(cost+1,s),P(r,S)));
  }


  int ans= 1e9;

  r(i,101)r(j,(1<<5)){
    ans=min(ans,dp[t][i][j]);
  }

  if(ans==1e9)cout<<-1<<endl;
  else cout<<ans<<endl;

  
  
  
}
accuracy: 1
time_ms: 70
memory_kb: 7096
final_rank: 16

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

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b)a=b;};
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b)a=b;};

const Int INF = 1e15;
Int dp[1<<4][111][111];

signed main(){
  Int n,m,e,s,t,r;
  cin>>n>>m>>e>>s>>t>>r;
  vector<vector<Int>> G(n),H(n);
  for(Int i=0;i<m;i++){
    Int a,b;
    cin>>a>>b;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }

  vector<Int> as(e),bs(e),cs(e);
  for(Int i=0;i<e;i++) cin>>as[i]>>bs[i]>>cs[i];

  for(Int b=0;b<16;b++)
    for(Int i=0;i<111;i++)
      for(Int j=0;j<111;j++)
	dp[b][i][j]=INF;

  using T = tuple<Int, Int, Int>;
  deque<T> que;
  dp[0][s][0]=0;
  que.emplace_back(0,s,0);

  while(!que.empty()){
    Int b,v,d;
    tie(b,v,d)=que.front();que.pop_front();


    // event
    for(Int i=0;i<e;i++){
      if(v==cs[i]){
	Int nb=b|(1<<i);
	Int nv=v;
	Int nd=d;	
	if(dp[nb][nv][nd]>dp[b][v][d]+0){
	  dp[nb][nv][nd]=dp[b][v][d]+0;
	  que.emplace_front(nb,nv,nd);
	}
      }
    }

    // okarina
    {
      Int nb=b;
      Int nv=s;
      Int nd=0;      
      if(dp[nb][nv][nd]>dp[b][v][d]+1){
	dp[nb][nv][nd]=dp[b][v][d]+1;
	que.emplace_back(nb,nv,nd);
      }
    }
    
    if(d==r) continue;
    
    // normal route
    for(Int u:G[v]){
      Int nb=b;
      Int nv=u;
      Int nd=d+1;      
      if(dp[nb][nv][nd]>dp[b][v][d]+1){
	dp[nb][nv][nd]=dp[b][v][d]+1;
	que.emplace_back(nb,nv,nd);
      }
    }

    // event route
    for(Int i=0;i<e;i++){
      if((~b>>i)&1) continue;
      if(bs[i]==v) swap(as[i],bs[i]);
      if(as[i]!=v) continue;
      // a -> b
      Int nb=b;
      Int nv=bs[i];
      Int nd=d+1;      
      if(dp[nb][nv][nd]>dp[b][v][d]+1){
	dp[nb][nv][nd]=dp[b][v][d]+1;
	que.emplace_back(nb,nv,nd);
      }
    }
  }  
  
  Int ans=INF;  
  for(Int b=0;b<16;b++)
    for(Int j=0;j<=r;j++)
      chmin(ans,dp[b][t][j]);
  if(ans==INF) ans=-1;
  
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4752
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int,P> P3;
const ll MOD = ll(1e9+7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()

int N, M, E, S, T, R;
int g[105][105];
int d[105][105], ans = IINF;
vector<P3> es;

void warshall_floyd(){
    REP(i,N) fill(d[i], d[i]+N, IINF/3);
    REP(i,N)REP(j,N) d[i][j] = g[i][j];
    REP(k,N)REP(i,N)REP(j,N){
        d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
    }
}

int main() {
    cin >> N >> M >> E >> S >> T >> R;
    REP(i,N) fill(g[i], g[i]+N, IINF/3);
    REP(i,M){
        int a, b;
        cin >> a >> b;
        g[a][b] = 1;
        g[b][a] = 1;
    }
    REP(i,E){
        int a, b, c;
        cin >> a >> b >> c;
        if(c!=S)es.push_back({c,{a,b}});
        else{
            g[a][b] = 1;
            g[b][a] = 1;
        }
    }
    E = es.size();
    vector<int> v;
    REP(i,E) v.push_back(i);

    while(true){
        REP(i,E){
            int a, b;
            a = es[i].second.first;
            b = es[i].second.second;
            g[a][b] = IINF/3;
            g[b][a] = IINF/3;
        }
        int sum = 0, tmp = 0, pre = S;
        for(int i=0; i<=E; i++){
            warshall_floyd();
            if(tmp+d[pre][T] <= R) ans = min(ans, sum+d[pre][T]);
            if(tmp+d[S][T] <= R) ans = min(ans, sum+1+d[S][T]);
            if(i==E)break;
            int a, b, c;
            a = es[v[i]].second.first;
            b = es[v[i]].second.second;
            c = es[v[i]].first;
            if(tmp+d[pre][c] <= R && d[pre][c] < d[S][c]+1){
                tmp += d[pre][c];
                sum += d[pre][c];
                pre = c;
                g[a][b] = 1;
                g[b][a] = 1;
            }
            else if(pre != S){
                pre = S;
                sum++;
                tmp = 0;
                i--;
            }
            else break;
        }
        

        if(!next_permutation(ALL(v)))break;
    }

    cout << (ans<=IINF/3?ans:-1) << endl;

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3144
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const int INF = 10101010;

const int E = 1<<4;
const int N = 100;
const int R = 101;

int d[E][N][R];
int event[N];
vector<int> G[E][N];

struct state{
    int mask, pos, day;
};

int main(){
    int n,m,e,s,t,r;
    cin >>n >>m >>e >>s >>t >>r;

    rep(i,m){
        int a,b;
        scanf(" %d %d", &a, &b);
        rep(j,1<<e){
            G[j][a].pb(b);
            G[j][b].pb(a);
        }
    }

    int smask = 0;
    fill(event, event+N, -1);
    rep(i,e){
        int a,b,c;
        scanf(" %d %d %d", &a, &b, &c);
        event[c] = i;
        if(c==s) smask = 1<<i;

        rep(j,1<<e)if(j>>i&1){
            G[j][a].pb(b);
            G[j][b].pb(a);
        }
    }

    rep(i,E)rep(j,N)rep(k,R) d[i][j][k] = INF;
    d[smask][s][0] = 0;
    queue<state> que;
    que.push({smask,s,0});

    while(!que.empty()){
        state c = que.front();
        que.pop();

        int D = d[c.mask][c.pos][c.day];

        // move
        if(c.day<r){
            int nd = c.day+1;
            for(int nx:G[c.mask][c.pos]){
                int nmask = c.mask;
                if(event[nx]!=-1) nmask |= (1<<event[nx]);

                if(d[nmask][nx][nd] > D+1){
                    d[nmask][nx][nd] = D+1;
                    que.push({nmask,nx,nd});
                }
            }
        }

        // ocarina
        if(d[c.mask][s][0] > D+1){
            d[c.mask][s][0] = D+1;
            que.push({c.mask,s,0});
        }
    }

    int ans = INF;
    rep(mask,1<<e)rep(i,r+1) ans = min(ans,d[mask][t][i]);
    if(ans == INF) ans = -1;
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4192
final_rank: 9

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
 
#define INF (1<<26)
struct edge{int to,cost,key;};
struct state{
  int pos,pos2,S,cost;  
  bool operator < (const state &p)const{    return cost > p.cost;  }
};
 
vector<edge> g[200];
int N,M,E,Si,Ti,R;
int c[200];
vector<int> nodes;
int dp[105][105];
int d[105][205][300];
 
void solve(){
  for(int i=0;i<105;i++)
    for(int j=0;j<205;j++)
      for(int k=0;k<300;k++)
    d[i][j][k]=INF;
 
  priority_queue<state> Q;
  int a=0;
  if(c[Si]!=-1)a=(1<<c[Si]);
  Q.push((state){Si,0,a,0});
  d[Si][0][a]=0;
 
  while(!Q.empty()){
    state s=Q.top();Q.pop();
    if(d[s.pos][s.pos2][s.S]<s.cost)continue;
    if(s.pos==Ti){cout<<s.cost<<endl;return;}
    if(s.pos2>R)continue;
    if(d[Si][0][s.S]>s.cost+1){
      d[Si][0][s.S]=s.cost+1;
      Q.push((state){Si,0,s.S,s.cost+1});
    }
 
    for(int i=0;i<(int)nodes.size();i++){      
      int to=nodes[i];
      if(dp[s.pos][to]==INF)continue;
      int ncost=s.cost+dp[s.pos][to];
      int nS=s.S;
      int npos2=s.pos+dp[s.pos][to];
      if(c[to]!=-1)nS|=(1<<c[to]);
      if(d[to][npos2][nS]<=ncost)continue;
      d[to][npos2][nS]=ncost;
      Q.push((state){to,npos2,nS,ncost});
    }
     
    for(int i=0;i<(int)g[s.pos].size();i++){
      edge e=g[s.pos][i];
      if(!((s.S>>e.key)&1))continue;
      int to=e.to;
      int ncost=s.cost+e.cost;
      int nS=s.S;
      int npos2=s.pos+e.cost;
      if(c[to]!=-1)nS|=(1<<c[to]);
      if(d[to][npos2][nS]<=ncost)continue;
      d[to][npos2][nS]=ncost;
      Q.push((state){to,npos2,nS,ncost});
    }
  }
  cout<<-1<<endl;
}
 
int main(){
  for(int i=0;i<105;i++){
    c[i]=-1;
    for(int j=0;j<105;j++)dp[i][j]=(i==j?0:INF);
  }
 
  cin>>N>>M>>E>>Si>>Ti>>R;
  int x,y,z;
  for(int i=0;i<M;i++){
    cin>>x>>y;
    dp[x][y]=dp[y][x]=min(dp[x][y],1);
  }
 
  for(int i=0;i<E;i++){
    cin>>x>>y>>z;
    g[x].push_back((edge){y,1,i});
    g[y].push_back((edge){x,1,i});
    c[z]=i;
    nodes.push_back(x);
    nodes.push_back(y);
    nodes.push_back(z);
  }
  nodes.push_back(Si);  nodes.push_back(Ti);
  for(int k=0;k<N;k++)
    for(int i=0;i<N;i++)
      for(int j=0;j<N;j++)
    dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
 
  solve();
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 28412
final_rank: 19

code_snippet: #include <map>
#include <queue>
#include <iostream>
using namespace std;
int N, M, E, S, T, R, a, b, c, f[111]; vector<int> G[6][100];
void add_edge(int r, int a, int b) {
	G[r][a].push_back(b);
	G[r][b].push_back(a);
}
struct State {
	int pos, day, bit;
	State(int pos_, int day_, int bit_) : pos(pos_), day(day_), bit(bit_) {};
};
bool operator<(const State& s1, const State& s2) {
	return s1.pos * ((R + 1) << E) + (s1.day << E) + s1.bit < s2.pos * ((R + 1) << E) + (s2.day << E) + s2.bit;
}
int main() {
	cin >> N >> M >> E >> S >> T >> R;
	for (int i = 0; i < M; i++) {
		cin >> a >> b;
		add_edge(0, a, b);
	}
	fill(f, f + N, -1);
	for (int i = 0; i < E; i++) {
		cin >> a >> b >> c;
		add_edge(i + 1, a, b); f[c] = i;
	}
	State ss = State(S, 0, 0);
	if (f[S] != -1) ss.bit |= (1 << f[S]);
	map<State, int> d; d[ss] = 1;
	queue<State> que; que.push(ss);
	while (!que.empty()) {
		State val = que.front(); que.pop();
		int v = val.pos, r = val.day, bit = val.bit;
		if (r < R) {
			for (int i = 0; i < E + 1; i++) {
				if (i && !(bit & (1 << (i - 1)))) continue;
				for (auto &e : G[i][v]) {
					State z(e, r + 1, bit);
					if (f[e] != -1) z.bit |= (1 << f[e]);
					if (!d[z]) {
						d[z] = d[val] + 1;
						que.push(z);
					}
				}
			}
		}
		State y(S, 0, bit);
		if (!d[y]) {
			d[y] = d[val] + 1;
			que.push(y);
		}
	}
	int ret = 999999999;
	for (int i = 0; i <= R; i++) {
		for (int j = 0; j < (1 << E); j++) {
			State z(T, i, j);
			if (d[z]) ret = min(ret, d[z] - 1);
		}
	}
	if (ret == 999999999) cout << -1 << endl;
	else cout << ret << endl;
	return 0;
}
accuracy: 1
time_ms: 900
memory_kb: 9564
final_rank: 20

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

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int N, M, E, S, T, R;
    vector< vector<int> > G;
    vector<int> events;
    void input() {
        cin >> N >> M >> E >> S >> T >> R;
        G.clear(); G.resize(N, vector<int>(N, -1));
        for (int i = 0; i < M; i++) {
            int a, b; cin >> a >> b;
            G[a][b] = G[b][a] = 0;
        }
        events.clear(); events.resize(N, 0);
        for (int i = 0; i < E; i++) {
            int a, b, c; cin >> a >> b >> c;
            events[c] = (1 << i);
            G[a][b] = G[b][a] = (1 << i);
        }
    }

    const int MAX_E = 4;
    const int MAX_R = 100;
    const int MAX_N = 100;
    const int INF = 1<<28;

    struct State {
        int e, r, v, t;
        State(int e, int r, int v, int t) :
            e(e), r(r), v(v), t(t) {}
    };

    void solve() {
        static int D[1 << MAX_E][MAX_R + 1][MAX_N];
        for (int i = 0; i < (1 << MAX_E); i++) for (int j = 0; j <= MAX_R; j++) for (int k = 0; k < MAX_N; k++) {
            D[i][j][k] = INF;
        }
        D[0][0][S] = 0;
        queue<State> Q;
        Q.push(State(events[S], 0, S, 0));
        while (not Q.empty()) {
            State s = Q.front(); Q.pop();
            if (s.r < R) {
                for (int i = 0; i < N; i++) {
                    int x = G[s.v][i];
                    if (x < 0) continue;
                    if (x == 0 || (x & s.e)) {
                        int ne = s.e;
                        int nr = s.r + 1;
                        int nv = i;
                        int nt = s.t + 1;
                        if (events[i] > 0) ne |= events[i];
                        if (D[ne][nr][nv] > nt) {
                            D[ne][nr][nv] = nt;
                            Q.push(State(ne, nr, nv, nt));
                        }
                    }
                }
            }
            if (D[s.e][0][S] > s.t + 1) {
                D[s.e][0][S] = s.t + 1;
                Q.push(State(s.e, 0, S, s.t + 1));
            }
        }
        int ans = INF;
        for (int i = 0; i < (1 << MAX_E); i++) {
            for (int j = 0; j <= R; j++) {
                ans = min(ans, D[i][j][T]);
            }
        }
        cout << (ans == INF ? -1 : ans) << endl;
    }
}

int main() {
    input(); solve();
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3820
final_rank: 5

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

struct edge{
    int to,id;
    edge(int to,int id):to(to),id(id){}
};

struct data{
    int pos,day,state;
    data(int pos,int day,int state)
        :pos(pos),day(day),state(state){}
};
const int INF=1001001001;
int N,M,E,S,T,R;
vector<edge>G[100];
int event[100];
int dist[100][101][1<<4];
int main(){
    cin>>N>>M>>E>>S>>T>>R;
    for(int i=0;i<M;i++){
        int a,b;cin>>a>>b;
        G[a].push_back(edge(b,-1));
        G[b].push_back(edge(a,-1));
    }

    memset(event,-1,sizeof(event));
    for(int i=0;i<E;i++){
        int a,b,c;
        cin>>a>>b>>c;
        G[a].push_back(edge(b,i));
        G[b].push_back(edge(a,i));
        event[c]=i;
    }

    queue<data>que;
    fill_n(**dist,100*101*(1<<4),INF);
    que.push(data(S,0,(~event[S])?(1<<event[S]):0));
    dist[S][0][(~event[S])?(1<<event[S]):0]=0;


    while(que.size()){
        data d=que.front();que.pop();

        if(d.day<R){
            for(int i=0;i<G[d.pos].size();i++){
                edge &e=G[d.pos][i];
                if((~e.id)&&(d.state>>e.id&1)==0)continue;
                int nstate=d.state;
                if(~event[e.to])nstate|=1<<event[e.to];
                if(dist[e.to][d.day+1][nstate]<=dist[d.pos][d.day][d.state]+1)continue;
                dist[e.to][d.day+1][nstate]=dist[d.pos][d.day][d.state]+1;
                que.push(data(e.to,d.day+1,nstate));
            }
        }
        if(dist[S][0][d.state]>dist[d.pos][d.day][d.state]+1){
            dist[S][0][d.state]=dist[d.pos][d.day][d.state]+1;
            que.push(data(S,0,d.state));
        }
    }
    int mi=INF;
    for(int i=0;i<=R;i++){
        for(int j=0;j<(1<<E);j++){
            mi=min(mi,dist[T][i][j]);
        }
    }
    if(mi==INF)cout<<-1<<endl;
    else cout<<mi<<endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1976
final_rank: 2

code_snippet: #include<stdio.h>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
int g[16][110][110];
int G[110][110];
int v[7];
int s[5];
int t[5];
int u[5];
int dp[5][110][16][7];
int main(){
	int a,b,c,d,e,f;
	scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
	for(int i=0;i<a;i++)for(int j=0;j<a;j++){
		if(i!=j)G[i][j]=99999999;
	}
	for(int i=0;i<b;i++){
		int p,q;scanf("%d%d",&p,&q);
		G[p][q]=G[q][p]=1;
	}
	
	for(int i=0;i<c;i++){
		scanf("%d%d%d",s+i,t+i,u+i);
	}
	for(int i=0;i<(1<<c);i++){
		for(int j=0;j<a;j++)for(int k=0;k<a;k++){
			g[i][j][k]=G[j][k];
		}
		for(int j=0;j<c;j++)if(i&(1<<j)){
			g[i][s[j]][t[j]]=g[i][t[j]][s[j]]=1;
		}
		for(int l=0;l<a;l++)for(int j=0;j<a;j++)for(int k=0;k<a;k++)
			g[i][j][k]=min(g[i][j][k],g[i][j][l]+g[i][l][k]);
	}
	for(int i=0;i<c;i++)v[i]=u[i];
	v[c]=d;v[c+1]=e;
	int n=c+2;
	for(int i=0;i<5;i++)for(int j=0;j<110;j++)for(int k=0;k<16;k++)for(int l=0;l<7;l++)
		dp[i][j][k][l]=999999999;
	int st=0;
	for(int i=0;i<c;i++)if(v[i]==d)st+=(1<<i);
	dp[0][0][st][c]=0;
	for(int i=0;i<=4;i++){
		for(int j=0;j<=f;j++){
			for(int k=0;k<(1<<c);k++){
				for(int l=0;l<n;l++){
					if(dp[i][j][k][l]>99999999)continue;
					if(i<4){
						dp[i+1][0][k][c]=min(dp[i+1][0][k][c],dp[i][j][k][l]+1);
					}
					for(int m=0;m<n;m++){
						if(j+g[k][v[l]][v[m]]<=f){
							int to=k;
							if(m<c)to|=(1<<m);
							dp[i][j+g[k][v[l]][v[m]]][to][m]=min(dp[i][j+g[k][v[l]][v[m]]][to][m],dp[i][j][k][l]+g[k][v[l]][v[m]]);
						}
					}
				}
			}
		}
	}
	int ret=999999999;
	for(int i=0;i<5;i++)for(int j=0;j<=f;j++)for(int k=0;k<(1<<c);k++)for(int l=0;l<n;l++){
		if(v[l]==e){
		//	printf("%d %d %d %d: %d\n",i,j,k,l,dp[i][j][k][l]);
			ret=min(ret,dp[i][j][k][l]);
		}
	}
	if(ret>99999999)ret=-1;
	printf("%d\n",ret);
}
accuracy: 1
time_ms: 10
memory_kb: 1940
final_rank: 1

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

#define for_(i,a,b) for(int i=(a);i<(b);++i)

struct Edge { int to, en; };

struct State {
	int v, day, dst, event;
	bool operator > (const State& a) const { return dst > a.dst; }
};

int N, M, E, S, T, R, eve[111];
vector< Edge > edges[111];

const int iINF = 1L << 30;

int dst[111][111][16];

void solve() {
	for_(i,0,111) for_(j,0,111) for_(k,0,16) dst[i][j][k] = iINF;
	
	priority_queue< State, vector< State >, greater< State > > que;
	int ini_eve = (eve[S] >= 0 ? 1 << eve[S] : 0);
	que.push(State{S, 0, 0, ini_eve});
	dst[S][0][ini_eve] = 0;
	
	while (!que.empty()) {
		State s = que.top(); que.pop();
		if (s.day > R || dst[s.v][s.day][s.event] < s.dst) continue;
		
		if (dst[S][0][s.event] > s.dst + 1) {
			dst[S][0][s.event] = s.dst + 1;
			que.push(State{S, 0, s.dst + 1, s.event});
		}
		
		for (Edge e : edges[s.v]) {
			if (!(e.en == -1 || (e.en >= 0 && (s.event >> e.en & 1)))) continue;
			
			int nx_eve = s.event;
			if (eve[e.to] >= 0) nx_eve |= 1 << eve[e.to];
			
			if (dst[e.to][s.day + 1][nx_eve] > s.dst + 1) {
				dst[e.to][s.day + 1][nx_eve] = s.dst + 1;
				que.push(State{e.to, s.day + 1, s.dst + 1, nx_eve});
			}
		}
	}
	
	int ans = iINF;
	
	for_(r,0,R+1) for_(ev,0,1<<E) {
		ans = min(ans, dst[T][r][ev]);
	}
	
	cout << (ans == iINF ? -1 : ans) << endl;
}

int main() {
	cin >> N >> M >> E >> S >> T >> R;
	
	for_(i,0,M) {
		int a, b;
		cin >> a >> b;
		edges[a].push_back(Edge{b, -1});
		edges[b].push_back(Edge{a, -1});
	}
	
	memset(eve, -1, sizeof(eve));
	
	for_(i,0,E) {
		int a, b, c;
		cin >> a >> b >> c;
		edges[a].push_back(Edge{b, i});
		edges[b].push_back(Edge{a, i});
		eve[c] = i;
	}
	
	solve();
}
accuracy: 1
time_ms: 30
memory_kb: 2196
final_rank: 3