Problem:
G: 雨降りバス乗り換え
背景
今日は AOR イカちゃんにとって初となるデートの日だ。
 AOR イカちゃんは駅からバスを乗り継ぎ、待ち合わせ場所のバス停に向かう予定である。

 AOR イカちゃんが駅に着いた時、不幸にも雨が降ってきた。当初予定していた経路ではバスの待ち時間で濡れてしまい、せっかく整えた身だしなみが台無しになってしまう可能性がある。

そこで、バスの待ち時間が最も少なくなるような経路でバス停まで向かうことにした。
 AOR イカちゃんは、待ち合わせ場所のバス停に着くまでにどの程度濡れるのか心配している。
問題
AOR イカちゃんは時刻 $0$ に $S$ 番目のバス停におり、そこから $G$ 番目のバス停まで行きたいと考えている。

バス停の個数 $N$ と、異なるバス停同士を繋ぐ $M$ 個の経路 (※) が与えられる。
バス停には、それぞれ $1, \dots, N$ の番号がふられている。

各経路は、出発地 $u$ 、目的地 $v$ 、出発時刻 $t$ 、所要時間 $c$ の $4$ つの値からなる。
時刻 $t$ に出発地 $u$ にいればバスに乗ることができ、時刻 $t + c$ に目的地 $v$ へ到着する。

バスに乗っていない間、 AOR イカちゃんは雨に濡れてしまう。
雨にぬれる時間が最小となる経路を通り $G$ 番目のバス停へ向かう時、時刻 $0$ から $G$ 番目のバス停に着くまでの間に雨に濡れた時間の合計を出力せよ。
(※) グラフ理論の用語における経路とは頂点と辺の列であるが，ここでは辺の意味で使われていることに注意してほしい。
制約
$2 \le N \le 10^5$
$1 \le M \le 2 \times 10^5$
$1 \le S , G \le N$
$1 \le u_i , v_i \le N$
$0 \le t_i \le 10^5$
$1 \le c_i \le 10^5$
$S \neq G$
$u_i \neq v_i$
出発地 $u$ と目的地 $v$ が同じであるような経路は存在しない。
出発地 $u$ と目的地 $v$ を結ぶ経路は複数存在する場合がある。
バスの乗り降り、乗り換えに時間はかからないものとする。
$G$ 番目のバス停へ到着する時間は問わない。
$S$ 番目のバス停から $G$ 番目のバス停へ到着できることは保証されている。
入力
入力は以下の形式で標準入力から与えられる。
$N \ M \ S \ G$
$u_1 \ v_1 \ t_1 \ c_1$
$\vdots$
$u_M \ v_M \ t_M \ c_M$
出力
雨に濡れた最小の時間を 1 行で出力せよ。また、末尾に改行も出力せよ。
サンプル
入力例 1
2 2 1 2
1 2 10 100
1 2 5 500
出力例 1
5
到着時間が遅くても、できるだけ雨に濡れない経路を選ぶ。
入力例 2
3 2 1 3
1 2 0 123
2 3 123 500
出力例 2
0
乗り換えに時間はかからない。

Submitted solutions:

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

using namespace std;

const int M = 200001;
int u[M], v[M], t[M], c[M];
vector<int> start[M], dest[M], busy_time[M];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifdef BACKER
        freopen("input.txt", "r", stdin);
        // freopen("output.txt", "w", stdout);
    #endif

    int n, m, s, g;
    cin >> n >> m >> s >> g;
    for (int i = 0; i < m; i++) cin >> u[i] >> v[i] >> t[i] >> c[i];
    for (int i = 0; i < m; i++) start[t[i]].push_back(i);
    vector<int> best(n + 1, -2e9);
    best[s] = 0;
    int ans = 2e9;
    for (int t = 0; t < M; t++) {
        int sz = dest[t].size();
        for (int i = 0; i < sz; i++) {
            best[dest[t][i]] = max(best[dest[t][i]], busy_time[t][i]);
            if (dest[t][i] == g) ans = min(ans, t - best[dest[t][i]]); 
        }
        for (int i: start[t]) {
            dest[t + c[i]].push_back(v[i]);
            busy_time[t + c[i]].push_back(best[u[i]] + c[i]);
        }
    }
    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 32172
