Problem:
ヘビの JOI 君 (Snake JOI)
問題
ヘビの JOI 君は，ある大きな屋敷に迷い込んでしまった．屋敷の住人に見つかる前に，屋敷を脱出しなければならない．
この屋敷には部屋が N 個あり，1, 2, ..., N の番号が付けられている．また，廊下が M 本あり，i 本目の廊下 (1 ≦ i ≦ M) は部屋 A
i
と部屋 B
i
を結んでいる．JOI 君はこれらの廊下をどちらの向きにも通ることができ，廊下 i を通るのには D
i
分かかる．部屋と部屋の間を廊下を通る以外の手段で移動する方法はない．
この屋敷の部屋の温度はそれぞれ一定に調節されており，JOI 君にとって寒すぎるか，快適であるか，暑すぎるかである．JOI 君は，急な温度変化に対応できないため，最後に寒すぎる部屋を出てから X 分未満のうちに暑すぎる部屋に入ることはできない．同様に，最後に暑すぎる部屋を出てから X 分未満のうちに寒すぎる部屋に入ることもできない．
JOI 君は，移動中に部屋に入るとすぐに部屋から出なければならない．また，廊下の途中で引き返したり，廊下 i を D
i
分より長い時間かけて通ることもできない．ただし，一度訪れた部屋にもう一度入ることや，一度使った廊下をもう一度使うことは許される．
JOI 君は現在部屋 1 にいる．この部屋は JOI 君にとって寒すぎる．JOI 君は屋敷の出口のある部屋 N に入ると，屋敷から脱出できる．
JOI 君が屋敷から脱出するのにかかる最短の時間を求めよ．
入力
入力は 1 + N + M 行からなる．
1 行目には，3 個の整数 N, M, X (2 ≦ N ≦ 10000, 1 ≦ M ≦ 20000, 1 ≦ X ≦ 200) が空白を区切りとして書かれている．これは，屋敷に N 個の部屋と M 本の廊下があり，JOI 君が温度変化に対応するのに X 分かかることを表す．
続く N 行のうちの i 行目 (1 ≦ i ≦ N) には，部屋 i の温度を表す整数 T
i
(0 ≦ T
i
≦ 2) が書かれている．JOI 君にとって部屋 i は，T
i
= 0 のとき寒すぎ，T
i
= 1 のとき快適であり，T
i
= 2 のとき暑すぎる．T
1
= 0 であることが保証されている．
続く M 行のうちの j 行目 (1 ≦ j ≦ M) には，3 個の整数 A
j
, B
j
, D
j
(1 ≦ A
j
< B
j
≦ N, 1 ≦ D
j
≦ 200) が空白を区切りとして書かれている．これは，廊下 j が部屋 A
j
と部屋 B
j
を結んでおり，通るのに D
j
分かかることを表す．同じ部屋の組を結ぶ廊下が複数ある可能性があることに注意せよ．
与えられる入力データでは，JOI 君が屋敷から脱出できることは保証されている．
出力
JOI 君が屋敷から脱出するのに最短で何分かかるかを表す整数を 1 行で出力せよ．
入出力例
入力例 1
8 10 4
0
1
1
2
1
1
2
0
1 2 1
1 3 1
2 3 3
2 4 5
3 4 1
4 5 1
5 6 1
5 8 1
1 7 2
7 8 2
出力例 1
9
入力例 2
15 25 4
0
1
1
0
2
1
0
1
1
2
0
0
1
0
1
8 11 1
7 10 1
12 14 1
3 8 1
1 5 1
3 9 1
3 8 1
1 5 1
6 15 1
11 12 1
2 14 1
7 10 1
11 12 1
5 13 1
2 8 1
1 4 1
2 11 1
5 6 1
1 13 1
6 12 1
5 10 1
9 13 1
4 10 1
3 12 1
7 13 1
出力例 2
6
入力例 1 では，部屋を 1 → 2 → 3 → 4 → 5 → 6 → 5 → 8 の順に移動するのが最短となる．
入力例 2 では，いくつかの部屋の組 (たとえば部屋 1 と部屋 5) を結ぶ廊下が複数ある．
情報オリンピック日本委員会作 『第 16 回日本情報オリンピック JOI 2016/2017 予選競技課題』

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ul=unsigned long long;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
using Graph=vector<vector<int>>;
vector<vector<ll>> Dijkstra(vector<vector<vector<tuple<int,int,ll>>>> &G, int s,int X){
    vector<vector<ll>> D(G.size(),vector<ll>(2*X+2,1e18));
    D[s][2*X+1] = 0;
    priority_queue<tuple<ll,int,int>, vector<tuple<ll,int,int>>, greater<tuple<ll,int,int>>> q;
    q.push(make_tuple(0, s, 2*X+1));
    while (!q.empty()){
        auto [d, u, x] = q.top();
        q.pop();
        if (D[u][x] < d){
            continue;
        }
        for (auto [v, w, c] : G[u][x]){
            d = D[u][x] + c;
            if (d < D[v][w]){
                D[v][w] = d;
                q.push(make_tuple(d, v, w));
            }
        }
    }
    return D;
}
int main(){
	int N,M,X;
	cin>>N>>M>>X;
	vector<vector<vector<tuple<int,int,ll>>>>G(N,vector<vector<tuple<int,int,ll>>>(2*X+2));
	vector<int>T(N);
	for(int i=0;i<N;i++)cin>>T[i];
	for(int i=0;i<N;i++){
		if(T[i]==0)G[i][2*X+1].push_back({i,0,0});
		if(T[i]==2)G[i][2*X+1].push_back({i,2*X,0});
	}
	for(int i=0;i<M;i++){
		int a,b,d;
		cin>>a>>b>>d;
		a--,b--;
		if(T[a]==0&&T[b]==0){
			for(int j=0;j<X;j++){
				G[a][j].push_back({b,2*X+1,d});
				G[b][j].push_back({a,2*X+1,d});
			}
		}
		if(T[a]==0&&T[b]==1){
			for(int j=0;j<=X;j++){
				if(j+d>=X)G[a][j].push_back({b,2*X+1,d});
				else G[a][j].push_back({b,j+d,d});
			}
		}
		if(T[a]==0&&T[b]==2){
			for(int j=0;j<=X;j++){
				if(j+d>=X)G[a][j].push_back({b,2*X+1,d});
			}
		}
		if(T[a]==1&&T[b]==0){
			for(int j=0;j<=X;j++){
				G[a][j].push_back({b,2*X+1,d});
			}
			G[a][2*X+1].push_back({b,2*X+1,d});
			for(int j=X;j<=2*X;j++){
				if(j-d<=X)G[a][j].push_back({b,2*X+1,d});
			}
		}
		if(T[a]==1&&T[b]==1){
			for(int j=0;j<=X;j++){
				if(j+d>=X)G[a][j].push_back({b,2*X+1,d});
				else G[a][j].push_back({b,j+d,d});
				if(j+d>=X)G[b][j].push_back({a,2*X+1,d});
				else G[b][j].push_back({a,j+d,d});
			}
			G[a][2*X+1].push_back({b,2*X+1,d});
			G[b][2*X+1].push_back({a,2*X+1,d});
			for(int j=X;j<=2*X;j++){
				if(j-d<=X)G[a][j].push_back({b,2*X+1,d});
				else G[a][j].push_back({b,j-d,d});
				if(j-d<=X)G[b][j].push_back({a,max(X,j-d),d});
				else G[b][j].push_back({a,j-d,d});
			}
		}
		if(T[a]==1&&T[b]==2){
			for(int j=0;j<=X;j++){
				if(j+d>=X)G[a][j].push_back({b,2*X+1,d});
			}
			G[a][2*X+1].push_back({b,2*X+1,d});
			for(int j=X;j<=2*X;j++){
				G[a][j].push_back({b,2*X+1,d});
			}
		}
		if(T[a]==2&&T[b]==0){
			for(int j=X;j<=2*X;j++){
				if(j-d<=X)G[a][j].push_back({b,2*X+1,d});
			}
		}
		if(T[a]==2&&T[b]==1){
			for(int j=X;j<=2*X;j++){
				if(j-d<=X)G[a][j].push_back({b,2*X+1,d});
				else G[a][j].push_back({b,j-d,d});
			}
		}
		if(T[a]==2&&T[b]==2){
			for(int j=X+1;j<=2*X;j++){
				G[a][j].push_back({b,2*X+1,d});
				G[b][j].push_back({a,2*X+1,d});
			}
		}
		swap(a,b);
		if(T[a]==0&&T[b]==0){
			for(int j=0;j<X;j++){
				G[a][j].push_back({b,2*X+1,d});
				G[b][j].push_back({a,2*X+1,d});
			}
		}
		if(T[a]==0&&T[b]==1){
			for(int j=0;j<=X;j++){
				if(j+d>=X)G[a][j].push_back({b,2*X+1,d});
				else G[a][j].push_back({b,j+d,d});
			}
		}
		if(T[a]==0&&T[b]==2){
			for(int j=0;j<=X;j++){
				if(j+d>=X)G[a][j].push_back({b,2*X+1,d});
			}
		}
		if(T[a]==1&&T[b]==0){
			for(int j=0;j<=X;j++){
				G[a][j].push_back({b,2*X+1,d});
			}
			G[a][2*X+1].push_back({b,2*X+1,d});
			for(int j=X;j<=2*X;j++){
				if(j-d<=X)G[a][j].push_back({b,2*X+1,d});
			}
		}
		if(T[a]==1&&T[b]==1){
			for(int j=0;j<=X;j++){
				if(j+d>=X)G[a][j].push_back({b,2*X+1,d});
				else G[a][j].push_back({b,j+d,d});
				if(j+d>=X)G[b][j].push_back({a,2*X+1,d});
				else G[b][j].push_back({a,j+d,d});
			}
			G[a][2*X+1].push_back({b,2*X+1,d});
			G[b][2*X+1].push_back({a,2*X+1,d});
			for(int j=X;j<=2*X;j++){
				if(j-d<=X)G[a][j].push_back({b,2*X+1,d});
				else G[a][j].push_back({b,j-d,d});
				if(j-d<=X)G[b][j].push_back({a,max(X,j-d),d});
				else G[b][j].push_back({a,j-d,d});
			}
		}
		if(T[a]==1&&T[b]==2){
			for(int j=0;j<=X;j++){
				if(j+d>=X)G[a][j].push_back({b,2*X+1,d});
			}
			G[a][2*X+1].push_back({b,2*X+1,d});
			for(int j=X;j<=2*X;j++){
				G[a][j].push_back({b,2*X+1,d});
			}
		}
		if(T[a]==2&&T[b]==0){
			for(int j=X;j<=2*X;j++){
				if(j-d<=X)G[a][j].push_back({b,2*X+1,d});
			}
		}
		if(T[a]==2&&T[b]==1){
			for(int j=X;j<=2*X;j++){
				if(j-d<=X)G[a][j].push_back({b,2*X+1,d});
				else G[a][j].push_back({b,j-d,d});
			}
		}
		if(T[a]==2&&T[b]==2){
			for(int j=X+1;j<=2*X;j++){
				G[a][j].push_back({b,2*X+1,d});
				G[b][j].push_back({a,2*X+1,d});
			}
		}
	}
	auto D=Dijkstra(G,0,X);
	cout<<*min_element(D[N-1].begin(),D[N-1].end())<<endl;
}
accuracy: 1
time_ms: 620
memory_kb: 578944
final_rank: 20

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

