Submission #1235816

#TimeUsernameProblemLanguageResultExecution timeMemory
1235816iamhereforfunRobot (JOI21_ho_t4)C++20
0 / 100
188 ms24496 KiB
// IamHereForFun //

#include <bits/stdc++.h>

using namespace std;

#define LSOne(X) ((X) & -(X))

const int N = 1e5 + 5;
const int M = 2e5 + 5;
const int L = 12;
const int LG = 25;
const int P = 37;
const long long INF = 1e18 + 5;
const int MOD = 1e9 + 7;
const int nx[] = {0, -1, 0, 1}, ny[] = {-1, 0, 1, 0};

// find shortest path, for every node, calculate minimum path
// problemo: because everytime you change an edge, it affects the next node too =))
// lets say you change a edge, then you should change it so that it doesn't affect the next one =))
// but if you change a edge for another edge to be valid, then you shouldn't go to the node that it's connect too, other wise just use it
// we need to save dp[i][j] for the ith node and the last one being j, but it would be mle, so we compress j so that we can do it

int n, m;
vector<array<int, 3>> adj[N];
vector<int> id[N];
vector<long long> dp[N];
vector<bool> vis[N];
long long ans = 0, tot[M];

int gid(int a, int b)
{
    return lower_bound(id[b].begin(), id[b].end(), a) - id[b].begin();
}

void solve()
{
    cin >> n >> m;
    for(int x = 0; x < m; x++)
    {
        int a, b, c, p; cin >> a >> b >> c >> p;
        adj[a].push_back({b, c, p});
        adj[b].push_back({a, c, p});
        id[a].push_back(b);
        id[b].push_back(a);
    }
    for(int x = 1; x <= n; x++)
    {
        id[x].push_back(-1);
        sort(id[x].begin(), id[x].end());
        dp[x].assign(id[x].size(), INF);
        vis[x].assign(id[x].size(), 0);
    }
    priority_queue<array<long long, 3>> pq;
    dp[1][0] = 0;
    pq.push({0, -1, 1});
    while(!pq.empty())
    {
        array<long long, 3> a = pq.top();
        pq.pop();
        int i = a[2], l = a[1];
        l = gid(l, i);
        if(vis[i][l]) continue;
        // cout << i << " " << id[i][l] << " " << l << "\n";
        vis[i][l] = true;
        for(array<int, 3> arr : adj[i])
        {
            if(arr[0] == a[1]) continue;
            tot[arr[1]] += arr[2];
        }
        for(array<int, 3> arr : adj[i])
        {
            long long val = tot[arr[1]] - arr[2];
            int j = arr[0], t = arr[1], cid = gid(i, j);
            if(dp[j][cid] > dp[i][l] + val)
            {
                dp[j][cid] = dp[i][l] + val;
                pq.push({-dp[j][cid], i, j});
            }
            cid = 0;
            if(dp[j][cid] > dp[i][l] + arr[2])
            {
                dp[j][cid] = dp[i][l] + arr[2];
                pq.push({-dp[j][cid], -1, j});
            }
        }
        for(array<int, 3> arr : adj[i])
        {
          if(arr[0] == a[1]) continue;
            tot[arr[1]] -= arr[2];
        }
    }
    ans = INF;
    for(long long i : dp[n]) ans = min(ans, i);
    if(ans == INF)
    {
        cout << -1;
    }
    else
    {
        cout << ans;
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...