final_rank: 5

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
int n, m, s, g;
int u[200005], v[200005], t[200005], c[200005];
int st[100005], ed[100005];
int sum;
int head[400005], dis[400005], vis[400005];
struct pi
{
    int v, t, id;
    pi(int aa = 0, int bb = 0)
    {
        v = aa;
        t = bb;
    }
    bool operator<(const pi &bb) const
    {
        return t < bb.t;
    }
};
vector<pi> p[200005];
struct node
{
    int v, w, nxt;
} a[400005];
struct node2
{
    int id, dis;
    node2(int aa = 0, int bb = 0)
    {
        id = aa;
        dis = bb;
    }
    bool operator<(const node2 &bb) const
    {
        return dis > bb.dis;
    }
};
priority_queue<node2> q;
void ins(int u, int v, int w)
{
    ++sum;
    a[sum].v = v;
    a[sum].w = w;
    a[sum].nxt = head[u];
    head[u] = sum;
}
void dijkstra()
{
    memset(dis, 0x7f, sizeof(dis));
    dis[0] = 0;
    q.push(node2(0, dis[0]));
    while (!q.empty())
    {
        int id = q.top().id;
        q.pop();
        if (id == st[g])
            break;
        if (vis[id])
            continue;
        vis[id] = 1;
        if (head[id])
            for (int d = head[id]; d; d = a[d].nxt)
                if (dis[id] + a[d].w < dis[a[d].v])
                {
                    dis[a[d].v] = dis[id] + a[d].w;
                    q.push(node2(a[d].v, dis[a[d].v]));
                }
    }
    return;
}
int main()
{
    cin >> n >> m >> s >> g;
    for (int i = 1; i <= m; ++i)
    {
        cin >> u[i] >> v[i] >> t[i] >> c[i];
        p[u[i]].push_back(pi(v[i], t[i]));
    }
    for (int i = 1; i <= n; ++i)
        sort(p[i].begin(), p[i].end());
    for (int i = 1; i <= n; ++i)
    {
        if (i == g)
        {
            st[i] = ed[i - 1] + 1;
            ed[i] = ed[i - 1] + 1;
            continue;
        }
        st[i] = ed[i - 1] + 1;
        ed[i] = max(st[i], st[i] + (int)p[i].size() - 1);
        if (p[i].size())
        {
            if (p[i].size() > 1)
                for (int j = 0; j < p[i].size() - 1; ++j)
                {
                    p[i][j].id = j;
                    ins(st[i] + j, st[i] + j + 1, p[i][j + 1].t - p[i][j].t);
                }
            p[i][p[i].size() - 1].id = p[i].size() - 1;
        }
    }
    ins(0, st[s], p[s][0].t);
    for (int i = 1; i <= m; ++i)
    {
        pi k = *(--upper_bound(p[u[i]].begin(), p[u[i]].end(), pi(0, t[i])));
        if (v[i] == g)
        {
            ins(st[u[i]] + k.id, st[v[i]], 0);
            continue;
        }
        if (p[v[i]].size())
            if (lower_bound(p[v[i]].begin(), p[v[i]].end(), pi(0, t[i] + c[i])) != p[v[i]].end())
            {
                pi kk = *lower_bound(p[v[i]].begin(), p[v[i]].end(), pi(0, t[i] + c[i]));
                ins(st[u[i]] + k.id, st[v[i]] + kk.id, kk.t - t[i] - c[i]);
            }
    }
    dijkstra();
    cout << dis[st[g]] << endl;
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 25612
final_rank: 4

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
int n, m, s, g;
int u[400005], v[400005], t[400005], c[400005];
int st[400005], ed[400005];
int sum;
int head[400005], dis[400005], vis[400005];
struct pi
{
    int v, t, id;
    pi(int aa = 0, int bb = 0)
    {
        v = aa;
        t = bb;
    }
    bool operator<(const pi &bb) const
    {
        return t < bb.t;
    }
};
vector<pi> p[400005];
struct node
{
    int v, w, nxt;
} a[800005];
struct node2
{
    int id, dis;
    node2(int aa = 0, int bb = 0)
    {
        id = aa;
        dis = bb;
    }
    bool operator<(const node2 &bb) const
    {
        return dis > bb.dis;
    }
};
priority_queue<node2> q;
void ins(int u, int v, int w)
{
    ++sum;
    a[sum].v = v;
    a[sum].w = w;
    a[sum].nxt = head[u];
    head[u] = sum;
}
void dijkstra()
{
    memset(dis, 0x7f, sizeof(dis));
    dis[0] = 0;
    q.push(node2(0, dis[0]));
    while (!q.empty())
    {
        int id = q.top().id;
        q.pop();
        if (id == st[g])
            break;
        if (vis[id])
            continue;
        vis[id] = 1;
        if (head[id])
            for (int d = head[id]; d; d = a[d].nxt)
                if (dis[id] + a[d].w < dis[a[d].v])
                {
                    dis[a[d].v] = dis[id] + a[d].w;
                    q.push(node2(a[d].v, dis[a[d].v]));
                }
    }
    return;
}
int main()
{
    cin >> n >> m >> s >> g;
    for (int i = 1; i <= m; ++i)
    {
        cin >> u[i] >> v[i] >> t[i] >> c[i];
        p[u[i]].push_back(pi(v[i], t[i]));
    }
    for (int i = 1; i <= n; ++i)
        sort(p[i].begin(), p[i].end());
    for (int i = 1; i <= n; ++i)
    {
        if (i == g)
        {
            st[i] = ed[i - 1] + 1;
            ed[i] = ed[i - 1] + 1;
            continue;
        }
        st[i] = ed[i - 1] + 1;
        ed[i] = max(st[i], st[i] + (int)p[i].size() - 1);
        if (p[i].size())
        {
            if (p[i].size() > 1)
                for (int j = 0; j < p[i].size() - 1; ++j)
                {
                    p[i][j].id = j;
                    ins(st[i] + j, st[i] + j + 1, p[i][j + 1].t - p[i][j].t);
                }
            p[i][p[i].size() - 1].id = p[i].size() - 1;
        }
    }
    ins(0, st[s], p[s][0].t);
    for (int i = 1; i <= m; ++i)
    {
        pi k = *(--upper_bound(p[u[i]].begin(), p[u[i]].end(), pi(0, t[i])));
        if (v[i] == g)
        {
            ins(st[u[i]] + k.id, st[v[i]], 0);
            continue;
        }
        if (p[v[i]].size())
            if (lower_bound(p[v[i]].begin(), p[v[i]].end(), pi(0, t[i] + c[i])) != p[v[i]].end())
            {
                pi kk = *lower_bound(p[v[i]].begin(), p[v[i]].end(), pi(0, t[i] + c[i]));
                ins(st[u[i]] + k.id, st[v[i]] + kk.id, kk.t - t[i] - c[i]);
            }
    }
    dijkstra();
    cout << dis[st[g]] << endl;
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 36308
final_rank: 8

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
int n, m, s, g;
int u[200005], v[200005], t[200005], c[200005];
int st[100005], ed[100005];
int sum;
int head[200005], dis[200005], vis[200005];
struct pi
{
    int v, t, id;
    pi(int aa = 0, int bb = 0)
    {
        v = aa;
        t = bb;
    }
    bool operator<(const pi &bb) const
    {
        return t < bb.t;
    }
};
vector<pi> p[100005];
struct node
{
    int v, w, nxt;
} a[400005];
struct node2
{
    int id, dis;
    node2(int aa = 0, int bb = 0)
    {
        id = aa;
        dis = bb;
    }
    bool operator<(const node2 &bb) const
    {
        return dis > bb.dis;
    }
};
priority_queue<node2> q;
void ins(int u, int v, int w)
{
    ++sum;
    a[sum].v = v;
    a[sum].w = w;
    a[sum].nxt = head[u];
    head[u] = sum;
}
void dijkstra()
{
    memset(dis, 0x7f, sizeof(dis));
    dis[0] = 0;
    q.push(node2(0, dis[0]));
    while (!q.empty())
    {
        int id = q.top().id;
        q.pop();
        if (id == st[g])
            break;
        if (vis[id])
            continue;
        vis[id] = 1;
        if (head[id])
            for (int d = head[id]; d; d = a[d].nxt)
                if (dis[id] + a[d].w < dis[a[d].v])
                {
                    dis[a[d].v] = dis[id] + a[d].w;
                    q.push(node2(a[d].v, dis[a[d].v]));
                }
    }
    return;
}
int main()
{
    cin >> n >> m >> s >> g;
    for (int i = 1; i <= m; ++i)
    {
        cin >> u[i] >> v[i] >> t[i] >> c[i];
        p[u[i]].push_back(pi(v[i], t[i]));
    }
    for (int i = 1; i <= n; ++i)
        sort(p[i].begin(), p[i].end());
    for (int i = 1; i <= n; ++i)
    {
        if (i == g)
        {
            st[i] = ed[i - 1] + 1;
            ed[i] = ed[i - 1] + 1;
            continue;
        }
        st[i] = ed[i - 1] + 1;
        ed[i] = max(st[i], st[i] + (int)p[i].size() - 1);
        if (p[i].size())
        {
            for (int j = 0; j < p[i].size() - 1; ++j)
            {
                p[i][j].id = j;
                ins(st[i] + j, st[i] + j + 1, p[i][j + 1].t - p[i][j].t);
            }
            p[i][p[i].size() - 1].id = p[i].size() - 1;
        }
    }
    ins(0, st[s], p[s][0].t);
    for (int i = 1; i <= m; ++i)
    {
        pi k = *(--upper_bound(p[u[i]].begin(), p[u[i]].end(), pi(0, t[i])));
        if (v[i] == g)
        {
            ins(st[u[i]] + k.id, st[v[i]], 0);
            continue;
        }
        if (lower_bound(p[v[i]].begin(), p[v[i]].end(), pi(0, t[i] + c[i])) != p[v[i]].end())
        {
            pi kk = *lower_bound(p[v[i]].begin(), p[v[i]].end(), pi(0, t[i] + c[i]));
            ins(st[u[i]] + k.id, st[v[i]] + kk.id, kk.t - t[i] - c[i]);
        }
    }
    dijkstra();
    cout << dis[st[g]] << endl;
    return 0;
}
accuracy: 0.6551724137931034
time_ms: 130
memory_kb: 19808
final_rank: 16

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

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

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;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}


template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S),
          F (*composition)(F, F), F (*id)()>
struct lazysegtree {
  private:
    int n, lg2, sz;
    std::vector<S> d;
    std::vector<F> lz;

    void update(int i) {
        d[i] = op(d[2 * i], d[2 * i + 1]);
    }

    void all_apply(int i, F f) {
        d[i] = mapping(f, d[i]);
        if (i < sz) lz[i] = composition(f, lz[i]);
    }

    void push(int i) {
        all_apply(2 * i, lz[i]);
        all_apply(2 * i + 1, lz[i]);
        lz[i] = id();
    }

  public:
    lazysegtree(int _n) : lazysegtree(std::vector<S>(_n, e())) {}

    lazysegtree(const std::vector<S> &v) : n(v.size()) {
        lg2 = 0;
        while ((1 << lg2) < n) lg2++;
        sz = 1 << lg2;
        d = std::vector<S>(2 * sz, e());
        lz = std::vector<F>(2 * sz, id());
        for (int i = 0; i < n; i++) d[sz + i] = v[i];
        for (int i = sz - 1; i >= 1; i--) update(i);
    }

    void set(int p, S x) {
        assert(0 <= p && p < n);
        p += sz;
        rrep(i, 1, lg2 + 1) push(p >> i);
        d[p] = x;
        rep(i, 1, lg2 + 1) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < n);
        p += sz;
        rrep(i, 1, lg2 + 1) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= n);
        if (l == r) return e();
        l += sz;
        r += sz;
        rrep(i, 1, lg2 + 1) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        S sml = e(), smr = e();
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }

        return op(sml, smr);
    }

    S all_prod() {
        return d[1];
    }

    void apply(int p, F f) {
        assert(0 <= p && p < n);
        p += sz;
        rrep(i, 1, lg2 + 1) push(p >> i);
        d[p] = mapping(f, d[p]);
        rep(i, 1, lg2 + 1) update(p >> i);
    }

    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= n);
        if (l == r) return;
        l += sz;
        r += sz;
        rrep(i, 1, lg2 + 1) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        rep(i, 1, lg2 + 1) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= n);
        assert(g(e()));
        if (l == n) return n;
        l += sz;
        for (int i = lg2; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < sz) {
                    push(l);
                    l = (2 * l);
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - sz;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return n;
    }

    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= n);
        assert(g(e()));
        if (r == 0) return 0;
        r += sz;
        for (int i = lg2; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < sz) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - sz;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }
};