using P = pair<int, int>;

struct edge {
    int to, weight;
};

using edges = vector<edge>;
using graph = vector<edges>;

constexpr int INF = 1e9;

int main() {
    int N, M, X;
    cin >> N >> M >> X;
    vector<int> T(N);
    for(int i=0; i<N; ++i) {
        cin >> T[i];
    }
    graph g(N);
    for(int i=0; i<M; ++i) {
        int a, b, d;
        cin >> a >> b >> d;
        a--; b--;
        g[a].push_back((edge){b, d});
        g[b].push_back((edge){a, d});
    }
    
    using state = tuple<int, int, int, int>;
    vector<vector<vector<int>>> d(N, vector<vector<int>>(3, vector<int>(X+1, INF)));
    d[0][0][X] = 0;
    priority_queue<state, vector<state>, greater<state>> que;
    que.push(make_tuple(0, 0, 0, X));
    while(!que.empty()) {
        int cd, v, t, x;
        tie(cd, v, t, x) = que.top();
        que.pop();
        if(d[v][t][x] < cd) {
            continue;
        }
        for(auto& e : g[v]) {
            int nx = (T[e.to] == 1 ? max(0, x - e.weight) : X);
            if(T[e.to] == 1 && d[e.to][t][nx] > d[v][t][x] + e.weight) {
                d[e.to][t][nx] = d[v][t][x] + e.weight;
                que.push(make_tuple(d[e.to][t][nx], e.to, t, nx));
            } else if(T[e.to] != 1 && (T[e.to] != t && x <= e.weight || T[e.to] == t)) {
                if(d[e.to][T[e.to]][nx] > d[v][t][x] + e.weight) {
                    d[e.to][T[e.to]][nx] = d[v][t][x] + e.weight;
                    que.push(make_tuple(d[e.to][T[e.to]][nx], e.to, T[e.to], nx));
                }
            }
        }
    }
    int res = INF;
    for(int i=0; i<3; ++i) {
        for(int j=0; j<X+1; ++j) {
            res = min(res, d[N-1][i][j]);
        }
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 29016
final_rank: 10

code_snippet: #include <iostream>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;

struct E{
	int to,d;
};
struct E2{
	int to,state,d;
	long long int time;
	bool operator<(const E2& e2)const{
		return time>e2.time;
	}
};


long long int dp[4][203][10003];
int room[10003];
vector<E> cons[10003];
int n,m,x;
int main() {
	memset(dp,-1,sizeof(dp));
	cin>>n>>m>>x;
	for(int i=1;i<=n;i++){
		cin>>room[i];
	}
	for(int i=0;i<m;i++){
		int from,to;
		E e1;
		cin>>from>>to>>e1.d;
		e1.to=to;
		cons[from].push_back(e1);
		e1.to=from;
		cons[to].push_back(e1);
	}
	E2 e2;
	e2.to=1;
	e2.state=room[1];
	e2.time=0;
	e2.d=x;
	priority_queue<E2> pq;
	pq.push(e2);
	while(pq.size()>0){
		E2 e2=pq.top();
		//cout<<"("<<e2.time<<" "<<e2.state<<" "<<e2.to<<" "<<e2.d<<")"<<endl;
		pq.pop();
		long long int t1=dp[e2.state][e2.d][e2.to];
		if(t1!=-1 && t1<=e2.time)continue;
		dp[e2.state][e2.d][e2.to]=e2.time;
		for(auto it=cons[e2.to].begin();it!=cons[e2.to].end();it++){
			E e1a=(*it);
			
			E2 e2a;
			e2a.to=e1a.to;
			e2a.state=e2.state;
			e2a.time=e2.time+e1a.d;
			e2a.d=e2.d-e1a.d;
			if(e2a.d<=0)e2a.d=0;
			bool moveok=false;
			if(e2a.d<=0){
				moveok=true;
				e2a.state=room[e1a.to];
				if(room[e1a.to]==1){
					e2a.d=0;
				
				}else{
					e2a.d=x;
				}
			}else{
				
				if(room[e1a.to]==1){
					moveok=true;
				}else if(room[e1a.to]==e2a.state){
					moveok=true;
					e2a.d=x;
				}
			}
			//cout<<"("<<e1a.to<<" "<<e1a.d<<" "<<e2a.d<<")";
			if(moveok==false)continue;
			pq.push(e2a);
		}
	}
	int ans=-1;
	for(int j=0;j<3;j++){
		for(int i=0;i<=200;i++){
			long long int t1=dp[j][i][n];
			if(ans==-1 || (t1!=-1 && t1<ans))ans=t1;
		}
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 68012
final_rank: 15

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define printar(ar)             \
    do                          \
    {                           \
        for (auto dbg : ar)     \
        {                       \
            cout << dbg << " "; \
        }                       \
        cout << endl;           \
    } while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
    if constexpr (idx < n)
        return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else
        return init;
}

template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
    return make_vec(d, T{});
}
//////////////// 以下を貼る ////////////////
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
    return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
    size_t operator()(const std::pair<T, S> &keyval) const noexcept
    {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
////////////////////////////////////////////
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, m, x;
    cin >> n >> m >> x;
    vector<ll> ar(n);
    rep(i, 0, n)
    {
        cin >> ar[i];
        if (ar[i] == 1)
        {
            ar[i] = 2;
        }
        else if (ar[i] == 2)
        {
            ar[i] = 1;
        }
    }
    vector<vector<pair<ll, ll>>> g(n);
    rep(i, 0, m)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        g[a].push_back(pr(b, c));
        g[b].push_back(pr(a, c));
    }
    auto d = make_vec({n, 2, x + 1}, inf);
    // d[i][j][k]:最後に入った温度がj:(1:温/0:冷)であり、そこからk分経過した時点で頂点iにいるときの最小値
    d[0][0][0] = 0;
    using T = pair<ll, pair<ll, pair<ll, ll>>>;
    priority_queue<T, vector<T>, greater<T>> que;
    que.push(pr(0, pr(0, pr(0, 0))));
    while (!que.empty())
    {
        ll A = que.top().second.first;
        ll B = que.top().second.second.first;
        ll C = que.top().second.second.second;
        ll D = que.top().first;
        que.pop();
        if (D != d[A][B][C])
        {
            continue;
        }
        rep(i, 0, g[A].size())
        {
            ll nxt = g[A][i].first;
            ll dist = g[A][i].second;
            if (ar[nxt] == 2)
            {
                if (d[nxt][B][min(C + dist, x)] > d[A][B][C] + dist)
                {
                    d[nxt][B][min(C + dist, x)] = d[A][B][C] + dist;
                    que.push(pr(d[A][B][C] + dist, pr(nxt, pr(B, min(C + dist, x)))));
                }
            }
            else
            {
                if (ar[nxt] == B)
                {
                    if (d[nxt][B][0] > d[A][B][C] + dist)
                    {
                        d[nxt][B][0] = d[A][B][C] + dist;
                        que.push(pr(d[A][B][C] + dist, pr(nxt, pr(B, 0))));
                    }
                }
                else
                {
                    if (C + dist < x)
                    {
                        continue;
                    }
                    if (d[nxt][1 - B][0] > d[A][B][C] + dist)
                    {
                        d[nxt][1 - B][0] = d[A][B][C] + dist;
                        que.push(pr(d[A][B][C] + dist, pr(nxt, pr(1 - B, 0))));
                    }
                }
            }
        }
    }
    /*
    rep(a, 0, n)
    {
        ll sub = inf;
        rep(i, 0, 2)
        {
            rep(j, 0, x + 1)
            {
                chmin(sub, d[a][i][j]);
            }
        }
        cout << sub << " ";
    }
    cout << Endl;
    */
    ll ans = inf;
    rep(i, 0, 2)
    {
        rep(j, 0, x + 1)
        {
            chmin(ans, d[n - 1][i][j]);
        }
    }
    cout << ans << Endl;
}
accuracy: 1
time_ms: 20
memory_kb: 37248
final_rank: 9

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


int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    ll N,M,X;
    cin>>N>>M>>X;
    vector<ll> T(N);
    rep(i,N)cin>>T[i];
    vector<vector<array<ll,2>>> G(N);
    rep(i,M){
        ll U,V,D;
        cin>>U>>V>>D;
        U--;V--;
        G[U].push_back({V,D});
        G[V].push_back({U,D});
    }
    vector<vector<vector<ll>>> DP(N,vector<vector<ll>>(X+1,vector<ll>(2,1e18)));
    priority_queue<array<ll,4>,vector<array<ll,4>>,greater<array<ll,4>>> Q; 
    vector<vector<vector<bool>>> seen(N,vector<vector<bool>>(X+1,vector<bool>(2,0)));
    Q.push({0,0,0,0});
    DP[0][0][0]=0;
    while(!Q.empty()){
        auto P=Q.top();
        Q.pop();
        if(seen[P[1]][P[2]][P[3]])continue;
        seen[P[1]][P[2]][P[3]]=1;
        for(auto VW:G[P[1]]){
            ll V=VW[0];
            ll W=VW[1];
            ll ND=W+P[0];
            ll NX=min(X,P[2]+W);
            bool NB=P[3];
            if(T[V]==0&&P[3]&&NX<X)continue;
            if(T[V]==2&&!P[3]&&NX<X)continue;
            if(T[V]==0){
                NX=0;
                NB=0;
            }
            if(T[V]==2){
                NX=0;
                NB=1;
            }
            if(DP[V][NX][NB]>ND){
                DP[V][NX][NB]=ND;
                Q.push({ND,V,NX,NB});
            }
        }
        // if(T[P[1]]==1){
        //     P[0]++;
        //     P[2]++;
        //     if(X>=P[2])
        //     if(DP[P[1]][P[2]][P[3]]>P[0]){
        //         Q.push(P);
        //         DP[P[1]][P[2]][P[3]]=P[0];
        //     }
        // }
    }
    ll an=1e18;
    rep(i,X+1)rep(j,2)an=min(an,DP[N-1][i][j]);
    cout<<an<<endl;
}
accuracy: 1
time_ms: 210
memory_kb: 256068
final_rank: 17

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

