Submission #1092316

#TimeUsernameProblemLanguageResultExecution timeMemory
1092316ortsacRobot (JOI21_ho_t4)C++17
0 / 100
58 ms23888 KiB
#include <bits/stdc++.h>
 
using namespace std;

#define pii pair<int, int>
#define fr first
#define se second
#define ll long long

ll INF = 0x3f3f3f3f3f3f3f3f;

struct State {
    int edge;
    bool order;
    bool k;
    State(const int& a = 0, const bool& b = 0, const bool& c = 0) : edge(a), order(b), k(c) {}
};

struct Edge {
    int to, cor, p, idx;
    bool order;
    Edge(const int& a = 0, const int& b = 0, const int& c = 0, const int& d = 0, const bool& e = 0) : to(a), cor(b), p(c), idx(d), order(e) {}
};

const int MAXN = 1e5 + 10;
const int MAXM = 2e5 + 10;
vector<Edge> adj[MAXN];
pii edges[MAXN];
ll d[2][2][MAXM];
bool vis[2][2][MAXM];
bool prop[2][2][MAXM];

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int a, b, c, p;
        cin >> a >> b >> c >> p;
        adj[a].push_back(Edge(b, c, p, i, 1));
        adj[b].push_back(Edge(a, c, p, i, 0));
        edges[i] = {a, b};
    }
    deque<pair<ll, State>> pq;
    edges[0] = {1, 1};
    pq.push_back(make_pair(0, State(0, 0, 0)));
    vis[0][0][0] = 1;
    while (!pq.empty()) {
        auto node = pq.front().second;
        pq.pop_front();
        if (prop[node.k][node.order][node.edge]) continue;
        prop[node.k][node.order][node.edge] = 1;
        pii abc = edges[node.edge];
        if (node.order) swap(abc.fr, abc.se);
        int v = abc.fr;
        int x = abc.se;
        ll dn = d[node.k][node.order][node.edge];
        if (v == n) {
            cout << dn << "\n";
            return 0;
        }
        map<int, ll> somaP;
        for (auto u : adj[v]) {
            if (node.k && (u.to == x)) continue;
            somaP[u.cor] += u.p;
        }
        for (auto u : adj[v]) {
            if (u.to == x) continue;
            // sem mudar a cor
            if (somaP[u.cor] == u.p) {
                State p1(u.idx, u.order, 0);
                ll p = 0;
                ll np1 = (dn + p);
                if (!vis[p1.k][p1.order][p1.edge]) {
                    vis[p1.k][p1.order][p1.edge] = 1;
                    d[p1.k][p1.order][p1.edge] = np1;
                    pq.push_front({-np1, p1});
                } else if (np1 < d[p1.k][p1.order][p1.edge]) {
                    d[p1.k][p1.order][p1.edge] = np1;
                    pq.push_front({-np1, p1});
                }
            }
            // mudando
            State p2(u.idx, u.order, 1);
            ll np2 = (dn + u.p);
            if (!vis[p2.k][p2.order][p2.edge]) {
                vis[p2.k][p2.order][p2.edge] = 1;
                d[p2.k][p2.order][p2.edge] = np2;
                pq.push_back({-np2, p2});
            } else if (np2 < d[p2.k][p2.order][p2.edge]) {
                d[p2.k][p2.order][p2.edge] = np2;
                pq.push_back({-np2, p2});
            }
        }
    }
    cout << "-1\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...