QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#86272#5034. >.<_slb0 269ms218572kbC++175.7kb2023-03-09 16:39:402023-03-09 16:39:43

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-09 16:39:43]
  • 评测
  • 测评结果:0
  • 用时:269ms
  • 内存:218572kb
  • [2023-03-09 16:39:40]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
using namespace std;

namespace solve
{
    const int maxn = 5e5 + 10;
    typedef long long ll;
    const ll inf = 1e18;

    vector<pair<int, int>> e[maxn];
    inline void add(int x, int y, int w) { e[x].emplace_back(y, w); }

    int n, m, k;

    map<int, int> ch[maxn];
    int ed[maxn], tot = 1;

    void insert(vector<int> a)
    {
        int p = 1;
        for (int i = 0; i < (int)a.size(); i++)
        {
            int c = a[i];
            if (!ch[p][c])
                ch[p][c] = ++tot;
            p = ch[p][c];
        }
        ed[p] = 1;
    }

    int root[maxn];
    int used[maxn];

    struct tree
    {
        static const int maxn = solve::maxn << 5;

        int ls[maxn], rs[maxn], tot, vis[maxn];
        int adj[maxn], v[maxn], w[maxn];
        tree() { memset(w, 0x3f, sizeof(w)); }
        void clone(int &k)
        {
            tot++, ls[tot] = ls[k], rs[tot] = rs[k], adj[tot] = adj[k], v[tot] = v[k], w[tot] = w[k];
            k = tot;
        }
        void modify(int l, int r, int x, int v, int &k)
        {
            clone(k);
            if (l == r)
            {
                adj[k] = v;
                return;
            }
            int mid = (l + r) / 2;
            if (x <= mid)
                modify(l, mid, x, v, ls[k]);
            else
                modify(mid + 1, r, x, v, rs[k]);
        }
        void modify2(int l, int r, int x, int vv, int ww, int &k)
        {
            clone(k);
            if (l == r)
            {
                v[k] = vv, w[k] = ww;
                return;
            }
            int mid = (l + r) / 2;
            if (x <= mid)
                modify2(l, mid, x, vv, ww, ls[k]);
            else
                modify2(mid + 1, r, x, vv, ww, rs[k]);
        }
        void get(int l, int r, int k)
        {
            if (vis[k] || !k)
                return;
            vis[k] = 1;
            if (l == r)
            {
                if (!used[adj[k]] && !ed[adj[k]])
                    b.emplace_back(adj[k], w[k]);
                return;
            }
            int mid = (l + r) / 2;
            get(l, mid, ls[k]), get(mid + 1, r, rs[k]);
        }
        int query(int l, int r, int x, int k)
        {
            if (l == r)
                return adj[k];
            int mid = (l + r) / 2;
            if (x <= mid)
                return query(l, mid, x, ls[k]);
            else
                return query(mid + 1, r, x, rs[k]);
        }
        void dfs(int l, int r, int k)
        {
            if (!k)
                return;
            if (l == r)
            {
                cerr << "?" << l << " " << adj[k]  << " " << w[k] << endl;
                return;
            }
            int mid = (l + r) / 2;
            dfs(l, mid, ls[k]), dfs(mid + 1, r, rs[k]);
        }
        vector<pair<int, int>> b;
        vector<pair<int, int>> get(int x)
        {
            b.clear();
            get(1, n, root[x]);
            return b;
        }
    } t;

    int fail[maxn];

    void build()
    {
        queue<int> q;
        q.push(1);
        for (auto o : ch[1])
            t.modify(1, n, o.first, o.second, root[1]);
        while (!q.empty())
        {
            int x = q.front();
            q.pop();
            root[x] = root[fail[x]];
            if (x >= 2 && x <= n + 1)
                for (auto [v, w] : e[x - 1])
                    t.modify2(1, n, v, v, w, root[x]);
            for (auto o : ch[x])
            {
                fail[o.second] = t.query(1, n, o.first, root[x]);
                ed[o.second] |= ed[fail[o.second]];
                t.modify(1, n, o.first, o.second, root[x]);
                q.push(o.second);
            }
        }
        /*
        for (int i = 1; i <= tot; i++)
        {
            cerr << i << " " << fail[i] << " " << ed[i] << endl;
            t.dfs(1, n, root[i]);
        }
        */
    }

    ll dis[maxn];
    int vis[maxn];
    
    void dij()
    {
        priority_queue<pair<ll, int>> q;
        for (int i = 1; i <= tot; i++)
            dis[i] = inf;
        dis[2] = 0, q.push({0, 2});
        while (!q.empty())
        {
            auto o = q.top();
            q.pop();
            if (used[o.second] || ed[o.second])
                continue;
            used[o.second] = 1;
            int x = o.second;
            auto e = t.get(x);
            for (auto [v, w] : e)
            {
                if (dis[x] + w < dis[v])
                    dis[v] = dis[x] + w, q.push({-dis[v], v});
            }
        }
        /*
        for (int i = 1; i <= tot; i++)
            cerr << dis[i] << " ";
        cerr << endl;
        */
    }

    void main()
    {
        cin >> n >> m >> k;
        for (int i = 0, x, y, w; i < m; i++)
            cin >> x >> y >> w, add(x, y, w);
        for (int i = 1; i <= n; i++)
            ch[1][i] = ++tot, ch[0][i] = 1;
        for (auto o : ch[0])
            t.modify(1, n, o.first, o.second, root[0]);
        for (int i = 0; i < k; i++)
        {
            vector<int> qwq;
            int u;
            cin >> u;
            for (int i = 0, x; i < u; i++)
                cin >> x, qwq.push_back(x);
            insert(qwq);
        }
        // cerr << "?" << tot << endl;
        build();
        dij();
        cout << (dis[n + 1] == inf ? -1ll : dis[n + 1]) << endl;
    }
}

int main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--)
        solve::main();
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 30ms
memory: 101104kb

input:

35 100 0
34 7 447733879
24 20 187005344
14 34 654502303
2 31 865194349
20 33 9517055
33 15 991889891
24 33 395599993
13 16 237525328
9 5 373850826
30 34 391470240
10 7 650077565
26 10 400825980
34 27 189924713
19 27 907609573
20 10 614945312
10 5 960007605
1 7 984076202
32 25 539699728
24 31 2553027...

output:

1061109567

result:

wrong answer 1st lines differ - expected: '1970522617', found: '1061109567'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #11:

score: 0
Wrong Answer
time: 269ms
memory: 218572kb

input:

50000 200000 1
7542 37166 116613326
3581 43961 629220971
12873 42953 440313807
31744 5286 697832848
25882 12748 106667302
34760 29676 181570340
41570 9240 885513989
22227 35688 63657981
43180 29194 174058940
8977 41899 48262624
7465 18291 600002514
46925 9281 951474878
2115 31162 373758881
5386 3798...

output:

1061109567

result:

wrong answer 1st lines differ - expected: '2526392504', found: '1061109567'

Subtask #4:

score: 0
Skipped

Dependency #2:

0%