int main() {
    int N, M, X;
    std::cin >> N >> M >> X;
    std::vector<int> T(N);
    for (auto& t : T) std::cin >> t;
    auto f{[&](int v, int i, bool j) -> int {
        assert(0 <= v and v < N);
        assert(0 <= i and i <= X);
        if (j) i += X;
        return v * (2 * (X + 1)) + i;
    }};
    std::vector<std::vector<std::pair<int, int>>> g(N * (2 * (X + 1)));
    // for (int v{} ; v < N ; v++) if (T[v] == 1) {
    //     for (int i{} ; i < X ; i++) {
    //         g[f(v, i, false)].emplace_back(f(v, std::min(X, i + 1), false), 1);
    //         g[f(v, i, true)].emplace_back(f(v, std::min(X, i + 1), true), 1);
    //     }
    // }
    for (int i{} ; i < M ; i++) {
        int a, b, d;
        std::cin >> a >> b >> d;
        a--; b--;
        for (int _{} ; _ < 2 ; _++, std::swap(a, b)) {
            if (T[a] == 0) {
                if (T[b] == 0) g[f(a, 0, false)].emplace_back(f(b, 0, false), d);
                else if (T[b] == 1) g[f(a, 0, false)].emplace_back(f(b, std::min(X, d), false), d);
                else {
                    assert(T[b] == 2);
                    if (d >= X) g[f(a, 0, false)].emplace_back(f(b, 0, true), d);
                }
            }
            else if (T[a] == 1) {
                if (T[b] == 0) {
                    for (int j{} ; j <= X ; j++) g[f(a, j, false)].emplace_back(f(b, 0, false), d);
                    for (int j{std::max(0, X - d)} ; j <= X ; j++) g[f(a, j, true)].emplace_back(f(b, 0, false), d);
                }
                else if (T[b] == 1) {
                    for (int j{} ; j <= X ; j++) {
                        g[f(a, j, false)].emplace_back(f(b, std::min(X, j + d), false), d);
                        g[f(a, j, true)].emplace_back(f(b, std::min(X, j + d), true), d);
                    }
                }
                else if (T[b] == 2) {
                    for (int j{} ; j <= X ; j++) g[f(a, j, true)].emplace_back(f(b, 0, true), d);
                    for (int j{std::max(0, X - d)} ; j <= X ; j++) g[f(a, j, false)].emplace_back(f(b, 0, true), d);
                }
                else assert(false);
            }
            else if (T[a] == 2) {
                if (T[b] == 2) g[f(a, 0, true)].emplace_back(f(b, 0, true), d);
                else if (T[b] == 1) g[f(a, 0, true)].emplace_back(f(b, std::min(X, d), true), d);
                else {
                    assert(T[b] == 0);
                    if (d >= X) g[f(a, 0, true)].emplace_back(f(b, 0, false), d);
                }
            }
            else assert(false);
        }
    }
    std::vector<int> dist(g.size(), (int)2e9);
    dist[f(0, 0, false)] = 0;
    using qt = std::pair<int, int>;
    std::priority_queue<qt, std::vector<qt>, std::greater<qt>> que;
    que.push(std::pair{ 0, f(0, 0, false) });
    while (que.size()) {
        auto [d, v]{que.top()};
        que.pop();
        if (dist[v] < d) continue;
        for (auto [x, w] : g[v]) if (dist[x] > dist[v] + w) {
            dist[x] = dist[v] + w;
            que.push(std::pair{ dist[x], x });
        }
    }
    // for (int v{} ; v < N ; v++) {
    //     for (int i{} ; i <= X ; i++) std::cout << '(' << dist[f(v, i, false)] << ',' << dist[f(v, i, true)] << ')' << ' ';
    //     std::cout << '\n';
    // }
    int ans{(int)2e9};
    for (int i{} ; i <= X ; i++) {
        ans = std::min(ans, dist[f(N - 1, i, false)]);
        ans = std::min(ans, dist[f(N - 1, i, true)]);
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 300
memory_kb: 242076
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
const int INF = (1<<29);
typedef pair<int, int> Pi;
typedef pair<Pi, Pi> PPi;
int id=0, min_cost[10001][3][201];
vector<Pi> vec[10001];
int main(){
    int n, m, x, a, b, d;
    int t[10001];
    cin >> n >> m >> x;
    for(int i=1;i<=n;i++){
        cin >> t[i];
    }
    for(int i=0;i<m;i++){
        cin >> a >> b >> d;
        vec[a].push_back(Pi(d, b));
        vec[b].push_back(Pi(d, a));
    }
    fill_n(**min_cost, 10001*3*201, INF);
    min_cost[1][0][x] = 0;
    priority_queue<PPi, vector<PPi>, greater<PPi> > pq;
    pq.push(PPi(Pi(0, 1), Pi(0, x)));
    while(!pq.empty()){
        PPi ppi = pq.top();
        pq.pop();
        Pi edge = ppi.first;
        Pi tmp = ppi.second;
        for(int i=0;i<vec[edge.second].size();i++){
            int to = vec[edge.second][i].second;
            int dis = vec[edge.second][i].first;
            int cost = edge.first + dis;
            if(abs(tmp.first-t[to])==2 && tmp.second-dis > 0) continue;
            if(t[to]==1){
                if(min_cost[to][tmp.first][max(0, tmp.second-dis)] > cost){
                    min_cost[to][tmp.first][max(0, tmp.second-dis)] = cost;
                    pq.push(PPi(Pi(cost, to), Pi(tmp.first, max(0, tmp.second-dis))));
                }
            } else{
                if(min_cost[to][t[to]][x] > cost){
                    min_cost[to][t[to]][x] = cost;
                    pq.push(PPi(Pi(cost, to), Pi(t[to], x)));
                }
            }
        }
    }
    int ans = INF;
    for(int i=0;i<3;i++){
        for(int j=0;j<=x;j++){
            ans = min(ans, min_cost[n][i][j]);
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 1
time_ms: 30
memory_kb: 27624
final_rank: 8

code_snippet: //#define _GLIBCXX_DEBUG

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

#define endl '\n'
#define lfs cout<<fixed<<setprecision(10)
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9+7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T> using PQ = priority_queue<T>;
template<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;
template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);};  
template<typename T>void debug(const T &v,ll h,ll w,string sv=" "){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};
template<typename T>void debug(const T &v,ll n,string sv=" "){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};
template<typename T>void debug(const vector<T>&v){debug(v,v.size());}
template<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}
template<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<" ";st.pop_front();}cout<<endl;}
template<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<" ";cout<<endl;}
template<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<" ";cout<<endl;}
template<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<" ";cout<<endl;}
template<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<"["<<z.first<<"]="<<z.second<<",";cout<<endl;}
template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};
template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;}
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;}
template<typename T>void rearrange(vector<int>&ord, vector<T>&v){
  auto tmp = v;
  for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];
}
template<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){
  rearrange(ord, head);
  rearrange(ord, tail...);
}
template<typename T> vector<int> ascend(const vector<T>&v){
  vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);
  sort(ord.begin(),ord.end(),[&](int i,int j){return make_pair(v[i],i)<make_pair(v[j],j);});
  return ord;
}
template<typename T> vector<int> descend(const vector<T>&v){
  vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);
  sort(ord.begin(),ord.end(),[&](int i,int j){return make_pair(v[i],-i)>make_pair(v[j],-j);});
  return ord;
}
template<typename T> vector<T> inv_perm(const vector<T>&ord){
  vector<T>inv(ord.size());
  for(int i=0;i<ord.size();i++)inv[ord[i]] = i;
  return inv;
}
ll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}
ll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}
ll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}
ll modulo(ll n,ll d){return (n%d+d)%d;};
template<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}
template<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}
template<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};
template<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int popcount(ll x){return __builtin_popcountll(x);};
int poplow(ll x){return __builtin_ctzll(x);};
int pophigh(ll x){return 63 - __builtin_clzll(x);};
template<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};
template<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};
template<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};
template<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};
ll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}
ll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}
ll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}
template< typename T = int >
struct edge {
  int to;
  T cost;
  int id;
  edge():id(-1){};
  edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}
  operator int() const { return to; }
};

