結果

問題 No.2321 Continuous Flip
ユーザー 👑 emthrmemthrm
提出日時 2023-05-27 00:05:37
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 306 ms / 2,000 ms
コード長 2,921 bytes
コンパイル時間 2,897 ms
コンパイル使用メモリ 261,700 KB
実行使用メモリ 60,696 KB
最終ジャッジ日時 2023-08-26 14:13:55
合計ジャッジ時間 12,792 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 296 ms
52,076 KB
testcase_05 AC 295 ms
52,176 KB
testcase_06 AC 294 ms
52,064 KB
testcase_07 AC 299 ms
52,076 KB
testcase_08 AC 300 ms
52,200 KB
testcase_09 AC 298 ms
52,116 KB
testcase_10 AC 285 ms
52,180 KB
testcase_11 AC 291 ms
52,080 KB
testcase_12 AC 288 ms
52,148 KB
testcase_13 AC 292 ms
52,116 KB
testcase_14 AC 289 ms
52,196 KB
testcase_15 AC 301 ms
52,068 KB
testcase_16 AC 291 ms
52,068 KB
testcase_17 AC 299 ms
52,084 KB
testcase_18 AC 287 ms
52,364 KB
testcase_19 AC 288 ms
52,144 KB
testcase_20 AC 289 ms
52,208 KB
testcase_21 AC 295 ms
52,060 KB
testcase_22 AC 306 ms
52,180 KB
testcase_23 AC 289 ms
52,096 KB
testcase_24 AC 122 ms
46,184 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 109 ms
45,952 KB
testcase_27 AC 116 ms
48,820 KB
testcase_28 AC 233 ms
59,620 KB
testcase_29 AC 235 ms
60,696 KB
testcase_30 AC 236 ms
59,636 KB
testcase_31 AC 251 ms
59,212 KB
testcase_32 AC 231 ms
59,840 KB
testcase_33 AC 238 ms
59,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename CostType>
struct Edge {
  CostType cost;
  int src, dst;

  explicit Edge(const int src, const int dst, const CostType cost = 0)
      : cost(cost), src(src), dst(dst) {}

  auto operator<=>(const Edge& x) const = default;
};

template <typename CostType>
struct Dijkstra {
  const CostType inf;

  Dijkstra(const std::vector<std::vector<Edge<CostType>>>& graph,
           const CostType inf = std::numeric_limits<CostType>::max())
      : inf(inf), is_built(false), graph(graph) {}

  std::vector<CostType> build(const int s) {
    is_built = true;
    const int n = graph.size();
    std::vector<CostType> dist(n, inf);
    dist[s] = 0;
    prev.assign(n, -1);
    std::priority_queue<std::pair<CostType, int>,
                        std::vector<std::pair<CostType, int>>,
                        std::greater<std::pair<CostType, int>>> que;
    que.emplace(0, s);
    while (!que.empty()) {
      const auto [d, ver] = que.top();
      que.pop();
      if (d > dist[ver]) continue;
      for (const Edge<CostType>& e : graph[ver]) {
        if (dist[ver] + e.cost < dist[e.dst]) {
          dist[e.dst] = dist[ver] + e.cost;
          prev[e.dst] = ver;
          que.emplace(dist[e.dst], e.dst);
        }
      }
    }
    return dist;
  }

  std::vector<int> build_path(int t) const {
    assert(is_built);
    std::vector<int> res;
    for (; t != -1; t = prev[t]) {
      res.emplace_back(t);
    }
    std::reverse(res.begin(), res.end());
    return res;
  }

 private:
  bool is_built;
  std::vector<int> prev;
  std::vector<std::vector<Edge<CostType>>> graph;
};

int main() {
  int n, m, c; cin >> n >> m >> c;
  ll ans = 0;
  vector<vector<Edge<ll>>> graph(n + 1);
  REP(i, n) {
    int a; cin >> a;
    ans += a;
    graph[i].emplace_back(i, i + 1, a);
    graph[i + 1].emplace_back(i + 1, i, a);
  }
  while (m--) {
    int l, r; cin >> l >> r; --l; --r;
    graph[l].emplace_back(l, r + 1, c);
    graph[r + 1].emplace_back(r + 1, l, c);
  }
  cout << ans - Dijkstra(graph).build(0)[n] << '\n';
  return 0;
}
0