結果

問題 No.2321 Continuous Flip
ユーザー rniyarniya
提出日時 2023-05-27 00:00:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 1,995 bytes
コンパイル時間 1,978 ms
コンパイル使用メモリ 212,240 KB
実行使用メモリ 30,076 KB
最終ジャッジ日時 2023-08-26 14:10:54
合計ジャッジ時間 10,183 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
4,380 KB
testcase_04 AC 213 ms
24,488 KB
testcase_05 AC 197 ms
24,508 KB
testcase_06 AC 197 ms
24,480 KB
testcase_07 AC 195 ms
24,544 KB
testcase_08 AC 196 ms
24,448 KB
testcase_09 AC 194 ms
24,484 KB
testcase_10 AC 201 ms
24,464 KB
testcase_11 AC 192 ms
24,448 KB
testcase_12 AC 192 ms
24,480 KB
testcase_13 AC 201 ms
24,504 KB
testcase_14 AC 196 ms
24,384 KB
testcase_15 AC 202 ms
24,400 KB
testcase_16 AC 194 ms
24,516 KB
testcase_17 AC 201 ms
24,544 KB
testcase_18 AC 190 ms
24,504 KB
testcase_19 AC 196 ms
24,520 KB
testcase_20 AC 200 ms
24,476 KB
testcase_21 AC 188 ms
24,408 KB
testcase_22 AC 193 ms
24,516 KB
testcase_23 AC 196 ms
24,484 KB
testcase_24 AC 96 ms
18,776 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 81 ms
18,892 KB
testcase_27 AC 86 ms
20,420 KB
testcase_28 AC 171 ms
29,100 KB
testcase_29 AC 170 ms
28,964 KB
testcase_30 AC 232 ms
30,076 KB
testcase_31 AC 220 ms
29,120 KB
testcase_32 AC 218 ms
29,636 KB
testcase_33 AC 226 ms
29,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T> istream& operator>>(istream& is, vector<T>& v) {
    for (auto& x : v) is >> x;
    return is;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    auto sep = "";
    for (const auto& x : v) os << exchange(sep, " ") << x;
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }

template <class T> void mkuni(vector<T>& v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, M, C;
    cin >> N >> M >> C;
    ll sum = 0;
    vector<vector<pair<int, int>>> G(N + 1);
    for (int i = 0; i < N; i++) {
        int A;
        cin >> A;
        sum += A;
        G[i].emplace_back(i + 1, A);
        G[i + 1].emplace_back(i, A);
    }
    for (; M--;) {
        int L, R;
        cin >> L >> R;
        L--;
        G[L].emplace_back(R, C);
        G[R].emplace_back(L, C);
    }

    vector<ll> dp(N + 1, IINF);
    priority_queue<pair<ll, int>> pq;
    dp[0] = 0;
    pq.emplace(-dp[0], 0);
    while (not pq.empty()) {
        auto [val, v] = pq.top();
        pq.pop();
        val *= -1;
        if (dp[v] < val) continue;
        for (auto [u, cost] : G[v]) {
            if (chmin(dp[u], val + cost)) {
                pq.emplace(-dp[u], u);
            }
        }
    }

    sum -= dp.back();
    cout << sum << '\n';
    return 0;
}
0