template<typename T>
using Graph = vector<vector<edge<T>>>;
template<typename T>
Graph<T>revgraph(const Graph<T> &g){
  Graph<T>ret(g.size());
  for(int i=0;i<g.size();i++){
    for(auto e:g[i]){
      int to = e.to;
      e.to = i;
      ret[to].push_back(e);
    }
  }
  return ret;
}
template<typename T>
Graph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){
  Graph<T> ret(n);
  for(int es = 0; es < m; es++){
    int u,v;
    T w=1;
    cin>>u>>v;u-=indexed,v-=indexed;
    if(weighted)cin>>w;
    ret[u].emplace_back(v,w,es);
    if(!directed)ret[v].emplace_back(u,w,es);
  }
  return ret;
}
template<typename T>
Graph<T> readParent(int n,int indexed=1,bool directed=true){
  Graph<T>ret(n);
  for(int i=1;i<n;i++){
    int p;cin>>p;
    p-=indexed;
    ret[p].emplace_back(i);
    if(!directed)ret[i].emplace_back(p);
  }
  return ret;
}
template<typename G>
vector<ll> dijkstra(G &g,const vector<int>&s) {
  using PT=pair<ll,int>;
  priority_queue<PT, vector<PT>, greater<PT> > que;
  vector<ll>dist(g.size(),INF);
  for(auto v:s){
    dist[v] = 0;
    que.emplace(0, v);
  }
  while (!que.empty()) {
    PT p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first) continue;
    for(auto e:g[v]) {
      if (dist[e.to] > dist[v] + e.cost) {
          dist[e.to] = dist[v] + e.cost;
          que.emplace(dist[e.to], e.to);
      }
    }
  }
  return dist;
}
template<typename G>
vector<ll>dijkstra(G &g,int s=0){
  return dijkstra(g,vector<int>(1,s));
}
template<typename T,typename F,int dim>
struct MultiGraph{
  using A=array<int,dim>;
  A init;
  int sz;
  const F &f;
  //f:(array)->vector<pair(array,cost)>
  MultiGraph(A init, const F &f):init(init),f(f){
    sz = 1;
    for(auto s:init)sz *= s;
  }
  int to_id(A state){
    int ret = 0;
    for(int i = 0; i < init.size(); i++){
      ret *= init[i];
      ret += state[i];
    }
    return ret;
  }
  A from_id(int id){
    A ret;
    for(int i = init.size() - 1; i >= 0; i--){
      ret[i] = id % init[i];
      id /= init[i];
    }
    return ret;
  }
  vector<edge<T>> operator[](int id){
    vector<edge<T>>ret;
    auto edges = f(from_id(id));
    ret.reserve(edges.size());
    for(int i = 0; i < edges.size(); i++){
      ret.emplace_back(to_id(edges[i].first), edges[i].second);
    }
    return ret;
  }
  int size(){
    return sz;
  }
};

/*
  
*/
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ll res=0,buf=0;
  bool judge = true;
	using A=array<int,2>;
	int n,m,x;cin>>n>>m>>x;
	vector<int>t(n);
	rep(i,0,n)cin>>t[i];
	auto g=readGraph<int>(n,m,1,false,true);
	int off=203;
	int sz=407;
  auto f=[&](A state)->vector<pair<A,int>>{
    auto [i,j]=state;
    vector<pair<A,int>>ret;
    ret.reserve(g[i].size());
		for(auto e:g[i]){
			int nj=j;
			if(nj<off){
				nj=min(nj+e.cost,off);
			}
			else{
				nj=max(off,nj-e.cost);
			}
			if(t[e.to]==0){
				if(nj>off)continue;
				nj=off-x;
			}
			if(t[e.to]==2){
				if(nj<off)continue;
				nj=off+x;
			}
			ret.EB(A{e.to,nj},e.cost);
		}
    return ret;
  };
  MultiGraph<int,decltype(f),2>mg(A{n,sz},f);
	auto d=dijkstra(mg,mg.to_id(A{0,off-x}));
	ll ret=INF;
	rep(i,0,sz)chmin(ret,d[mg.to_id(A{n-1,(int)i})]);
	cout<<ret<<endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 36236
