Submission #23249997


Source Code Expand

#include <bits/stdc++.h>
#define ll long long
#define SZ(vvv) ((int)vvv.size())
#define all(vvv) (vvv.begin(), vvv.end())
#define fastIO cout << fixed << setprecision(7), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
const int N = 1e5 + 9, M = 1e3 + 3, MOD = 1e9 + 7, OO = 1e9;
const ll INF = 1e15;

ll dist[N];
vector<array<ll, 3>> adj[N];

ll f(ll c, ll totalT, ll d) {
    if(totalT < 0)
        return LONG_LONG_MAX;
    return c + totalT + (d / (totalT + 1));
}

ll ternary_search(ll l, ll r, ll c, ll d) {
    while (r - l > 0) {
        ll m1 = l + (r - l) / 3;
        ll m2 = r - (r - l) / 3;
        ll f1 = f(c, m1, d);      //evaluates the function at m1
        ll f2 = f(c, m2, d);      //evaluates the function at m2
        if (f1 > f2)
            l = m1 + 1;
        else
            r = m2 - 1;
    }
    ll best = LONG_LONG_MAX;
    for (int i = 0; i < 5; ++i) {
        best = min(best, f(c, max(l, l + i), d));
        best = min(best, f(c, max(l, l - i), d));
        best = min(best, f(c, max(l, r + i), d));
        best = min(best, f(c, max(l, r - i), d));
    }
    return best;
}


void dij() {
    for (int i = 0; i < N; ++i)
        dist[i] = LONG_LONG_MAX;
    priority_queue<pair<ll, int>> pq;
    pq.push({0, 1});
    dist[1] = 0;
    while(!pq.empty()) {
        auto it = pq.top();
        pq.pop();
        ll cost = -it.first;
        int node = it.second;
        if(dist[node] < cost) continue;
        for(auto child: adj[node]) {
            ll start = cost, end = LONG_LONG_MAX - 5;
            ll nwCost;
            if(end < start)
                nwCost = cost + child[1];
            else
                nwCost = ternary_search(start, end, child[1], child[2]);
            int nwNode = child[0];
            if(dist[nwNode] > nwCost) {
                dist[nwNode] = nwCost;
                pq.push({-dist[nwNode], nwNode});
            }
        }
    }
}

void runtestcase() {
    int n, m;
    cin >> n >> m;
    for (int i = 0, a, b, c, d; i < m; ++i) {
        cin >> a >> b >> c >> d;
        adj[a].push_back({b, c, d});
        adj[b].push_back({a, c, d});
    }
    dij();
    if(dist[n] == LONG_LONG_MAX)
        dist[n] = -1;
    cout << dist[n] << '\n';
}

int main() {
//    freopen("/home/omar/input.in", "rt", stdin);
//    freopen("/home/omar/output.in", "wt", stdout);
#ifdef LOCAL
#endif
    fastIO;
    int t = 1, tt = 1;
//    cin >> t;
    while(t--)
        runtestcase();
}

Submission Info

Submission Time
Task E - Rush Hour 2
User it_wasnt_me
Language C++ (GCC 9.2.1)
Score 0
Code Size 2575 Byte
Status WA
Exec Time 503 ms
Memory 14780 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:89:16: warning: unused variable ‘tt’ [-Wunused-variable]
   89 |     int t = 1, tt = 1;
      |                ^~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 4
AC × 30
WA × 6
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All hack_01.txt, hack_02.txt, hack_03.txt, hand_01.txt, hand_02.txt, hand_03.txt, max_01.txt, random_01.txt, random_02.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, special_01.txt, special_02.txt, special_03.txt, special_04.txt, special_05.txt, special_06.txt, tester_hack_01.txt
Case Name Status Exec Time Memory
hack_01.txt AC 465 ms 13472 KiB
hack_02.txt AC 467 ms 13144 KiB
hack_03.txt AC 12 ms 6600 KiB
hand_01.txt WA 8 ms 6604 KiB
hand_02.txt WA 8 ms 6704 KiB
hand_03.txt AC 6 ms 6668 KiB
max_01.txt AC 502 ms 13236 KiB
random_01.txt AC 414 ms 12672 KiB
random_02.txt AC 50 ms 11324 KiB
random_11.txt AC 456 ms 13060 KiB
random_12.txt AC 392 ms 13012 KiB
random_13.txt AC 262 ms 10168 KiB
random_14.txt AC 352 ms 11076 KiB
random_15.txt AC 412 ms 12428 KiB
random_16.txt AC 458 ms 13948 KiB
random_17.txt AC 464 ms 13040 KiB
random_18.txt AC 342 ms 11024 KiB
random_21.txt AC 496 ms 13280 KiB
random_22.txt AC 493 ms 13168 KiB
random_23.txt AC 500 ms 13248 KiB
random_24.txt AC 503 ms 13240 KiB
random_31.txt AC 488 ms 14764 KiB
random_32.txt AC 486 ms 14676 KiB
random_33.txt AC 487 ms 14780 KiB
random_34.txt AC 484 ms 14684 KiB
sample_01.txt AC 6 ms 6676 KiB
sample_02.txt AC 6 ms 6760 KiB
sample_03.txt AC 7 ms 6676 KiB
sample_04.txt AC 9 ms 6676 KiB
special_01.txt AC 151 ms 7904 KiB
special_02.txt AC 150 ms 7820 KiB
special_03.txt WA 148 ms 7800 KiB
special_04.txt WA 151 ms 7736 KiB
special_05.txt WA 152 ms 7924 KiB
special_06.txt WA 148 ms 7880 KiB
tester_hack_01.txt AC 486 ms 13996 KiB