結果

問題 No.497 入れ子の箱
ユーザー PachicobuePachicobue
提出日時 2018-01-24 16:29:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,649 bytes
コンパイル時間 2,254 ms
コンパイル使用メモリ 212,024 KB
実行使用メモリ 23,952 KB
最終ジャッジ日時 2023-08-27 02:14:46
合計ジャッジ時間 17,702 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 729 ms
23,952 KB
testcase_04 AC 734 ms
23,620 KB
testcase_05 AC 703 ms
23,824 KB
testcase_06 AC 699 ms
23,940 KB
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 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 43 ms
4,380 KB
testcase_31 AC 45 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using ld = long double;
template <typename T>
vector<T> Vec(int n, T v)
{
    return vector<T>(n, v);
}
template <class... Args>
auto Vec(int n, Args... args)
{
    auto val = Vec(args...);
    return vector<decltype(val)>(n, move(val));
}

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = (ll)1e9 + 7LL;
constexpr ld PI = static_cast<ld>(3.1415926535898);

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 10;

struct CostGraph {
    using T = ll;
    CostGraph(const int v) : V{v}
    {
        edge.resize(v);
        rev_edge.resize(v);
    }
    struct Edge {
        Edge(const int from, const int to, const T cost) : from{from}, to{to}, cost{cost} {}
        const int from;
        const int to;
        const T cost;
        bool operator<(const Edge& e) const
        {
            return cost != e.cost ? cost < e.cost : to < e.to;
        }
    };
    void addEdge(const int from, const int to, const T cost = 1)
    {
        edge[from].push_back(Edge{from, to, cost});
        rev_edge[to].push_back(Edge(to, from, cost));
    }
    vector<vector<Edge>> edge;
    vector<vector<Edge>> rev_edge;
    const int V;
};

bool BellmanFord(const CostGraph& g, const int s, vector<CostGraph::T>& d)
{
    using T = CostGraph::T;
    assert(s < g.V);
    assert(d.size() == g.V);
    for (int i = 0; i < g.V; i++) {
        d[i] = INF<T>;
    }
    d[s] = 0;
    bool no_negative_loop = true;
    for (int i = 0; i < g.V; i++) {
        for (int v = 0; v < g.V; v++) {
            if (d[v] != INF<T>) {
                for (const auto& e : g.edge[v]) {
                    if (d[e.to] > d[e.from] + e.cost) {
                        d[e.to] = d[v] + e.cost;
                        if (i == g.V - 1) {
                            d[e.to] = -INF<T>;  // Confirm " -INF < min(possible_cost) * V "
                            no_negative_loop = false;
                        }
                    }
                }
            }
        }
    }
    return no_negative_loop;
}


struct Box {
    Box(ll x, ll y, ll z) : x(x), y(y), z(z) {}
    ll x, y, z;
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;
    vector<Box> box;
    for (int i = 0; i < N; i++) {
        ll x, y, z;
        cin >> x >> y >> z;
        box.push_back(Box(x, y, z));
    }
    CostGraph g(N + 2);
    const int s = N;
    const int t = N + 1;
    for (int i = 0; i < N; i++) {
        g.addEdge(s, i, -1);
        g.addEdge(i, t, 0);
        for (int j = 0; j < N; j++) {
            vector<ll> v{box[j].x, box[j].y, box[j].z};
            do {
                if (box[i].x > v[0] and box[i].y > v[1] and box[i].z > v[2]) {
                    g.addEdge(i, j, -1);
                    break;
                }
                if (box[i].x == v[0] and box[i].y == v[1] and box[i].z == v[2]) {
                    if (i < j) {
                        g.addEdge(i, j, -1);
                    }
                    break;
                }
            } while (next_permutation(v.begin(), v.end()));
        }
    }
    vector<ll> d(N + 2, INF<ll>);
    BellmanFord(g, s, d);
    cout << -d[t] << endl;

    return 0;
}
0