final_rank: 12

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0635.cc: ヘビの JOI 君 (Snake JOI)
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 10000;
const int MAX_X = 200;
const int T = 3;
const int MAX_GN = MAX_N * (MAX_X + 1) * T;
const int INF = 1 << 30;

enum { COLD = 0, FINE = 1, HOT = 2 };

/* typedef */

typedef pair<int,int> pii;
typedef vector<pii> vpii;

/* global variables */

int ts[MAX_N], ds[MAX_GN];
vpii nbrs[MAX_N];

/* subroutines */

inline int ijt2p(int i, int j, int t, int x) {
  return (i * (x + 1) + j) * T + t;
}

inline void p2ijt(int p, int &i, int &j, int &t, int x) {
  i = p / ((x + 1) * T), j = (p / T) % (x + 1), t = p % T;
}

/* main */

int main() {
  int n, m, x;
  scanf("%d%d%d", &n, &m, &x);

  for (int i = 0; i < n; i++) scanf("%d", ts + i);

  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    u--, v--;
    nbrs[u].push_back(pii(v, w));
    nbrs[v].push_back(pii(u, w));
  }

  int gn = n * (x + 1) * T;
  int st = ijt2p(0, x, COLD, x);
  fill(ds, ds + gn, INF);
  ds[st] = 0;

  priority_queue<pii> q;
  q.push(pii(0, st));

  int mind = INF;
  while (! q.empty()) {
    pii ue = q.top(); q.pop();
    int ud = -ue.first, u = ue.second;
    if (ds[u] != ud) continue;

    int ui, uj, ut;
    p2ijt(u, ui, uj, ut, x);
    if (ui == n - 1) {
      mind = ud;
      break;
    }

    for (auto vw: nbrs[ui]) {
      int vi = vw.first, w = vw.second;
      int vj, vt;
      if (uj > w) {
	if ((ut == COLD && ts[vi] == HOT) ||
	    (ut == HOT && ts[vi] == COLD)) continue;
	if (ts[vi] == FINE)
	  vj = uj - w, vt = ut;
	else
	  vj = x, vt = ts[vi];
      }
      else if (ts[vi] == FINE)
	vj = 0, vt = FINE;
      else
	vj = x, vt = ts[vi];

      int v = ijt2p(vi, vj, vt, x);
      int vd = ud + w;
      if (ds[v] > vd) {
	ds[v] = vd;
	q.push(pii(-vd, v));
      }
    }
  }

  printf("%d\n", mind);
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 27252
final_rank: 2

code_snippet: #include<iostream>
#include<string>
#include<algorithm>    
#include<cmath>
#include<ctime>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<tuple>
#include<cassert>
#include<set>
#include<bitset>
#include<functional>
#include <fstream>
//#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define rep3(i, x, y) for(ll i = x; i < y; i++)
#define rep4(i, x) for(ll i = x - 1; i >= 0; i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define puts(x) cout << (x) << "\n"
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const int intINF = 1000000000;
const ll mod = 1000000007;
const ll MOD = 998244353;
const ld pi = 3.141592653589793238;
//const ld EPS = 1e-9;

bool isprime(int p) {
	if (p == 1) return false;
	for (int i = 2; i < p; i++) {
		if (p % i == 0) return false;
	}
	return true;
}
ll gcd(ll a, ll b) {
	if (b == 0) { return a; }
	return gcd(b, a % b);
}
//ax + by = cが整数解をもつための必要十分条件は c が gcd(a,b) で割り切れること。
// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
//main関数内に extGCD(a, b, x, y); でx, yに解が格納
ll extGCD(ll a, ll b, ll& x, ll& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	ll d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}
ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}
ll keta(ll n) {
	ll res = 0;
	while (n >= 1) {
		//res++;
		res += n % 10;
		n /= 10;
	}
	return res;
}
ll modpow(ll x, ll y, ll m) {
	ll res = 1;
	while (y) {
		if (y % 2) { res *= x; res %= m; }
		x = x * x % m; y /= 2;
	}
	return res;
}
ll kaijyo[1000005];
void nCkinit(ll n, ll m) {
	ll cnt = 1; kaijyo[0] = 1;
	for (int h = 1; h <= n; h++) { cnt *= h; cnt %= m; kaijyo[h] = cnt; }
}
ll nCk(ll n, ll k, ll m) {
	/*ll a = 1, b = 1;
	for (int h = 1; h <= n; h++) { a *= h; a %= m; }
	for (int h = 1; h <= k; h++) { b *= h; b %= m; }
	for (int h = 1; h <= n - k; h++) { b *= h; b %= m; }*/
	ll a = kaijyo[n], b = kaijyo[k] * kaijyo[n - k] % m;
	return a * modpow(b, m - 2, m) % m;
}
//printf("%.10f\n", n);
typedef pair <ll, ll> P;
typedef pair <ld, ll> pp;
ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
struct edge { ll to; ll cost; };


vector<ll> spf;
void smallest_prime_factors(ll n) {
	spf.resize(n + 1); spf[1] = 1;
	for (int i = 2; i <= n; i++) {
		if (!spf[i]) {
			for (int j = i; j < n; j += i) { spf[j] = i; }
		}
	}
}
std::map<int, int> high_speed_prime_factor(int n) {
	std::map<int, int> res;
	while (n != 1) {
		res[spf[n]]++;
		n /= spf[n];
	}
	return res;
}

struct Point {
	ll x, y;
};
bool operator<(const Point& a1, const Point& a2) {
	if (a1.x < a2.x) return true;
	if (a1.x > a2.x) return false;
	if (a1.y < a2.y) return true;
	return false;
}
struct status {
	ll now;
	ll te;
	ll time;
	ll c;

	bool operator<(const status& rhs) const { return c < rhs.c; };
	bool operator>(const status& rhs) const { return c > rhs.c; };
};

//O(ElogV)
//const ll INF = 10000000000000;
//struct edge { ll to, cost; };
//typedef pair<ll, ll>P; //firstは最短距離、secondは頂点番号

ll n, m, x; ll temp[10005];

ll d[10005][3][205];//始点sから終点tまでのコストd[t]
vector<edge> G[213456];//コスト
void dijkstra(ll s) {
	//greater<P>を指定→firstが小さい順
	priority_queue<status, vector<status>, greater<status> >que;
	d[s][0][0] = 0; que.push({ s, 0, 0, 0 });
	while (!que.empty()) {
		status p = que.top(); que.pop();
		int v = p.now;
		if (d[p.now][p.te][p.time] < p.c) { continue; }
		for (int i = 0; i < G[v].size(); i++) {
			edge e = G[v][i];
			if (abs(temp[e.to] - p.te) == 2 && p.time + e.cost < x) {//気温がダメ
				continue;
			}
			ll nexttemp = temp[e.to];
			ll nexttime = 0;
			if (p.te != 1 && nexttemp == 1 && p.time + e.cost < x) {
				nexttemp = p.te; nexttime = p.time + e.cost;
			}
			if (d[e.to][nexttemp][nexttime] > p.c + e.cost) {
				d[e.to][nexttemp][nexttime] = p.c + e.cost;
				que.push({ e.to,nexttemp,nexttime,p.c + e.cost });
			}
		}
	}
}