using S = long long;
using F = long long;

const S INF = 1e18;

S op(S a, S b){ return std::min(a, b); }
S e(){ return INF; }
S mapping(F f, S x){ return f+x; }
F composition(F f, F g){ return f+g; }
F id(){ return 0; }

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


	int n, m; cin >> n >> m;
	int S, G; cin >> S >> G;
	S--; G--;

	
	vector<int> u(m), v(m);
	
	vector bucket(255555, vector<pair<int,int>>(0));
	rep(i,0,m){
		cin >> u[i] >> v[i];
		u[i]--; v[i]--;
		int t, c; cin >> t >> c;
		bucket[t].push_back(pair(0, i));
		bucket[t + c].push_back(pair(1, i));
	}

	lazysegtree<ll,op,e,ll,mapping,composition,id> seg(n);
	seg.set(S, 0);

	ll ans = 1e18;
	vector<ll> hokan(m);
	rep(tim,0,244444){
		sort(bucket[tim].begin(), bucket[tim].end());
		reverse(bucket[tim].begin(), bucket[tim].end());
		for (auto [typ,ind]: bucket[tim]){
			if (typ == 0){
				hokan[ind] = seg.get(u[ind]);				
			}else{
				seg.set(v[ind], min(seg.get(v[ind]), hokan[ind]));
			}
		}
		chmin(ans, seg.get(G));
		seg.apply(0, n, 1);
	}
	cout << ans << '\n';

}
accuracy: 1
time_ms: 120
memory_kb: 22820
final_rank: 3

code_snippet: //#include<atcoder/all>
//using namespace atcoder;

#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define eps (double)(1e-9)
#define pi (double) acos(-1)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

struct edge{
    ll to, cost;
};

using Graph = vector<vector<edge>>;

#define P pair<int, int>
vector<ll> dijkstra(const vector<vector<edge>> &G, int s) {
    vector<ll> d((int)G.size(), inf);
	priority_queue<P, vector<P>, greater<P>>que;
	d[s] = 0;
	que.push(P(0, s));
	while (!que.empty()) {
		P p = que.top(); que.pop();
		int v = p.second;
		if (d[v] < p.first)continue;
		rep(i, G[v].size()) {
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost) {
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
	return d;
}

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

    vector<int> u(m), v(m), t(m), c(m);
    rep(i, m)cin >> u[i] >> v[i] >> t[i] >> c[i];
    rep(i, m){u[i]--; v[i]--;}

    vector<vector<int>> vec(n);
    rep(i, m)vec[u[i]].push_back(t[i]);
    rep(i, m)vec[v[i]].push_back(t[i]+c[i]);
    vec[s].insert(vec[s].begin(), 0);
    rep(i, n)sort(all(vec[i]));

    int now = 0;
    map<P, int> idx;
    rep(i, n)rep(j, (int)vec[i].size()){
        idx[{i, vec[i][j]}] = now;
        now++;
    }

    idx[{s, 0}] = now;
    now++;

    Graph G(now);

    rep(i, m){
        G[idx[{u[i], t[i]}]].push_back({idx[{v[i], t[i]+c[i]}], 0});
    }

    rep(i, n)rep(j, (int)vec[i].size()-1){
        G[idx[{i, vec[i][j]}]].push_back({idx[{i, vec[i][j+1]}], vec[i][j+1]-vec[i][j]});
    }

    ll ans = inf;
    vector<ll> d = dijkstra(G, idx[{s, 0}]);
    rep(i, (int)vec[g].size())chmin(ans, d[idx[{g, vec[g][i]}]]);

    cout << ans << endl;
}
accuracy: 1
time_ms: 630
memory_kb: 64064
final_rank: 11

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
int n, m, s, g;
int u[200005], v[200005], t[200005], c[200005];
int st[100005], ed[100005];
int sum;
int head[200005], dis[200005], vis[200005];
struct pi
{
    int v, t, c, id;
    pi(int aa = 0, int bb = 0, int cc = 0)
    {
        v = aa;
        t = bb;
        c = cc;
    }
    bool operator<(const pi &bb) const
    {
        return t < bb.t;
    }
};
vector<pi> p[100005];
struct node
{
    int v, w, nxt;
} a[300005];
struct node2
{
    int id, dis;
    node2(int aa = 0, int bb = 0)
    {
        id = aa;
        dis = bb;
    }
    bool operator<(const node2 &bb) const
    {
        return dis > bb.dis;
    }
};
priority_queue<node2> q;
void ins(int u, int v, int w)
{
    ++sum;
    a[sum].v = v;
    a[sum].w = w;
    a[sum].nxt = head[u];
    head[u] = sum;
}
void dijkstra()
{
    memset(dis, 0x7f, sizeof(dis));
    dis[0] = 0;
    q.push(node2(0, dis[0]));
    while (!q.empty())
    {
        int id = q.top().id;
        q.pop();
        if (vis[id])
            continue;
        vis[id] = 1;
        for (int d = head[id]; d; d = a[d].nxt)
            if (dis[id] + a[d].w < dis[a[d].v])
            {
                dis[a[d].v] = dis[id] + a[d].w;
                q.push(node2(a[d].v, dis[a[d].v]));
            }
    }
    return;
}
int main()
{
    cin >> n >> m >> s >> g;
    for (int i = 1; i <= m; ++i)
    {
        cin >> u[i] >> v[i] >> t[i] >> c[i];
        p[u[i]].push_back(pi(v[i], t[i], c[i]));
    }
    for (int i = 1; i <= n; ++i)
        sort(p[i].begin(), p[i].end());
    for (int i = 1; i <= n; ++i)
    {
        if (i == g)
        {
            st[i] = ed[i - 1] + 1;
            ed[i] = ed[i - 1] + 1;
            continue;
        }
        if (p[i].size())
        {
            st[i] = ed[i - 1] + 1;
            ed[i] = st[i] + p[i].size() - 1;
            for (int j = 0; j < p[i].size() - 1; ++j)
            {
                p[i][j].id = j;
                ins(st[i] + j, st[i] + j + 1, p[i][j + 1].t - p[i][j].t);
            }
            p[i][p[i].size() - 1].id = p[i].size() - 1;
        }
    }
    ins(0, st[s], p[s][0].t);
    for (int i = 1; i <= m; ++i)
    {
        pi k = *lower_bound(p[u[i]].begin(), p[u[i]].end(), pi(0, t[i], 0));
        if (v[i] == g)
        {
            ins(st[u[i]] + k.id, st[v[i]], 0);
            continue;
        }
        if (lower_bound(p[v[i]].begin(), p[v[i]].end(), pi(0, t[i] + c[i], 0)) != p[v[i]].end())
        {
            pi kk = *lower_bound(p[v[i]].begin(), p[v[i]].end(), pi(0, t[i] + c[i], 0));
            ins(st[u[i]] + k.id, st[v[i]] + kk.id, kk.t - t[i] - c[i]);
        }
    }
    dijkstra();
    cout << dis[st[g]] << endl;
    return 0;
}
accuracy: 0.3103448275862069
time_ms: 90
memory_kb: 17736
final_rank: 20

code_snippet: #line 2 "/Users/noya2/Desktop/Noya2_library/template/template.hpp"
using namespace std;

#include<bits/stdc++.h>
#line 1 "/Users/noya2/Desktop/Noya2_library/template/inout_old.hpp"
namespace noya2 {

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

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
    int s = (int)v.size();
    for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i];
    return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v){
    for (auto &x : v) is >> x;
    return is;
}

void in() {}
template <typename T, class... U>
void in(T &t, U &...u){
    cin >> t;
    in(u...);
}

void out() { cout << "\n"; }
template <typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u){
    cout << t;
    if (sizeof...(u)) cout << sep;
    out(u...);
}

template<typename T>
void out(const vector<vector<T>> &vv){
    int s = (int)vv.size();
    for (int i = 0; i < s; i++) out(vv[i]);
}

