結果

問題 No.1984 [Cherry 4th Tune *] Dilemma
ユーザー 👑 colognecologne
提出日時 2022-05-03 13:41:45
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,399 bytes
コンパイル時間 2,179 ms
コンパイル使用メモリ 104,192 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-26 01:42:18
合計ジャッジ時間 6,542 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cstdio>
#include <cinttypes>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
class StrictInput
{
    char *p;
    off_t cur = 0;
    off_t len = 0;

public:
    explicit StrictInput(int fd = 0)
    {
        struct stat st;
        fstat(fd, &st);
        p = (char *)mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
        len = st.st_size;
    }

    char readChar()
    {
        assert(cur != len);
        return p[cur++];
    }

    void unreadChar()
    {
        assert(cur != 0);
        --cur;
    }

    bool isEof() { return cur == len; }
    void readEof() { assert(isEof()); }
    void readSpace() { assert(readChar() == ' '); }
    void readEoln() { assert(readChar() == '\n'); }

    // reads uint64_t in range [from, to]
    uint64_t readU64(uint64_t from = 0, uint64_t to = UINT64_MAX)
    {
        uint64_t cur = 0;
        off_t read_cnt = 0;
        bool leading_zero = false;
        while (!isEof())
        {
            char p = readChar();
            if (!('0' <= p && p <= '9'))
            {
                unreadChar();
                break;
            }
            uint64_t v = p - '0';
            assert(cur <= UINT64_MAX / 10);
            cur *= 10;
            assert(cur <= UINT64_MAX - v);
            cur += v;
            if (read_cnt == 0 && v == 0)
                leading_zero = true;
            ++read_cnt;
        }
        if (cur == 0)
            assert(read_cnt == 1);
        else
            assert(!leading_zero);
        assert(from <= cur && cur <= to);
        return cur;
    }

    // reads int64_t in range [from, to]
    int64_t readI64(int64_t from = INT64_MIN, int64_t to = INT64_MAX)
    {
        uint64_t cur = 0;
        off_t read_cnt = 0;
        bool leading_zero = false;
        bool leading_minus = readChar() == '-';
        if (!leading_minus)
            unreadChar();
        while (!isEof())
        {
            char p = readChar();
            if (!('0' <= p && p <= '9'))
            {
                unreadChar();
                break;
            }
            uint64_t v = p - '0';
            assert(cur <= UINT64_MAX / 10);
            cur *= 10;
            assert(cur <= UINT64_MAX - v);
            cur += v;
            if (read_cnt == 0 && v == 0)
                leading_zero = true;
            ++read_cnt;
        }
        if (cur == 0)
            assert(read_cnt == 1 && !leading_minus);
        else
            assert(!leading_zero);

        if (cur <= INT64_MAX)
        {
            int64_t ret = cur;
            if (leading_minus)
                ret = -ret;
            assert(from <= ret && ret <= to);
            return ret;
        }
        else
        {
            assert(leading_minus && cur == uint64_t(INT64_MIN));
            assert(from == INT64_MIN);
            return INT64_MIN;
        }
    }
};

#include <algorithm>
#include <vector>
#include <atcoder/maxflow>

using namespace std;

int main()
{
    StrictInput inf;

    int N, M, K, P;
    N = inf.readU64(1, 50);
    inf.readSpace();
    M = inf.readU64(1, 50);
    inf.readSpace();
    K = inf.readU64(1, 50);
    inf.readSpace();
    P = inf.readU64(0, N * M);
    inf.readEoln();

    vector<int> E(N);
    for (int i = 0; i < N; i++)
    {
        E[i] = inf.readU64(1, 1'000'000'000);
        if (i == N - 1)
            inf.readEoln();
        else
            inf.readSpace();
    }

    vector<int> F(M);
    for (int j = 0; j < M; j++)
    {
        F[j] = inf.readU64(1, 1'000'000'000);
        if (j == M - 1)
            inf.readEoln();
        else
            inf.readSpace();
    }

    vector<int> V(K);
    for (int k = 0; k < K; k++)
    {
        V[k] = inf.readU64(1, 1'000'000'000);
        if (k == K - 1)
            inf.readEoln();
        else
            inf.readSpace();
    }

    vector<vector<int>> A(N);
    for (int i = 0; i < N; i++)
    {
        int L = inf.readU64(0, K);
        A[i].resize(L);
        int p = 0;
        for (int t = 0; t < L; t++)
        {
            inf.readSpace();
            A[i][t] = inf.readU64(p + 1, K);
            p = A[i][t];
            A[i][t]--;
        }
        inf.readEoln();
    }
    vector<pair<int, int>> IJ(P);
    for (int t = 0; t < P; t++)
    {
        IJ[t].first = inf.readU64(1, N);
        inf.readSpace();
        IJ[t].second = inf.readU64(1, M);
        inf.readEoln();
        IJ[t].first--;
        IJ[t].second--;
    }
    inf.readEof();

    sort(IJ.begin(), IJ.end());
    assert(unique(IJ.begin(), IJ.end()) == IJ.end());

    atcoder::mf_graph<long long> G(N + M + K + 2);

    int Goal = 0, Action = N, Preparation = N + M;
    int Source = N + M + K, Sink = N + M + K + 1;

    long long INF = 1e18;
    long long ans = 0;

    // Source~Goal: Goal i is achieved.
    for (int i = 0; i < N; i++)
    {
        ans += E[i];
        G.add_edge(Source, Goal + i, E[i]);
    }

    // Action~Sink: Action i is achieved
    for (int j = 0; j < M; j++)
    {
        ans += F[j];
        G.add_edge(Action + j, Sink, F[j]);
    }

    // Source~Preparation: Preparation i is achieved
    for (int k = 0; k < K; k++)
        G.add_edge(Preparation + k, Sink, V[k]);

    // If Source->Goal exists, Source->Preparation should also exist
    for (int i = 0; i < N; i++)
        for (int k : A[i])
            G.add_edge(Goal + i, Preparation + k, INF);

    // If Source->Goal exists, Source->Action should also exist
    for (auto [I, J] : IJ)
        G.add_edge(Goal + I, Action + J, INF);

    // Min cut should be deducted from maxflow
    ans -= G.flow(Source, Sink);

    int cnt = 0;

    vector<bool> cut = G.min_cut(Source);
    vector<bool> goal(N), action(M), preparation(K);
    for (int i = 0; i < N; i++)
        cnt += (goal[i] = cut[Goal + i]);
    for (int j = 0; j < M; j++)
        cnt += (action[j] = !cut[Action + j]);
    for (int k = 0; k < K; k++)
        cnt += (preparation[k] = cut[Preparation + k]);

    printf("%lld\n", ans);
    printf("%d\n", cnt);

    // Do all preparations first
    for (int i = 0; i < K; i++)
        if (preparation[i])
            printf("Preparation %d\n", i - K);

    // Others can be done arbitarily
    for (int j = 0; j < N; j++)
        if (goal[j])
            printf("Goal %d\n", j - N);

    for (int k = 0; k < M; k++)
        if (action[k])
            printf("Action %d\n", k - M);

    return 0;
}
0