結果

問題 No.798 コレクション
ユーザー Tiramister
提出日時 2019-03-15 22:33:55
言語 C++14
(gcc 8.2.0)
結果
TLE  
実行時間 -
コード長 5,269 Byte
コンパイル時間 1,017 ms
使用メモリ 83,880 KB
最終ジャッジ日時 2019-03-15 22:34:06

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
000_sample_1 AC 3 ms
2,480 KB
000_sample_2 AC 4 ms
1,508 KB
000_sample_3 AC 4 ms
1,516 KB
001_rand_1 AC 4 ms
1,508 KB
001_rand_2 AC 4 ms
1,512 KB
001_rand_3 AC 3 ms
1,512 KB
001_rand_4 AC 3 ms
1,512 KB
001_rand_5 AC 4 ms
1,512 KB
001_rand_6 AC 3 ms
1,520 KB
001_rand_7 AC 3 ms
1,524 KB
001_rand_8 AC 4 ms
1,516 KB
001_rand_9 AC 4 ms
1,516 KB
001_rand_10 AC 4 ms
1,512 KB
002_rand_1 TLE -
002_rand_2 -- -
002_rand_3 -- -
002_rand_4 -- -
002_rand_5 -- -
002_rand_6 -- -
002_rand_7 -- -
002_rand_8 -- -
002_rand_9 -- -
002_rand_10 -- -
100_hand_1 -- -
100_hand_2 -- -
100_hand_3 -- -
テストケース一括ダウンロード

ソースコード

diff #
// IO library
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>
#include <cmath>
#include <numeric>
#include <random>

// contancer library
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using ld = long double;

template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> p);

template <class T>
ostream& operator<<(ostream& os, vector<T> v) {
    os << "[";
    for (auto vv : v) os << vv << ",";
    return os << "]";
}

template <class T>
ostream& operator<<(ostream& os, set<T> v) {
    os << "{";
    for (auto vv : v) os << vv << ",";
    return os << "}";
}

template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> p) {
    return os << "(" << p.first << "," << p.second << ")";
}

template <class K, class T>
ostream& operator<<(ostream& os, map<K, T> v) {
    os << "{";
    for (auto vv : v) os << vv << ",";
    return os << "}";
}

template <class T>
ostream& operator<<(ostream& os, queue<T> q) {
    os << "[";
    while (!q.empty()) {
        os << q.front() << ",";
        q.pop();
    }
    return os << "]";
}

template <class T>
ostream& operator<<(ostream& os, priority_queue<T> q) {
    os << "{";
    while (!q.empty()) {
        os << q.top() << ",";
        q.pop();
    }
    return os << "}";
}

const ll MOD = 1000000007;
// const ll MOD = 998244353;
// const int INF = 1 << 25;
// const ll INF = 1LL << 50;
// const ld PI = acos(-1);
// const ld EPS = 1e-10;
// mt19937 mt(ll(time(0)));

template <class T>
vector<T> Vec(size_t l, T v) { return vector<T>(l, v); }

template <class T, class... Ts>
auto Vec(size_t l, Ts... ts) {
    return vector<decltype(Vec<T>(ts...))>(l, Vec<T>(ts...));
}

template <typename T>
inline T sq(T a) { return a * a; }

template <typename T>
inline T iceil(T n, T d) { return (n + d - 1) / d; }

template <typename T>
T gcd(T a, T b) {
    while (b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}

template <typename T, typename U>
T ipow(T b, U n) {
    T ret = 1;
    while (n > 0) {
        if (n & 1) ret *= b;
        n >>= 1;
        b *= b;
    }
    return ret;
}

template <typename T, typename U>
T mpow(T b, U n) {
    T ret = 1;
    while (n > 0) {
        if (n & 1) ret = ret * b % MOD;
        n >>= 1;
        b = b * b % MOD;
    }
    return ret;
}

struct edge {
    ll to;
    ll cap;
    ll cost;
    ll rev;  // path[to]中でのfromのイテレータ
    edge(ll to_, ll cap_, ll cost_, ll rev_) : to(to_), cap(cap_), cost(cost_), rev(rev_){};
};

class MinCostFlow {
public:
    explicit MinCostFlow(ll N) : MAX_V(N) {
        path.resize(MAX_V);
    }

    void add_edge(ll from, ll to, ll cap, ll cost) {
        // fromとtoの間に双方向の辺を加える
        // 有向の場合は後者のcapを0にする
        path[from].push_back(edge(to, cap, cost, path[to].size()));
        path[to].push_back(edge(from, 0, -cost, path[from].size() - 1));
    }

    ll min_cost_flow(ll s, ll g, ll f) {
        // sからgへ流量fを流すときの最小コストを求める

        ll ret = 0;
        while (f > 0) {
            // Bellman-Fordでs-g間最短経路を求める
            vector<ll> d(MAX_V, INF), prevv(MAX_V), preve(MAX_V);
            d[s] = 0;
            while (true) {
                bool update = false;

                for (ll v = 0; v < MAX_V; ++v) {
                    if (d[v] == INF) continue;
                    for (ll i = 0; i < path[v].size(); ++i) {
                        auto e = path[v][i];

                        if (e.cap > 0 && d[e.to] > d[v] + e.cost) {
                            d[e.to] = d[v] + e.cost;
                            prevv[e.to] = v;
                            preve[e.to] = i;
                            update = true;
                        }
                    }
                }

                if (!update) break;
            }

            // これ以上流せない
            if (d[g] == INF) break;

            ll sf = f, v = g;
            while (v != s) {
                sf = min(sf, path[prevv[v]][preve[v]].cap);
                v = prevv[v];
            }

            f -= sf;
            ret += sf * d[g];

            v = g;
            while (v != s) {
                auto& e = path[prevv[v]][preve[v]];
                e.cap -= sf;
                path[v][e.rev].cap += sf;
                v = prevv[v];
            }
        }
        return ret;
    }

    const ll INF = 1LL << 50;
    vector<vector<edge>> path;
    ll MAX_V;
};

int main() {
    int N;
    cin >> N;
    int day = N - N / 3;

    vector<ll> A(N), B(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i] >> B[i];
    }

    MinCostFlow mcf(day + N + 2);
    for (int d = 0; d < day; ++d) {
        mcf.add_edge(day + N + 1, d, 1, 0);
    }

    for (int d = 0; d < day; ++d) {
        for (int i = 0; i < N; ++i) {
            mcf.add_edge(d, day + i, 1, A[i] + B[i] * d);
        }
    }

    for (int i = 0; i < N; ++i) {
        mcf.add_edge(day + i, day + N, 1, 0);
    }

    cout << mcf.min_cost_flow(day + N + 1, day + N, day) << endl;
    return 0;
}
0