struct IoSetup {
    IoSetup(){
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
        cerr << fixed << setprecision(7);
    }
} iosetup_noya2;

} // namespace noya2
#line 1 "/Users/noya2/Desktop/Noya2_library/template/const.hpp"
namespace noya2{

const int iinf = 1'000'000'007;
const long long linf = 2'000'000'000'000'000'000LL;
const long long mod998 =  998244353;
const long long mod107 = 1000000007;
const long double pi = 3.14159265358979323;
const vector<int> dx = {0,1,0,-1,1,1,-1,-1};
const vector<int> dy = {1,0,-1,0,1,-1,-1,1};
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string NUM = "0123456789";

void yes(){ cout << "Yes\n"; }
void no(){ cout << "No\n"; }
void YES(){ cout << "YES\n"; }
void NO(){ cout << "NO\n"; }
void yn(bool t){ t ? yes() : no(); }
void YN(bool t){ t ? YES() : NO(); }

} // namespace noya2
#line 1 "/Users/noya2/Desktop/Noya2_library/template/utils.hpp"
namespace noya2{

unsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){
    if (a == 0 || b == 0) return a + b;
    int n = __builtin_ctzll(a); a >>= n;
    int m = __builtin_ctzll(b); b >>= m;
    while (a != b) {
        int mm = __builtin_ctzll(a - b);
        bool f = a > b;
        unsigned long long c = f ? a : b;
        b = f ? b : a;
        a = (c - b) >> mm;
    }
    return a << min(n, m);
}

template<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(abs(a),abs(b))); }

long long sqrt_fast(long long n) {
    if (n <= 0) return 0;
    long long x = sqrt(n);
    while ((x + 1) * (x + 1) <= n) x++;
    while (x * x > n) x--;
    return x;
}

template<typename T> T floor_div(const T n, const T d) {
    assert(d != 0);
    return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);
}

template<typename T> T ceil_div(const T n, const T d) {
    assert(d != 0);
    return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);
}

template<typename T> void uniq(vector<T> &v){
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}

template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }

template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

template<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }

} // namespace noya2
#line 8 "/Users/noya2/Desktop/Noya2_library/template/template.hpp"

#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)
#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;

namespace noya2{

/*　~ (. _________ . /)　*/

}

using namespace noya2;


#line 2 "c.cpp"


void solve(){
    int n, m, sv, gv; in(n,m,sv,gv); sv--, gv--;
    vector<int> from(m), to(m), ts(m), cs(m);
    rep(i,m){
        int u, v; in(u,v); u--, v--;
        int t, c; in(t,c);
        from[i] = u;
        to[i] = v;
        ts[i] = t;
        cs[i] = c;
    }
    vector<int> ids(m); iota(all(ids),0);
    sort(all(ids),[&](int l, int r){
        return ts[l] < ts[r];
    });
    vector<int> dp(n,-iinf);
    dp[sv] = 0;
    priority_queue<pii,vector<pii>,greater<pii>> pque;
    vector<int> cal(m,-iinf);
    int ans = iinf;
    for (int i : ids){
        while (!pque.empty() && pque.top().first <= ts[i]){
            auto [t, id] = pque.top(); pque.pop();
            chmax(dp[to[id]],cal[id]);
        }
        cal[i] = dp[from[i]] + cs[i];
        pque.push(pii(ts[i]+cs[i],i));
        if (to[i] == gv){
            chmin(ans,ts[i]+cs[i]-cal[i]);
        }
    }
    out(ans);
}

int main(){
    int t = 1; //in(t);
    while (t--) { solve(); }
}
accuracy: 1
time_ms: 60
memory_kb: 9088
final_rank: 1

code_snippet: #line 2 "/Users/noya2/Desktop/Noya2_library/template/template.hpp"
using namespace std;

#include<bits/stdc++.h>
#line 1 "/Users/noya2/Desktop/Noya2_library/template/inout_old.hpp"
namespace noya2 {

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

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
    int s = (int)v.size();
    for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i];
    return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v){
    for (auto &x : v) is >> x;
    return is;
}

void in() {}
template <typename T, class... U>
void in(T &t, U &...u){
    cin >> t;
    in(u...);
}

void out() { cout << "\n"; }
template <typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u){
    cout << t;
    if (sizeof...(u)) cout << sep;
    out(u...);
}

template<typename T>
void out(const vector<vector<T>> &vv){
    int s = (int)vv.size();
    for (int i = 0; i < s; i++) out(vv[i]);
}

struct IoSetup {
    IoSetup(){
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
        cerr << fixed << setprecision(7);
    }
} iosetup_noya2;

} // namespace noya2
#line 1 "/Users/noya2/Desktop/Noya2_library/template/const.hpp"
namespace noya2{

const int iinf = 1'000'000'007;
const long long linf = 2'000'000'000'000'000'000LL;
const long long mod998 =  998244353;
const long long mod107 = 1000000007;
const long double pi = 3.14159265358979323;
const vector<int> dx = {0,1,0,-1,1,1,-1,-1};
const vector<int> dy = {1,0,-1,0,1,-1,-1,1};
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string NUM = "0123456789";

void yes(){ cout << "Yes\n"; }
void no(){ cout << "No\n"; }
void YES(){ cout << "YES\n"; }
void NO(){ cout << "NO\n"; }
void yn(bool t){ t ? yes() : no(); }
void YN(bool t){ t ? YES() : NO(); }

} // namespace noya2
#line 1 "/Users/noya2/Desktop/Noya2_library/template/utils.hpp"
namespace noya2{

unsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){
    if (a == 0 || b == 0) return a + b;
    int n = __builtin_ctzll(a); a >>= n;
    int m = __builtin_ctzll(b); b >>= m;
    while (a != b) {
        int mm = __builtin_ctzll(a - b);
        bool f = a > b;
        unsigned long long c = f ? a : b;
        b = f ? b : a;
        a = (c - b) >> mm;
    }
    return a << min(n, m);
}

template<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(abs(a),abs(b))); }

long long sqrt_fast(long long n) {
    if (n <= 0) return 0;
    long long x = sqrt(n);
    while ((x + 1) * (x + 1) <= n) x++;
    while (x * x > n) x--;
    return x;
}

template<typename T> T floor_div(const T n, const T d) {
    assert(d != 0);
    return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);
}

template<typename T> T ceil_div(const T n, const T d) {
    assert(d != 0);
    return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);
}

template<typename T> void uniq(vector<T> &v){
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}

template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }

template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

template<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }

} // namespace noya2
#line 8 "/Users/noya2/Desktop/Noya2_library/template/template.hpp"

#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)
#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;

namespace noya2{

/*　~ (. _________ . /)　*/

}

using namespace noya2;


#line 2 "c.cpp"


void solve(){
    int n, m, sv, gv; in(n,m,sv,gv); sv--, gv--;
    vector<vector<array<int,3>>> g(n);
    rep(tt,m){
        int u, v; in(u,v); u--, v--;
        int t, c; in(t,c);
        g[u].push_back({v,t,c});
        g[v].push_back({u,t,c});
    }
    using ppi = pair<pii,int>;
    priority_queue<ppi,vector<ppi>,greater<ppi>> pque;
    vector<pii> dist(n,pii(iinf,iinf));
    dist[sv] = pii(0,0);
    pque.push(ppi(dist[sv],sv));
    while (!pque.empty()){
        auto [dd, v] = pque.top(); pque.pop();
        if (dist[v] < dd) continue;
        auto [val, cur] = dd;
        int sum = cur - val;
        for (auto [u, t, c] : g[v]){
            if (cur > t) continue;
            int nval = t+c - (sum+c);
            int ncur = t+c;
            if (chmin(dist[u],pii(nval,ncur))){
                pque.push(ppi(dist[u],u));
            }
        }
    }
    out(dist[gv].first);
}

int main(){
    int t = 1; //in(t);
    while (t--) { solve(); }
}
accuracy: 0.3103448275862069
time_ms: 40
memory_kb: 12272
final_rank: 19

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 2811.cc: Rainy Bus Stops
 */

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

/* constant */

const int MAX_N = 100000;
const int MAX_M = 200000;
const int MAX_K = MAX_M * 2 + 1;
const long long LINF = 1LL << 60;

/* typedef */

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<pii> vpii;
typedef tuple<int,int,int,int> quadi;

/* global variables */

quadi es[MAX_M];
pii ps[MAX_K];
vpii nbrs[MAX_K];
ll ds[MAX_K];

/* subroutines */

