結果
| 問題 | No.2321 Continuous Flip |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-12-13 16:17:51 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 531 ms / 2,000 ms |
| コード長 | 1,763 bytes |
| 記録 | |
| コンパイル時間 | 1,279 ms |
| コンパイル使用メモリ | 106,368 KB |
| 実行使用メモリ | 40,988 KB |
| 最終ジャッジ日時 | 2025-12-13 16:18:08 |
| 合計ジャッジ時間 | 16,672 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 30 |
ソースコード
#include <iostream>
#include <vector>
#include <queue>
#include <limits>
using namespace std;
const long long INF = numeric_limits<long long>::max();
// グラフの定義
struct Edge {
int to;
long long weight;
};
void dijkstra(int start, const vector<vector<Edge>>& graph, vector<long long>& dist) {
// 優先度付きキュー (最小ヒープ)
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
dist[start] = 0;
pq.push({0, start});
while (!pq.empty()) {
auto [current_dist, current_vertex] = pq.top();
pq.pop();
// 最短距離が更新されていない場合はスキップ
if (current_dist > dist[current_vertex]) continue;
for (const auto& edge : graph[current_vertex]) {
long long new_dist = current_dist + edge.weight;
if (new_dist < dist[edge.to]) {
dist[edge.to] = new_dist;
pq.push({new_dist, edge.to});
}
}
}
}
int main() {
int n, m, c; // 頂点数と辺数
cin >> n >> m >> c;
vector<long long> a(n);
for(int i = 0; i < n;i++)cin >> a[i];
vector<vector<Edge>> graph(n+1);
for(int i = 0; i < n; i++){
graph[i].push_back({i+1, a[i]});
graph[i+1].push_back({i, a[i]});
}
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
u--;
graph[u].push_back({v, c});
graph[v].push_back({u, c}); // 無向グラフの場合
}
int start = 0; // 始点
vector<long long> dist(n+1, INF);
dijkstra(start, graph, dist);
long long asum = 0;
// for(int i = 0; i < n + 1; i ++)cout << dist[i] << " ";
// cout << endl;
for(int i = 0; i < n; i++)asum += a[i];
cout << asum - dist[n] << endl;
return 0;
}