結果

問題 No.2321 Continuous Flip
ユーザー zfs 732
提出日時 2025-08-26 16:04:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 1,056 bytes
コンパイル時間 2,016 ms
コンパイル使用メモリ 211,448 KB
実行使用メモリ 30,232 KB
最終ジャッジ日時 2025-08-26 16:04:30
合計ジャッジ時間 10,581 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

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

	int N, M, C;
	long long S = 0;
	std::cin >> N >> M >> C;
	std::vector<std::vector<std::pair<int, int>>> graph(N + 1);
	
	for (int i = 0, v; i < N; i++) {
	  std::cin >> v, S += v;
	  graph[i].emplace_back(i + 1, v);
	  graph[i + 1].emplace_back(i, v);
	}
	
	for (int i = 0, l, r, v; i < M; i++) {
	  std::cin >> l >> r, l--;
	  graph[l].emplace_back(r, C);
	  graph[r].emplace_back(l, C);
	}
	
	std::vector<bool> vis(N + 1);
	std::vector<long long> dist(N + 1, LLONG_MAX);
	std::priority_queue<std::pair<long long, int>> pq;
	pq.emplace(dist[0] = 0, 0);
	while (!pq.empty()) {
	  int u = pq.top().second;
	  pq.pop();
	  if (vis[u]) { continue; }
	  vis[u] = true;
	  for (auto t: graph[u]) {
	    int v, w;
	    std::tie(v, w) = t;
	    if (dist[u] + w < dist[v]) { pq.emplace(-(dist[v] = dist[u] + w), v); }
	  }
	}
	std::cout << S - dist[N] << '\n';

  return 0;
}

/*
g++ card.cpp -o card -std=c++14 -O2 -Wl,--stack=999999999 -Wall -Wextra && card
*/
0