/* main */

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

  int k = 0;
  ps[k++] = pii(st, 0);
  for (int i = 0; i < m; i++) {
    int u, v, t, c;
    scanf("%d%d%d%d", &u, &v, &t, &c);
    es[i] = { u, v, t, c };
    ps[k++] = pii(u, t), ps[k++] = pii(v, t + c);
  }

  sort(ps, ps + k);
  k = unique(ps, ps + k) - ps;

  for (int i = 1; i < k; i++)
    if (ps[i - 1].first == ps[i].first)
      nbrs[i - 1].push_back(pii(i, ps[i].second - ps[i - 1].second));

  for (int i = 0; i < m; i++) {
    auto [ u, v, t, c ] = es[i];
    int ui = lower_bound(ps, ps + k, pii(u, t)) - ps;
    int vi = lower_bound(ps, ps + k, pii(v, t + c)) - ps;
    nbrs[ui].push_back(pii(vi, 0));
  }

  int pst = lower_bound(ps, ps + k, pii(st, 0)) - ps;
  
  fill(ds, ds + k, LINF);
  ds[pst] = 0;
  
  priority_queue<pli> q;
  q.push(pli(0, pst));

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

    for (auto &vw: nbrs[u]) {
      int v = vw.first;
      ll vd = ud + vw.second;
      if (ds[v] > vd) {
	ds[v] = vd;
	q.push(pli(-vd, v));
      }
    }
  }

  ll mind = LINF;
  for (int i = 0; i < k; i++)
    if (ps[i].first == gl) mind = min(mind, ds[i]);

  printf("%lld\n", mind);

  return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 34136
final_rank: 10

code_snippet: #pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
const vector<int> dy={-1,0,1,0},dx={0,-1,0,1};
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
struct edge{
      int to,t;
      ll cost;
      edge(int to,int t,ll cost):to(to),t(t),cost(cost){}
};
struct edge2{
      int to,id;
      ll cost;
      edge2(int to,int id,ll cost):to(to),id(id),cost(cost){}
};
using tp=tuple<ll,int,int>;
int main(){
      int n,m,S,G;
      cin>>n>>m>>S>>G;
      --S;--G;
      V<V<edge>> g(n);
      V<V<int>> tmp(n);
      for(int i=0;i<m;i++){
            ll u,v,t,c;
            cin>>u>>v>>t>>c;
            --u;--v;
            g[u].emplace_back(v,t,c);
            tmp[u].emplace_back(t);
      }
      V<V<ll>> dp(n);
      V<V<V<edge2>>> e(n);
      for(int i=0;i<n;i++){
            sort(all(g[i]),[](auto a,auto b){
                  if(a.t!=b.t)return a.t<b.t;
                  return a.cost<b.cost;
            });
            dp[i].assign(g[i].size()+1,inf);
            e[i].resize(g[i].size()+1);
            sort(all(tmp[i]));
      }
      for(int i=0;i<n;i++){
            int sz=g[i].size();
            for(int j=0;j<sz;j++){
                  edge cp=g[i][j];
                  if(j!=sz-1){
                        edge cp2=g[i][j+1];
                       e[i][j].emplace_back(i,j+1,cp2.t-cp.t);
                  }
                  if(cp.to==G){
                        e[i][j].emplace_back(cp.to,0,0);
                  }else{
                        auto ite=lower_bound(all(tmp[cp.to]),cp.t+cp.cost);
                        if(ite!=tmp[cp.to].end()){
                              e[i][j].emplace_back(cp.to,(int)(ite-tmp[cp.to].begin()),*ite-(cp.t+cp.cost));
                        }
                  }
            }
      }
      priority_queue<tp,V<tp>,greater<tp>> pq;
      pq.emplace(0,S,0);
      dp[S][0]=g[S][0].t;
      while(pq.size()){
            auto [v,cur,id]=pq.top();
            pq.pop();
            if(dp[cur][id]<v)continue;
            for(edge2 &to:e[cur][id]){
                  if(chmin(dp[to.to][to.id],dp[cur][id]+to.cost)){
                        pq.emplace(dp[to.to][to.id],to.to,to.id);
                  }
            }
      }
      cout<<dp[G][0]<<"\n";
}
accuracy: 1
time_ms: 120
memory_kb: 39180
final_rank: 9

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
using PII = pair<ll, ll>;
#define FOR(i,a,n) for(ll i=(ll)a; i<(ll)n; ++i)
#define REP(i,n) FOR(i,0,n)

struct Edge {
	int to, sttime, cost, index;
	Edge(const int t, const int s, const int c, const int i) {
		to = t, sttime = s, cost = c, index = i;
		return;
	}
	bool operator<(const Edge&e)const {
		return sttime < e.sttime;
	}
};

int main() {
	int N, M, S, G;
	cin >> N >> M >> S >> G;
	S--, G--;
	vector<vector<Edge>>edge(N);
	for (int i = 0; i < M; i++) {
		int l, r, s, t;
		cin >> l >> r >> s >> t;
		l--, r--;
		edge[l].push_back(Edge(r, s, t, i));
	}
//cout << "JHIJHI" << endl;
	for (int i = 0; i < N; i++)sort(edge[i].begin(), edge[i].end());
	vector<vector<pair<int, int>>>dis_edge(M + 1);
//	cout << "GUUG" << endl;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < (int)(edge[i].size()) - 1; j++) {
			dis_edge[edge[i][j].index].push_back({ edge[i][j + 1].index,edge[i][j + 1].sttime - edge[i][j].sttime });
		}
		vector<pair<int, int>>box;
		for (auto j : edge[i]) {
			if (j.to == G) {
				dis_edge[j.index].push_back({ M,0 });
				continue;
			}
			if (edge[j.to].empty())continue;
			int L = -1, R = edge[j.to].size();
			while (R - L > 1) {
				int mid = (R + L) / 2;
				if (edge[j.to][mid].sttime >= j.sttime + j.cost)R = mid;
				else L = mid;
			}
			if (R == edge[j.to].size())continue;
			dis_edge[j.index].push_back({ edge[j.to][R].index,edge[j.to][R].sttime - j.cost - j.sttime });
		}
	}
	//cout << "Ho" << endl;
	vector<int>dis(M+1, MOD);
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>PQ;
	for (auto i : edge[S]) {
		dis[i.index] = i.sttime;
		PQ.push({ i.sttime,i.index });
	}
	while (!PQ.empty()) {
		int cn = PQ.top().second;
		int c = PQ.top().first;
		PQ.pop();
		for (auto j : dis_edge[cn]) {
			if (dis[j.first] > c + j.second) {
				dis[j.first] = c + j.second;
				PQ.push({ dis[j.first],j.first });
			}
		}
	}
	cout << dis[M] << endl;
}
accuracy: 1
time_ms: 220
memory_kb: 21312
final_rank: 7

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
using PII = pair<ll, ll>;
#define FOR(i,a,n) for(ll i=(ll)a; i<(ll)n; ++i)
#define REP(i,n) FOR(i,0,n)

struct Edge {
	int to, sttime, cost, index;
	Edge(const int t, const int s, const int c, const int i) {
		to = t, sttime = s, cost = c, index = i;
		return;
	}
	bool operator<(const Edge&e)const {
		return sttime < e.sttime;
	}
};

int main() {
	int N, M, S, G;
	cin >> N >> M >> S >> G;
	S--, G--;
	vector<vector<Edge>>edge(N);
	for (int i = 0; i < M; i++) {
		int l, r, s, t;
		cin >> l >> r >> s >> t;
		l--, r--;
		edge[l].push_back(Edge(r, s, t, i));
	}
//cout << "JHIJHI" << endl;
	for (int i = 0; i < N; i++)sort(edge[i].begin(), edge[i].end());
	vector<vector<pair<int, int>>>dis_edge(M + 1);
//	cout << "GUUG" << endl;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < (int)(edge[i].size()) - 1; j++) {
			dis_edge[edge[i][j].index].push_back({ edge[i][j + 1].index,edge[i][j + 1].sttime - edge[i][j].sttime });
		}
		vector<pair<int, int>>box;
		for (auto j : edge[i]) {
			if (j.to == G) {
				dis_edge[j.index].push_back({ M,0 });
				continue;
			}
			if (edge[j.to].empty())continue;
			int L = -1, R = edge[j.to].size();
			while (R - L > 1) {
				int mid = (R + L) / 2;
				if (edge[j.to][mid].sttime >= j.sttime + j.cost)R = mid;
				else L = mid;
			}
			if (R == edge[j.to].size())continue;
			dis_edge[j.index].push_back({ edge[j.to][R].index,edge[j.to][R].sttime - j.cost - j.sttime });
		}
	}
	//cout << "Ho" << endl;
	vector<int>dis(M+1, MOD);
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>PQ;
	for (auto i : edge[S]) {
		dis[i.index] = i.sttime;
		PQ.push({ i.sttime,i.index });
	}
	while (!PQ.empty()) {
		int cn = PQ.top().second;
		int c = PQ.top().first;
		PQ.pop();
		for (auto j : dis_edge[cn]) {
			if (dis[j.first] > c + j.second) {
				dis[j.first] = c + j.second;
				PQ.push({ dis[j.first],j.first });
			}
		}
	}
	cout << dis[M] << endl;
}
accuracy: 1
time_ms: 220
memory_kb: 21236
final_rank: 6

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define HUGE_NUM 9999999999999999

