Submission #894495

#TimeUsernameProblemLanguageResultExecution timeMemory
894495hafoRobot (JOI21_ho_t4)C++14
100 / 100
677 ms102192 KiB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 7;
const ll oo = 1e18 + 69;

int n, m, u, v, c, p;
struct edge {
    int v, c, p;
};
map<int, vector<edge>> g[maxn];
ll f[maxn];
map<int, ll> f2[maxn], cnt[maxn];

struct state {
    ll cost;
    int u, c;

    friend bool operator < (const state &a, const state &b) {
        return a.cost > b.cost;
    } 

};

void dijkstra() {
    priority_queue<state> q;
    fill_n(f, n + 1, oo);
    f[1] = 0;
    q.push({0, 1, 0});

    while(!q.empty()) {
        int u = q.top().u, c = q.top().c;
        ll fu = q.top().cost;
        q.pop();

        if(c > 0) {
            if(f2[u][c] != fu) continue;
            for(auto e:g[u][c]) {
                int v = e.v;
                if(mini(f[v], fu + cnt[u][c] - e.p)) {
                    q.push({f[v], v, 0});
                }
            }
        } else {
            if(f[u] != fu) continue;
            for(auto tmp:g[u]) {
                for(auto e:tmp.se) {
                    int v = e.v;
                    if(mini(f[v], fu + cnt[u][e.c] - e.p)) {
                        q.push({f[v], v, 0});
                    }
                    if(mini(f[v], fu + e.p)) {
                        q.push({f[v], v, 0});
                    }
                    if(!f2[v].count(e.c) || f2[v][c] > fu) {
                        f2[v][e.c] = fu;
                        q.push({fu, v, e.c});
                    }
                }
            }
        }
    }

    cout<<(f[n] == oo ? -1:f[n]);
}

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

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>n>>m;
    while(m--) {
        cin>>u>>v>>c>>p;
        g[u][c].pb({v, c, p});
        g[v][c].pb({u, c, p});
        cnt[u][c] += p;
        cnt[v][c] += p;
    }

    dijkstra();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...