int main() {
	srand((unsigned)time(NULL));
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	//cout << fixed << setprecision(15);

	cin >> n >> m >> x;
	rep2(i, n) {
		cin >> temp[i];
	}
	rep(i, m) {
		ll a, b, d; cin >> a >> b >> d;
		G[a].push_back({ b,d });
		G[b].push_back({ a,d });
	}
	rep(i, 10005) {
		rep(j, 3) {
			rep(k, 205) {
				d[i][j][k] = INF;
			}
		}
	}
	dijkstra(1);
	ll ans = INF;
	rep(j, 3) {
		rep(k, 205) {
			ans = min(ans, d[n][j][k]);
		}
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 57876
final_rank: 13

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcountll(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=1LL<<60;
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){return (a+b-1)/b;}
int msb(ll v){return 63-__builtin_clzll(v);}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    INT(n,m,x);
    vector<int> type(n);
    in(type);
    rep(i,n){
        if(type[i]==1)type[i]=2;
        else if(type[i]==2)type[i]=1;
    }
    vector<vector<pair<int,int>>> g(n);
    int u,v,s,t;
    ll c;
    rep(i,m){
        in(u,v,c);
        g[--u].emplace_back(--v,c);
        g[v].emplace_back(u,c);
    }
    rpriority_queue<tuple<ll,int,int,int>> pq;
    vector3d(ll,dist,n,2,x+1,INF2);
    dist[0][0][x]=0;
    pq.push({0,0,0,x});
    while(!pq.empty()){
        tie(c,v,s,t)=pq.top();
        pq.pop();
        for(auto p:g[v]){
            if(type[p.first]==2||t-p.second<=0){
                if(type[p.first]==2){
                    if(c+p.second<dist[p.first][s][max(0,t-p.second)]){
                        dist[p.first][s][max(0,t-p.second)]=c+p.second;
                        pq.push({c+p.second,p.first,s,max(0,t-p.second)});
                    }
                }else{
                    if(c+p.second<dist[p.first][type[p.first]][x]){
                        dist[p.first][type[p.first]][x]=c+p.second;
                        pq.push({c+p.second,p.first,type[p.first],x});
                    }
                }
            }else if(type[p.first]==s){
                if(c+p.second<dist[p.first][type[p.first]][x]){
                    dist[p.first][type[p.first]][x]=c+p.second;
                    pq.push({c+p.second,p.first,type[p.first],x});
                }
            }
        }
    }
    ll ans=INF2;
    rep(i,2)rep(j,x+1)chmin(ans,dist[n-1][i][j]);
    out(ans);
}
accuracy: 1
time_ms: 20
memory_kb: 36472
final_rank: 7

code_snippet: #include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 1012345678;
struct edge {
	int to, cost;
};
struct state {
	int pos, level, cost;
	bool operator<(const state& s) const {
		return cost > s.cost;
	}
};
int main() {
	int N, M, X;
	cin >> N >> M >> X;
	vector<int> T(N);
	for (int i = 0; i < N; ++i) {
		cin >> T[i];
	}
	vector<vector<edge> > G(N);
	for (int i = 0; i < M; ++i) {
		int a, b, c;
		cin >> a >> b >> c; --a, --b;
		G[a].push_back(edge{ b, c });
		G[b].push_back(edge{ a, c });
	}
	vector<vector<int> > dist(N, vector<int>(2 * X + 1, inf));
	priority_queue<state> que;
	dist[0][T[0] * X] = 0;
	que.push(state{ 0, T[0] * X, 0 });
	vector<vector<bool> > vis(N, vector<bool>(2 * X + 1, false));
	while (!que.empty()) {
		state u = que.top(); que.pop();
		if (vis[u.pos][u.level]) continue;
		vis[u.pos][u.level] = true;
		for (edge e : G[u.pos]) {
			int nlevel = X;
			if (u.level < X) nlevel = min(u.level + e.cost, X);
			if (u.level > X) nlevel = max(u.level - e.cost, X);
			int nd = dist[u.pos][u.level] + e.cost;
			if (T[e.to] == 0 && nlevel <= X && dist[e.to][0] > nd) {
				dist[e.to][0] = nd;
				que.push(state{ e.to, 0, nd });
			}
			if (T[e.to] == 1 && dist[e.to][nlevel] > dist[u.pos][u.level] + e.cost) {
				dist[e.to][nlevel] = dist[u.pos][u.level] + e.cost;
				que.push(state{ e.to, nlevel, nd });
			}
			if (T[e.to] == 2 && nlevel >= X && dist[e.to][2 * X] > nd) {
				dist[e.to][2 * X] = nd;
				que.push(state{ e.to, 2 * X, nd });
			}
		}
	}
	int ans = inf;
	for (int i = 0; i <= 2 * X; ++i) {
		ans = min(ans, dist[N - 1][i]);
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 20772
final_rank: 5

code_snippet: #include <stdio.h>
#include <tuple>
#include <vector>
#include <queue>
using namespace std;
 
#define MAX_N 11111
#define MAX_TMPR 222
 
typedef tuple<int, int, int, int> info;
typedef pair<int, int> PAIR;
 
vector<pair<int, int> > v[MAX_N];
priority_queue<info, vector<info>, greater<info> > pq;
pair<int, int> visit[MAX_N];
int tmpr[MAX_N];
 
int main(void)
{
    int n, m, x, i;
 
    scanf("%d %d %d", &n, &m, &x);
 
    for (i = 1; i <= n; i++)
    {
        scanf("%d", &tmpr[i]);
        visit[i] = make_pair(x + 1, x + 1);
    }
 
    while (m--)
    {
        int a, b, d;
 
        scanf("%d %d %d", &a, &b, &d);
 
        v[a].push_back(make_pair(b, d));
        v[b].push_back(make_pair(a, d));
    }
 
    pq.push(make_tuple(0, 1, x, 0));
 
    int time, node, hot, cold;
 
    while (!pq.empty())
    {
        tie(time, node, hot, cold) = pq.top(); pq.pop();
 
        if (node == n)
        {
            printf("%d\n", time);
            return 0;
        }
 
        if (visit[node].first <= hot && visit[node].second <= cold)
            continue;
        visit[node] = make_pair(min(visit[node].first, hot), min(visit[node].second, cold));
 
        for (PAIR next : v[node])
        {
            int nextv = next.first;
            int nexttime = time + next.second;
            PAIR nexttemp = make_pair(max(hot - next.second, 0), max(cold - next.second, 0));
 
            if (tmpr[nextv] == 0)
                nexttemp.first = x;
            else if (tmpr[nextv] == 2)
                nexttemp.second = x;
 
            if ((nexttemp.first && nexttemp.second) || (visit[nextv].first <= nexttemp.first && visit[nextv].second <= nexttemp.second))
                continue;
 
            pq.push(make_tuple(nexttime, nextv, nexttemp.first, nexttemp.second));
        }
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3784
final_rank: 1

code_snippet: #include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

int dp[3][201][10010];
vector<pair<int,int>> g[10010];

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m; cin >> n >> m;
    int x; cin >> x;
    vector<int> c(n);
    for(int i=0;i<n;i++){
        cin >> c[i];
    }
    while(m--){
        int x,y,d; cin >> x >> y >> d;
        x--; y--; 
        g[x].push_back(make_pair(y,d));
        g[y].push_back(make_pair(x,d));
    }
    for(int i=0;i<3;i++){
        for(int j=0;j<=200;j++){
            for(int k=0;k<n;k++){
                dp[i][j][k]=1e9;
            }
        }
    }
    dp[0][0][0]=0;
    priority_queue<pair<pair<int,int>,pair<int,int>>> q;
    q.push(make_pair(make_pair(0,0),make_pair(0,0)));
    while(q.size()){
        auto p=q.top(); q.pop();
        if(p.second.second==n-1){
            cout << -p.first.first << endl;
            return 0;
        }
        for(auto t:g[p.second.second]){
            if(c[t.first]==1){
                if(p.first.second==1){
                    if(dp[1][0][t.first]>-p.first.first+t.second){
                        dp[1][0][t.first]=-p.first.first+t.second;
                        q.push(make_pair(make_pair(-dp[1][0][t.first],1),make_pair(0,t.first)));
                    }
                }
                else{
                    if(p.second.first+t.second>=x){
                        if(dp[1][0][t.first]>-p.first.first+t.second){
                            dp[1][0][t.first]=-p.first.first+t.second;
                            q.push(make_pair(make_pair(-dp[1][0][t.first],1),make_pair(0,t.first)));
                        }
                    }
                    else{
                        if(dp[p.first.second][p.second.first+t.second][t.first]>-p.first.first+t.second){
                            dp[p.first.second][p.second.first+t.second][t.first]=-p.first.first+t.second;
                            q.push(make_pair(make_pair(p.first.first-t.second,p.first.second),make_pair(p.second.first+t.second,t.first)));
                        }
                    }
                }
            }
            else{
                int type=c[t.first];
                if(p.first.second==1){
                    if(dp[type][0][t.first]>-p.first.first+t.second){
                        dp[type][0][t.first]=-p.first.first+t.second;
                        q.push(make_pair(make_pair(-dp[type][0][t.first],type),make_pair(0,t.first)));
                    }
                }
                else{
                    if(type==p.first.second){
                        if(dp[type][0][t.first]>-p.first.first+t.second){
                            dp[type][0][t.first]=-p.first.first+t.second;
                            q.push(make_pair(make_pair(-dp[type][0][t.first],type),make_pair(0,t.first)));
                        }
                    }
                    else{
                        if(p.second.first+t.second>=x){
                            if(dp[type][0][t.first]>-p.first.first+t.second){
                                dp[type][0][t.first]=-p.first.first+t.second;
                                q.push(make_pair(make_pair(-dp[type][0][t.first],type),make_pair(0,t.first)));
                            }
                        }
                    }
                }
            }
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 27728
final_rank: 3

code_snippet: // #pragma GCC target("avx")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
// #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
// #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
#include<numeric>
#include<unordered_set>
#include<unordered_map>
#include<complex>
using namespace std;
const long long mod=1000000007;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const long double EPS=1e-9;
const double INF=1e+10;
const double PI=acos(-1.0);
const int C_SIZE = 3100000;
const int UF_SIZE = 3100000;
namespace{
	long long fact[C_SIZE];
	long long finv[C_SIZE];
	long long inv[C_SIZE];
	long long Comb(int a,int b){
	 	if(a<b||b<0)return 0;
	 	return fact[a]*finv[b]%mod*finv[a-b]%mod;
	}
	void init_C(int n){
		fact[0]=finv[0]=inv[1]=1;
		for(int i=2;i<n;i++){
			inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
		}
		for(int i=1;i<n;i++){
			fact[i]=fact[i-1]*i%mod;
			finv[i]=finv[i-1]*inv[i]%mod;
		}
	}
	long long pw(long long a,long long b){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%mod;
			a=a*a%mod;
			b/=2;
		}
		return ret;
	}
	long long pw_mod(long long a,long long b,long long M){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%M;
			a=a*a%M;
			b/=2;
		}
		return ret;
	}
	int pw_mod_int(int a,int b,int M){
		if(a<0)return 0;
		if(b<0)return 0;
		int ret=1;
		while(b){
			if(b%2)ret=(long long)ret*a%M;
			a=(long long)a*a%M;
			b/=2;
		}
		return ret;
	}
	int ABS(int a){return max(a,-a);}
	long long ABS(long long a){return max(a,-a);}
	double ABS(double a){return max(a,-a);}
	int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
	int UF[UF_SIZE];
	void init_UF(int n){
		for(int i=0;i<n;i++)UF[i]=-1;
	}
	int FIND(int a){
		if(UF[a]<0)return a;
		return UF[a]=FIND(UF[a]);
	}
	void UNION(int a,int b){
		a=FIND(a);b=FIND(b);if(a==b)return;
		if(UF[a]>UF[b])swap(a,b);
		UF[a]+=UF[b];UF[b]=a;
	}
}
// ここから編集しろ
int v[11000][2][210];
int ijk[11000][2][210];
vector<pair<int,int> > g[11000];
int t[11000];
int main(){
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	for(int i=0;i<a;i++)scanf("%d",t+i);
	for(int i=0;i<b;i++){
		int p,q,r;scanf("%d%d%d",&p,&q,&r);p--;q--;g[p].push_back(make_pair(q,r));
		g[q].push_back(make_pair(p,r));
	}
	for(int i=0;i<a;i++)for(int j=0;j<=c;j++)for(int k=0;k<2;k++)ijk[i][k][j]=mod;
	priority_queue<pair<pair<int,int>,pair<int,int> > >Q;
	Q.push(make_pair(make_pair(0,0),make_pair(0,c)));
	ijk[0][0][c]=0;
	while(Q.size()){
		int cost=-Q.top().first.first;
		int at=Q.top().second.first;
		int tp=Q.top().first.second;
		int T=Q.top().second.second;Q.pop();
		if(v[at][tp][T])continue;
		// printf("%d %d: %d\n",at,T,cost);
		v[at][tp][T]=1;
		for(int i=0;i<g[at].size();i++){
			int to=g[at][i].first;
			int toc=cost+g[at][i].second;
			int tt=max(T-g[at][i].second,0);
			// if(tt&&ABS(t[at]-t[to])==2)continue;
			if(tt){
				if(tp==0&&t[to]==2)continue;
				if(tp==1&&t[to]==0)continue;
			}
			if(t[to]!=1)tt=c;
			
			int ttp=tp;
			if(t[to]==0)ttp=0;
			if(t[to]==2)ttp=1;
			if(v[to][ttp][tt])continue;
			if(ijk[to][tp][tt]>toc){
				ijk[to][tp][tt]=toc;
				Q.push(make_pair(make_pair(-toc,ttp),make_pair(to,tt)));
			}
		}
	}
	int ret=mod;
	for(int i=0;i<=c;i++)ret=min(ret,min(ijk[a-1][0][i],ijk[a-1][1][i]));
	printf("%d\n",ret);
}
accuracy: 1
time_ms: 30
memory_kb: 33508
final_rank: 11

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
template <typename T> using posteriority_queue = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
template <typename T> void unique(vector<T> &a) { a.erase(unique(ALL(a)), a.end()); }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

using CostType = int;
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &rhs) const {
    return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
  }
  inline bool operator<=(const Edge &rhs) const { return !(rhs < *this); }
  inline bool operator>(const Edge &rhs) const { return rhs < *this; }
  inline bool operator>=(const Edge &rhs) const { return !(*this < rhs); }
};

int main() {
  int n, m, x; cin >> n >> m >> x;
  vector<int> t(n); REP(i, n) cin >> t[i];
  vector<vector<Edge> > graph(n);
  while (m--) {
    int a, b, d; cin >> a >> b >> d; --a; --b;
    graph[a].emplace_back(a, b, d);
    graph[b].emplace_back(b, a, d);
  }
  vector<vector<ll> > dist(n, vector<ll>(2 * x + 1, LINF));
  dist[0][0] = 0;
  posteriority_queue<pair<ll, pair<int, int> > > que;
  que.emplace(0, make_pair(0, 0));
  while (!que.empty()) {
    int ver, tm; tie(ver, tm) = que.top().second;
    ll now_cost = que.top().first;
    que.pop();
    if (now_cost > dist[ver][tm]) continue;
    for (const Edge &e : graph[ver]) {
      ll cost = dist[ver][tm] + e.cost;
      if (tm < x) {
        int nx_tm;
        if (t[e.dst] == 0) {
          nx_tm = 0;
        } else if (t[e.dst] == 1) {
          nx_tm = min(tm + e.cost, x);
        } else {
          nx_tm = min(tm + e.cost, x);
          if (nx_tm < x) continue;
          nx_tm = 2 * x;
        }
        if (dist[e.dst][nx_tm] > cost) {
          dist[e.dst][nx_tm] = cost;
          que.emplace(cost, make_pair(e.dst, nx_tm));
        }
      } else if (tm == x) {
        int nx_tm = tm;
        if (t[e.dst] == 0) {
          nx_tm = 0;
        } else if (t[e.dst] == 2) {
          nx_tm = 2 * x;
        }
        if (dist[e.dst][nx_tm] > cost) {
          dist[e.dst][nx_tm] = cost;
          que.emplace(cost, make_pair(e.dst, nx_tm));
        }
      } else {
        int nx_tm;
        if (t[e.dst] == 0) {
          nx_tm = max(tm - e.cost, x);
          if (nx_tm > x) continue;
          nx_tm = 0;
        } else if (t[e.dst] == 1) {
          nx_tm = max(tm - e.cost, x);
        } else {
          nx_tm = 2 * x;
        }
        if (dist[e.dst][nx_tm] > cost) {
          dist[e.dst][nx_tm] = cost;
          que.emplace(cost, make_pair(e.dst, nx_tm));
        }
      }
    }
  }
  cout << *min_element(ALL(dist[n - 1])) << '\n';
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 35532
final_rank: 6

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

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

constexpr int X = 200, Y = X * 2 + 1;

int enc(int v, int t) {
    return v * Y + t;
}

constexpr int INF = 1 << 30;

void solve() {
    int n, m, x;
    std::cin >> n >> m >> x;

    std::vector<int> ts(n);
    for (auto& t : ts) std::cin >> t;

    int nn = n * Y;
    std::vector<std::vector<std::pair<int, int>>> graph(nn);
    while (m--) {
        int u, v, d;
        std::cin >> u >> v >> d;
        --u, --v;

        for (int i = 0; i < 2; ++i) {
            for (int t = 0; t < Y; ++t) {
                int nt = (t >= X ? std::max(X, t - d) : std::min(X, t + d));

                if (ts[v] == 1) {
                    graph[enc(u, t)].emplace_back(enc(v, nt), d);
                } else if (ts[v] == 0) {
                    if (nt > X) continue;
                    graph[enc(u, t)].emplace_back(enc(v, X - x), d);
                } else {
                    if (nt < X) continue;
                    graph[enc(u, t)].emplace_back(enc(v, X + x), d);
                }
            }

            std::swap(u, v);
        }
    }

    int r = enc(0, X - x);
    std::vector<int> dist(nn, INF);
    dist[r] = 0;

    MinHeap<std::pair<int, int>> que;
    que.emplace(0, r);

    while (!que.empty()) {
        int d, v;
        std::tie(d, v) = que.top();
        que.pop();
        if (dist[v] > d) continue;

        for (auto p : graph[v]) {
            int u, c;
            std::tie(u, c) = p;
            if (dist[u] <= dist[v] + c) continue;

            dist[u] = dist[v] + c;
            que.emplace(dist[u], u);
        }
    }

    int ans = INF;
    for (int t = 0; t < Y; ++t) {
        ans = std::min(ans, dist[enc(n - 1, t)]);
    }
    std::cout << ans << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 560
memory_kb: 314796
final_rank: 19

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "functional"

using namespace std;

constexpr long long int MOD = 1000000007;
//const int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;

constexpr double EPS = 1e-8;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

struct Edge {
	int to, cost;
	Edge(const int t, const int c) {
		to = t, cost = c;
		return;
	}
};

struct Node {
	int dis, node, flag, turn;
	Node(const int d, const int n, const int f, const int t) {
		dis = d, node = n, flag = f, turn = t;
		return;
	}
	bool operator<(const Node&n)const {
		return dis > n.dis;
	}
};

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

	cin >> N >> M >> K;
	vector<int>v(N);
	for (auto &i : v)cin >> i;
	vector<vector<Edge>>edge(N);
	for (int i = 0; i < M; i++) {
		cin >> L >> R >> H;
		L--, R--;
		edge[L].push_back(Edge(R, H));
		edge[R].push_back(Edge(L, H));
	}
	vector<vector<vector<long long int>>>dis(N, vector<vector<long long int>>(3, vector<long long int>(K + 1, MOD*MOD)));
	dis[0][2][K] = 0;
	priority_queue<Node>PQ;
	PQ.push(Node(0, 0, 2, K));
	while (!PQ.empty()) {
		auto box = PQ.top();
		PQ.pop();
		for (auto i : edge[box.node]) {
			if (v[i.to] == box.flag&&box.turn > i.cost)continue;
			if (v[i.to] == 0) {
				if (dis[i.to][2][K] > box.dis + i.cost) {
					dis[i.to][2][K] = box.dis + i.cost;
					PQ.push(Node(dis[i.to][2][K], i.to, 2, K));
				}
			}
			else if (v[i.to] == 2) {
				if (dis[i.to][0][K] > box.dis + i.cost) {
					dis[i.to][0][K] = box.dis + i.cost;
					PQ.push(Node(dis[i.to][0][K], i.to, 0, K));
				}
			}
			else {
				if (box.turn <= i.cost) {
					if (dis[i.to][1][0] > box.dis + i.cost) {
						dis[i.to][1][0] = box.dis + i.cost;
						PQ.push(Node(dis[i.to][1][0], i.to, 1, 0));
					}
				}
				else {
					if (dis[i.to][box.flag][box.turn - i.cost] > box.dis + i.cost) {
						dis[i.to][box.flag][box.turn - i.cost] = box.dis + i.cost;
						PQ.push(Node(dis[i.to][box.flag][box.turn - i.cost], i.to, box.flag, box.turn - i.cost));
					}
				}
			}
		}
	}
	//for (int i = 0; i < N; i++)for (int j = 0; j < 3; j++)for (int k = 0; k <= K; k++)if(dis[i][j][k]!=MOD*MOD)cout << i << " " << j << " " << k << " " << dis[i][j][k] << endl;
	long long int ans = MOD * MOD;
	for (auto i : dis.back())for (auto j : i)ans = min(ans, j);
	if (ans == MOD * MOD)ans = -1;
	cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 52092
final_rank: 14

code_snippet: #include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<random>
#include<functional>
#include<utility>

#define rep(i,n) for(int i=0; i<n; i++)
#define pb push_back
#define P pair<int, int>
#define PLI pair<ll, int>

using namespace std;

typedef long long ll;


const int INF = 1e9;

struct State
{
	int d, v, t, n;
	State(int d, int v, int t, int n) :d(d), v(v), t(t), n(n) {}
	bool operator < (const State& s) const {
		return d > s.d;
	}
};


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

int N, M, X;
int T[10000];
vector<edge> g[10000];
int dist[10000][205][3];
int main() {
	cin >> N >> M >> X;
	for (int i = 0; i < N; i++) {
		cin >> T[i];
	}

	for (int i = 0; i < M; i++) {
		int a, b, d;
		cin >> a >> b >> d; a--; b--;
		g[a].emplace_back(b, d);
		g[b].emplace_back(a, d);
	}

	
	priority_queue<State> pq;
	pq.emplace(0, 0, 0, 0);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j <= X; j++) {
			for (int k = 0; k < 3; k++) {
				dist[i][j][k] = INF;
			}
		}
	}
	dist[0][0][0] = 0;
	int ans;
	while (!pq.empty())
	{
		State st = pq.top(); pq.pop();
		int v = st.v, t = st.t, n = st.n;

		if (st.d > dist[v][t][n]) continue;

		if (v == N - 1) {
			ans = dist[v][t][n]; 
			break;
		}

		for (auto e : g[v]) {
			int nt = min(t + e.co, X);
			int nco = dist[v][t][n] + e.co;
			if (T[e.to] == n) {
				if (dist[e.to][0][n] > nco) {
					dist[e.to][0][n] = nco;
					pq.emplace(dist[e.to][0][n], e.to, 0, n);
				}
			}
			else if(T[e.to] == 1){
				if (dist[e.to][nt][n] > nco) {
					dist[e.to][nt][n] = nco;
					pq.emplace(dist[e.to][nt][n], e.to, nt, n);
				}
			}
			else {
				if (nt != X) continue;
				if (dist[e.to][0][T[e.to]] > nco) {
					dist[e.to][0][T[e.to]] = nco;
					pq.emplace(dist[e.to][0][T[e.to]], e.to, 0, T[e.to]);
				}
			}
		}
	}

	cout << ans << endl;
	

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 28096
final_rank: 4

code_snippet: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n, m, x;
    cin >> n >> m >> x;
    vector<int> v(n);
    for (auto &&i : v) scanf("%d", &i);
    using P = pair<int, int>;
    int N = n*2*(x+1);
    vector<vector<P>> G(N);
    for (int i = 0; i < m; ++i) {
        int a, b, d;
        scanf("%d %d %d", &a, &b, &d);
        a--; b--;
        G[a].emplace_back(b, d);
        G[b].emplace_back(a, d);
    }
    auto f = [&x](int i, int j, int k){
        return i*2*(x+1) + j*(x+1) + k;
    };

    vector<int> d(N, INF<int>);
    priority_queue<P, vector<P>, greater<>> Q;
    d[f(0, 0, x)] = 0;
    Q.emplace(0, f(0, 0, x));
    while(!Q.empty()){
        int cost, i;
        tie(cost, i) = Q.top(); Q.pop();
        int a = i/(2*(x+1)), b = (i/(x+1)) & 1, c = i % (x+1);
        if(d[i] < cost) continue;
        auto update = [&](int x, int p) -> void{
            if(d[x] <= p) return;
            d[x] = p;
            Q.emplace(d[x], x);
        };
        for (auto &&e : G[a]) {
            int to, val; tie(to, val) = e;
            if(v[to] == 1){
                update(f(to, b, max(0, c-val)), cost+val);
            }else if(v[to] == 0) {
                if(b == 0 || max(0, c-val) == 0) update(f(to, 0, x), cost+val);
            }else {
                if(b == 1 || max(0, c-val) == 0) update(f(to, 1, x), cost+val);
            }
        }
    }
    int ans = INF<int>;
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j <= x; ++j) {
            ans = min(ans, d[f(n-1, i, j)]);
        }
    }
    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 113416
final_rank: 16