struct Info{
	Info(int arg_to,ll arg_time,ll arg_cost,ll arg_min_rain){
		to = arg_to;
		time = arg_time;
		cost = arg_cost;
		min_rain = arg_min_rain;
	}
	int to;
	ll time,cost,min_rain;
};

struct Data{
	Data(int arg_node_id,int arg_pre_node,int arg_pre_id,ll arg_current_time,ll arg_rain_time){
		node_id = arg_node_id;
		pre_node = arg_pre_node;
		pre_id = arg_pre_id;
		current_time = arg_current_time;
		rain_time = arg_rain_time;
	}
	bool operator<(const struct Data &arg) const{
		return rain_time > arg.rain_time;
	}
	int node_id,pre_node,pre_id;
	ll current_time,rain_time;
};

int N,M,start,goal;
vector<Info> G[100000];

int main(){

	scanf("%d %d %d %d",&N,&M,&start,&goal);
	start--;
	goal--;

	int from,to;
	ll time,cost;

	for(int loop = 0; loop < M; loop++){
		scanf("%d %d %lld %lld",&from,&to,&time,&cost);
		from--;
		to--;
		G[from].push_back(Info(to,time,cost,HUGE_NUM));
	}

	priority_queue<Data> Q;
	for(int i = 0; i < G[start].size(); i++){
		G[start][i].min_rain = G[start][i].time;
		Q.push(Data(G[start][i].to,start,i,G[start][i].time+G[start][i].cost,G[start][i].time));
	}

	while(!Q.empty()){

		if(Q.top().node_id == goal){
			printf("%lld\n",Q.top().rain_time);
			return 0;
		}else if(Q.top().rain_time > G[Q.top().pre_node][Q.top().pre_id].min_rain){
			Q.pop();
		}else{

			for(int i = 0; i < G[Q.top().node_id].size(); i++){
				if(Q.top().current_time > G[Q.top().node_id][i].time)continue;
				if(Q.top().rain_time+G[Q.top().node_id][i].time-Q.top().current_time >= G[Q.top().node_id][i].min_rain)continue;

				G[Q.top().node_id][i].min_rain = Q.top().rain_time+G[Q.top().node_id][i].time-Q.top().current_time;
				Q.push(Data(G[Q.top().node_id][i].to,Q.top().node_id,i,G[Q.top().node_id][i].time+G[Q.top().node_id][i].cost,G[Q.top().node_id][i].min_rain));
			}
			Q.pop();
		}
	}

	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 17596
final_rank: 2

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

using namespace std;

const bool debug = true;
#define dbg(...) if(debug) printf(__VA_ARGS__)
#define print(var) if (debug) cout << #var << " = " << var << endl

namespace {
    /** output whole vector. ex) vector<int>{1, 2, 3} -> '1 2 3'. */
    template<typename T>
    ostream& operator<<(ostream& os, const vector<T>& xs) {
       if (xs.empty()) return os << "[]";
       os << xs[0];
       for (auto i = 1; i < xs.size(); i++) os << ' ' << xs[i];
       return os;
    }
    template<typename K, typename V>
    ostream& operator<<(ostream& os, const pair<K, V>& p) {
        return os << "(" << p.first << "," << p.second << ")";
    }
    template<typename K, typename V>
    ostream& operator<<(ostream& os, const map<K, V>& m) {
        bool first = true;
        for (auto p : m) {
            if (first) first = false;
            else os << ":";
            os << "(" << p.first << "," << p.second << ")";
        }
        return os;
    }


    map<tuple<int, int, bool>, int> id;
    vector<tuple<int, int, bool>> fromId;

    struct Edge {
        int to, cost;
        Edge(int to, int cost) : to(to), cost(cost) {}
    };
    vector<vector<Edge>> Graph;

    int N, M, S, G;

    bool IN = false;
    bool OUT = true;

    void solve() {
        cin >> N >> M >> S >> G;
        S--; G--;
        vector<vector<tuple<int, int, bool>>> V(N);
        auto init = make_tuple(S, 0, IN);
        V[S].push_back(init);
        id[init] = 0;
        fromId.push_back(init);
        Graph.emplace_back();
        for (int  i = 0; i < M; i++) {
            int u, v, t, c; cin >> u >> v >> t >> c;
            u--; v--;
            auto x = make_tuple(u, t, OUT);
            auto x_id = id.count(x) ? id[x] : fromId.size();
            if (not id.count(x)) {
                fromId.push_back(x);
                id[x] = x_id;
                Graph.emplace_back();
            }
            V[u].push_back(x);
            auto y = make_tuple(v, t + c, IN);
            auto y_id = id.count(y) ? id[y] : fromId.size();
            if (not id.count(y)) {
                fromId.push_back(y);
                id[y] = y_id;
                Graph.emplace_back();
            }
            V[v].push_back(y);
            Graph[x_id].emplace_back(y_id, 0);
        }
        for (auto& L : V) {
            sort(L.begin(), L.end(), [&](const tuple<int, int, bool>& a, const tuple<int, int, bool>& b) {
                return get<1>(a) == get<1>(b) ? get<2>(a) < get<2>(b) : get<1>(a) < get<1>(b);
            });
            for (unsigned i = 0; i + 1 < L.size(); i++) {
                auto& x = L[i];
                auto& y = L[i + 1];
                auto time_x = get<1>(x);
                auto time_y = get<1>(y);
                Graph[id[x]].emplace_back(id[y], time_y - time_x);
            }
        }

        auto start = id[ V[S][0] ];

        // dijkstra
        const int INF = 1<<28;
        vector<int> D(Graph.size(), INF);
        struct State {
            int v, cost;
            State(int v, int cost) : v(v), cost(cost) {}
            bool operator<(const State& s) const {
                return cost > s.cost;
            }
        };
        priority_queue<State> PQ;
        PQ.emplace(start, 0);
        D[start] = 0;
        while (not PQ.empty()) {
            auto cur = PQ.top(); PQ.pop();
            for (auto& e : Graph[cur.v]) {
                auto next = e.to;
                if (D[next] > cur.cost + e.cost) {
                    D[next] = cur.cost + e.cost;
                    PQ.emplace(next, D[next]);
                }
            }
        }
        int ans = INF;
        for (auto& goal_vertex : V[G]) {
            auto goal = id[goal_vertex];
            ans = min(ans, D[goal]);
        }
        cout << ans << endl;
    }
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 670
memory_kb: 64968
final_rank: 13

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

using namespace std;

const bool debug = true;
#define dbg(...) if(debug) printf(__VA_ARGS__)
#define print(var) if (debug) cout << #var << " = " << var << endl

namespace {
    /** output whole vector. ex) vector<int>{1, 2, 3} -> '1 2 3'. */
    template<typename T>
    ostream& operator<<(ostream& os, const vector<T>& xs) {
       if (xs.empty()) return os << "[]";
       os << xs[0];
       for (auto i = 1; i < xs.size(); i++) os << ' ' << xs[i];
       return os;
    }
    template<typename K, typename V>
    ostream& operator<<(ostream& os, const pair<K, V>& p) {
        return os << "(" << p.first << "," << p.second << ")";
    }
    template<typename K, typename V>
    ostream& operator<<(ostream& os, const map<K, V>& m) {
        bool first = true;
        for (auto p : m) {
            if (first) first = false;
            else os << ":";
            os << "(" << p.first << "," << p.second << ")";
        }
        return os;
    }


    map<tuple<int, int, bool>, int> id;
    vector<tuple<int, int, bool>> fromId;

    struct Edge {
        int to, cost;
        Edge(int to, int cost) : to(to), cost(cost) {}
    };
    vector<vector<Edge>> Graph;

    int N, M, S, G;

    bool IN = false;
    bool OUT = true;

    void solve() {
        cin >> N >> M >> S >> G;
        S--; G--;
        vector<vector<tuple<int, int, bool>>> V(N);
        auto init = make_tuple(S, 0, IN);
        V[S].push_back(init);
        id[init] = 0;
        fromId.push_back(init);
        Graph.emplace_back();
        for (int  i = 0; i < M; i++) {
            int u, v, t, c; cin >> u >> v >> t >> c;
            u--; v--;
            auto x = make_tuple(u, t, OUT);
            auto x_id = id.count(x) ? id[x] : fromId.size();
            if (not id.count(x)) {
                fromId.push_back(x);
                id[x] = x_id;
                Graph.emplace_back();
            }
            V[u].push_back(x);
            auto y = make_tuple(v, t + c, IN);
            auto y_id = id.count(y) ? id[y] : fromId.size();
            if (not id.count(y)) {
                fromId.push_back(y);
                id[y] = y_id;
                Graph.emplace_back();
                Graph[x_id].emplace_back(y_id, 0);
            }
            V[v].push_back(y);
        }
        for (auto& L : V) {
            sort(L.begin(), L.end(), [&](const tuple<int, int, bool>& a, const tuple<int, int, bool>& b) {
                return get<1>(a) == get<1>(b) ? get<2>(a) < get<2>(b) : get<1>(a) < get<1>(b);
            });
            for (unsigned i = 0; i + 1 < L.size(); i++) {
                auto& x = L[i];
                auto& y = L[i + 1];
                auto time_x = get<1>(x);
                auto time_y = get<1>(y);
                Graph[id[x]].emplace_back(id[y], time_y - time_x);
            }
        }

        auto start = id[ V[S][0] ];

        // dijkstra
        const int INF = 1<<28;
        vector<int> D(Graph.size(), INF);
        struct State {
            int v, cost;
            State(int v, int cost) : v(v), cost(cost) {}
            bool operator<(const State& s) const {
                return cost > s.cost;
            }
        };
        priority_queue<State> PQ;
        PQ.emplace(start, 0);
        D[start] = 0;
        while (not PQ.empty()) {
            auto cur = PQ.top(); PQ.pop();
            for (auto& e : Graph[cur.v]) {
                auto next = e.to;
                if (D[next] > cur.cost + e.cost) {
                    D[next] = cur.cost + e.cost;
                    PQ.emplace(next, D[next]);
                }
            }
        }
        int ans = INF;
        for (auto& goal_vertex : V[G]) {
            auto goal = id[goal_vertex];
            ans = min(ans, D[goal]);
        }
        cout << ans << endl;
    }
}

int main() {
    solve();
    return 0;
}
accuracy: 0.7586206896551724
time_ms: 680
memory_kb: 65112
final_rank: 15

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

using namespace std;

const bool debug = true;
#define dbg(...) if(debug) printf(__VA_ARGS__)
#define print(var) if (debug) cout << #var << " = " << var << endl

namespace {
    /** output whole vector. ex) vector<int>{1, 2, 3} -> '1 2 3'. */
    template<typename T>
    ostream& operator<<(ostream& os, const vector<T>& xs) {
       if (xs.empty()) return os << "[]";
       os << xs[0];
       for (auto i = 1; i < xs.size(); i++) os << ' ' << xs[i];
       return os;
    }
    template<typename K, typename V>
    ostream& operator<<(ostream& os, const pair<K, V>& p) {
        return os << "(" << p.first << "," << p.second << ")";
    }
    template<typename K, typename V>
    ostream& operator<<(ostream& os, const map<K, V>& m) {
        bool first = true;
        for (auto p : m) {
            if (first) first = false;
            else os << ":";
            os << "(" << p.first << "," << p.second << ")";
        }
        return os;
    }


    map<tuple<int, int, bool>, int> id;
    vector<tuple<int, int, bool>> fromId;

    struct Edge {
        int to, cost;
        Edge(int to, int cost) : to(to), cost(cost) {}
    };
    vector<vector<Edge>> Graph;

    int N, M, S, G;

    bool IN = false;
    bool OUT = true;

    void solve() {
        cin >> N >> M >> S >> G;
        S--; G--;
        vector<vector<tuple<int, int, bool>>> V(N);
        auto init = make_tuple(S, 0, IN);
        V[S].push_back(init);
        id[init] = 0;
        fromId.push_back(init);
        Graph.emplace_back();
        for (int  i = 0; i < M; i++) {
            int u, v, t, c; cin >> u >> v >> t >> c;
            u--; v--;
            auto x = make_tuple(u, t, OUT);
            auto x_id = id.count(x) ? id[x] : fromId.size();
            if (not id.count(x)) {
                fromId.push_back(x);
                id[x] = x_id;
                Graph.emplace_back();
            }
            V[u].push_back(x);
            auto y = make_tuple(v, t + c, IN);
            auto y_id = id.count(y) ? id[y] : fromId.size();
            if (not id.count(y)) {
                fromId.push_back(y);
                id[y] = y_id;
                Graph.emplace_back();
                V[v].push_back(y);
                Graph[x_id].emplace_back(y_id, 0);
            }
        }
        for (auto& L : V) {
            sort(L.begin(), L.end(), [&](const tuple<int, int, bool>& a, const tuple<int, int, bool>& b) {
                return get<1>(a) == get<1>(b) ? get<2>(a) < get<2>(b) : get<1>(a) < get<1>(b);
            });
            for (unsigned i = 0; i + 1 < L.size(); i++) {
                auto& x = L[i];
                auto& y = L[i + 1];
                auto time_x = get<1>(x);
                auto time_y = get<1>(y);
                Graph[id[x]].emplace_back(id[y], time_y - time_x);
            }
        }

        auto start = id[ V[S][0] ];

        // dijkstra
        const int INF = 1<<28;
        vector<int> D(Graph.size(), INF);
        struct State {
            int v, cost;
            State(int v, int cost) : v(v), cost(cost) {}
            bool operator<(const State& s) const {
                return cost > s.cost;
            }
        };
        priority_queue<State> PQ;
        PQ.emplace(start, 0);
        D[start] = 0;
        while (not PQ.empty()) {
            auto cur = PQ.top(); PQ.pop();
            for (auto& e : Graph[cur.v]) {
                auto next = e.to;
                if (D[next] > cur.cost + e.cost) {
                    D[next] = cur.cost + e.cost;
                    PQ.emplace(next, D[next]);
                }
            }
        }
        int ans = INF;
        for (auto& goal_vertex : V[G]) {
            auto goal = id[goal_vertex];
            ans = min(ans, D[goal]);
        }
        cout << ans << endl;
    }
}

int main() {
    solve();
    return 0;
}
accuracy: 0.7586206896551724
time_ms: 680
memory_kb: 64980
final_rank: 14

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

using namespace std;

const bool debug = true;
#define dbg(...) if(debug) printf(__VA_ARGS__)
#define print(var) if (debug) cout << #var << " = " << var << endl

namespace {
    /** output whole vector. ex) vector<int>{1, 2, 3} -> '1 2 3'. */
    template<typename T>
    ostream& operator<<(ostream& os, const vector<T>& xs) {
       if (xs.empty()) return os << "[]";
       os << xs[0];
       for (auto i = 1; i < xs.size(); i++) os << ' ' << xs[i];
       return os;
    }
    template<typename K, typename V>
    ostream& operator<<(ostream& os, const pair<K, V>& p) {
        return os << "(" << p.first << "," << p.second << ")";
    }
    template<typename K, typename V>
    ostream& operator<<(ostream& os, const map<K, V>& m) {
        bool first = true;
        for (auto p : m) {
            if (first) first = false;
            else os << ":";
            os << "(" << p.first << "," << p.second << ")";
        }
        return os;
    }


    map<tuple<int, int, bool>, int> id;
    vector<tuple<int, int, bool>> fromId;

    struct Edge {
        int to, cost;
        Edge(int to, int cost) : to(to), cost(cost) {}
    };
    vector<vector<Edge>> Graph;

    int N, M, S, G;

    bool IN = false;
    bool OUT = true;

    void solve() {
        cin >> N >> M >> S >> G;
        S--; G--;
        vector<vector<tuple<int, int, bool>>> V(N);
        auto init = make_tuple(S, 0, IN);
        V[S].push_back(init);
        id[init] = 0;
        fromId.push_back(init);
        Graph.emplace_back();
        for (int  i = 0; i < M; i++) {
            int u, v, t, c; cin >> u >> v >> t >> c;
            u--; v--;
            auto x = make_tuple(u, t, OUT);
            auto x_id = fromId.size();
            fromId.push_back(x);
            id[x] = x_id;
            Graph.emplace_back();
            V[u].push_back(x);
            auto y = make_tuple(v, t + c, IN);
            auto y_id = fromId.size();
            fromId.push_back(y);
            id[y] = y_id;
            Graph.emplace_back();
            V[v].push_back(y);
            Graph[x_id].emplace_back(y_id, 0);
        }
        for (auto& L : V) {
            sort(L.begin(), L.end(), [&](const tuple<int, int, bool>& a, const tuple<int, int, bool>& b) {
                return get<1>(a) == get<1>(b) ? get<2>(a) < get<2>(b) : get<1>(a) < get<1>(b);
            });
            for (unsigned i = 0; i + 1 < L.size(); i++) {
                auto& x = L[i];
                auto& y = L[i + 1];
                auto time_x = get<1>(x);
                auto time_y = get<1>(y);
                Graph[id[x]].emplace_back(id[y], time_y - time_x);
            }
        }

        auto start = id[ V[S][0] ];

        // dijkstra
        const int INF = 1<<28;
        vector<int> D(Graph.size(), INF);
        struct State {
            int v, cost;
            State(int v, int cost) : v(v), cost(cost) {}
            bool operator<(const State& s) const {
                return cost > s.cost;
            }
        };
        priority_queue<State> PQ;
        PQ.emplace(start, 0);
        D[start] = 0;
        while (not PQ.empty()) {
            auto cur = PQ.top(); PQ.pop();
            for (auto& e : Graph[cur.v]) {
                auto next = e.to;
                if (D[next] > cur.cost + e.cost) {
                    D[next] = cur.cost + e.cost;
                    PQ.emplace(next, D[next]);
                }
            }
        }
        int ans = INF;
        for (auto& goal_vertex : V[G]) {
            auto goal = id[goal_vertex];
            ans = min(ans, D[goal]);
        }
        cout << ans << endl;
    }
}

int main() {
    solve();
    return 0;
}
accuracy: 0.4827586206896552
time_ms: 500
memory_kb: 55124
final_rank: 18

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

using namespace std;

const bool debug = true;
#define dbg(...) if(debug) printf(__VA_ARGS__)
#define print(var) if (debug) cout << #var << " = " << var << endl

namespace {
    /** output whole vector. ex) vector<int>{1, 2, 3} -> '1 2 3'. */
    template<typename T>
    ostream& operator<<(ostream& os, const vector<T>& xs) {
       if (xs.empty()) return os << "[]";
       os << xs[0];
       for (auto i = 1; i < xs.size(); i++) os << ' ' << xs[i];
       return os;
    }
    template<typename K, typename V>
    ostream& operator<<(ostream& os, const pair<K, V>& p) {
        return os << "(" << p.first << "," << p.second << ")";
    }
    template<typename K, typename V>
    ostream& operator<<(ostream& os, const map<K, V>& m) {
        bool first = true;
        for (auto p : m) {
            if (first) first = false;
            else os << ":";
            os << "(" << p.first << "," << p.second << ")";
        }
        return os;
    }


    map<tuple<int, int, bool>, int> id;
    vector<tuple<int, int, bool>> fromId;

    struct Edge {
        int to, cost;
        Edge(int to, int cost) : to(to), cost(cost) {}
    };
    vector<vector<Edge>> Graph;

    int N, M, S, G;

    bool IN = false;
    bool OUT = true;

    void solve() {
        cin >> N >> M >> S >> G;
        S--; G--;
        vector<vector<tuple<int, int, bool>>> V(N);
        auto init = make_tuple(S, 0, IN);
        V[S].push_back(init);
        id[init] = 0;
        fromId.push_back(init);
        Graph.emplace_back();
        for (int  i = 0; i < M; i++) {
            int u, v, t, c; cin >> u >> v >> t >> c;
            u--; v--;
            auto x = make_tuple(u, t, OUT);
            auto x_id = fromId.size();
            fromId.push_back(x);
            id[x] = x_id;
            Graph.emplace_back();
            V[u].push_back(x);
            auto y = make_tuple(v, t + c, IN);
            auto y_id = fromId.size();
            fromId.push_back(y);
            id[y] = y_id;
            Graph.emplace_back();
            V[v].push_back(y);
            Graph[x_id].emplace_back(y_id, 0);
        }
        for (auto& L : V) {
            sort(L.begin(), L.end(), [&](const tuple<int, int, bool>& a, const tuple<int, int, bool>& b) { return get<1>(a) == get<1>(b) ? get<2>(a) < get<2>(b) : get<1>(a) < get<1>(b); });
        }
        for (auto& L : V) {
            for (int i = 0; i + 1 < L.size(); i++) {
                auto& x = L[i];
                auto& y = L[i + 1];
                auto time_x = get<1>(x);
                auto time_y = get<1>(y);
                Graph[id[x]].emplace_back(id[y], time_y - time_x);
            }
        }

        auto start = id[ V[S][0] ];

        // dijkstra
        const int INF = 1<<28;
        vector<int> D(Graph.size(), INF);
        struct State {
            int v, cost;
            State(int v, int cost) : v(v), cost(cost) {}
            bool operator<(const State& s) const {
                return cost > s.cost;
            }
        };
        priority_queue<State> PQ;
        PQ.emplace(start, 0);
        D[start] = 0;
        while (not PQ.empty()) {
            auto cur = PQ.top(); PQ.pop();
            for (auto& e : Graph[cur.v]) {
                auto next = e.to;
                if (D[next] > cur.cost + e.cost) {
                    D[next] = cur.cost + e.cost;
                    PQ.emplace(next, D[next]);
                }
            }
        }
        int ans = INF;
        for (auto& goal_vertex : V[G]) {
            auto goal = id[goal_vertex];
            ans = min(ans, D[goal]);
        }
        cout << ans << endl;
    }
}

int main() {
    solve();
    return 0;
}
accuracy: 0.4827586206896552
time_ms: 490
memory_kb: 55092
final_rank: 17

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

using namespace std;

#define MP make_pair
#define FF first
#define SS second

using LL = long long;
using PII = pair<int,int>;
using PLL = pair<LL,LL>;
using PILL = pair<LL,PII>;

const LL INF = 1e15;
const int UB = 400010;

int N, M, S, Gl;
map<PLL,int> ids;
LL dist[UB];
void dijk(vector<vector<PLL>>& G){
  priority_queue<PLL, vector<PLL>, greater<PLL>> pq;
  fill(dist, dist+UB, INF);
  pq.push(PLL(0,ids[MP(S,0)]));
  dist[ids[MP(S,0)]] = 0;

  while(!pq.empty()){
	PLL p = pq.top();
	pq.pop();
	if(p.FF > dist[p.SS])
	  continue;
	  
	for(auto&& e: G[p.SS]){
	  if(p.FF + e.SS < dist[e.FF]){
		dist[e.FF] = p.FF + e.SS;
		pq.push(MP(dist[e.FF], e.FF));
	  }
	}
  }
}

int main(){
  cin >> N >> M >> S >> Gl;
  --S;
  --Gl;
  int id = 0;
  
  vector<vector<int>> tms(N);
  ids[MP(0,S)] = id++;
  tms[S].push_back(0);
  vector<vector<PLL>> G(UB);
  for(int i=0;i<M;++i){
	int u, v, t, c;
	cin >> u >> v >> t >> c;
	--u;
	--v;
	if(!ids.count(MP(u,t)))
	  ids[MP(u,t)] = id++;
	if(!ids.count(MP(v,t+c)))
	  ids[MP(v,t+c)] = id++;
	tms[u].push_back(t);
	tms[v].push_back(t+c);
	G[ids[MP(u,t)]].emplace_back(ids[MP(v,t+c)], 0);
  }

  for(int i=0;i<N;++i){
	sort(begin(tms[i]), end(tms[i]));
	tms[i].erase(unique(begin(tms[i]), end(tms[i])), end(tms[i]));
	for(int j=0;j+1<tms[i].size();++j){
	  G[ids[MP(i,tms[i][j])]].emplace_back(ids[MP(i,tms[i][j+1])], tms[i][j+1] - tms[i][j]);
	}
  }

  dijk(G);
  LL ans = INF;
  for(auto&& t: tms[Gl])
	ans = min(ans, dist[ids[MP(Gl,t)]]);
  cout << ans << endl;
  
  return 0;
}
accuracy: 1
time_ms: 680
memory_kb: 60960
final_rank: 12