Submission #1235687

#TimeUsernameProblemLanguageResultExecution timeMemory
1235687iamhereforfunRobot (JOI21_ho_t4)C++20
34 / 100
3093 ms66668 KiB
// IamHereForFun //

#include <bits/stdc++.h>

using namespace std;

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

const int N = 1e5 + 5;
const int M = 11;
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
// so we use dp[N] as normal, but in your pq, you save a number indicating that the edge that leads to it is changed or not

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

void solve()
{
    cin >> n >> m;
    for (int x = 1; x <= n; x++)
    {
        dp[x][-1] = INF;
    }
    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});
        dp[a][b] = INF;
        dp[b][a] = INF;
    }
    priority_queue<array<long long, 3>> pq;
    dp[1][-1] = 0;
    pq.push({0, -1, 1});
    while (!pq.empty())
    {
        array<long long, 3> arr = pq.top();
        pq.pop();
        int i = arr[2], l = arr[1];
        if (vis[i][l])
            continue;
        vis[i][l] = true;
        map<int, long long> mp;
        for (array<int, 3> a : adj[i])
        {
            if (a[0] == l)
                continue;
            mp[a[1]] += a[2];
        }
        for (array<int, 3> a : adj[i])
        {
            if (a[0] == l)
                continue;
            long long val = mp[a[1]] - a[2];
            int j = a[0];
            if (dp[j][i] > dp[i][l] + a[2])
            {
                dp[j][i] = dp[i][l] + a[2];
                pq.push({-dp[j][i], i, j});
            }
            if (dp[j][-1] > dp[i][l] + val)
            {
                dp[j][-1] = dp[i][l] + val;
                pq.push({-dp[j][-1], -1, j});
            }
        }
    }
    ans = INF;
    for(pair<int, long long> p : dp[n]) ans = min(ans, p.second);
    if (ans == INF)
    {
        cout << -1 << "\n";
    }
    else
    {
        cout << ans << "\n";
    }
}

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...