結果
| 問題 |
No.860 買い物
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2019-08-10 01:33:48 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 163 ms / 1,000 ms |
| コード長 | 3,083 bytes |
| コンパイル時間 | 1,379 ms |
| コンパイル使用メモリ | 129,020 KB |
| 最終ジャッジ日時 | 2025-01-07 11:36:55 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 15 |
ソースコード
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
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()
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
struct UnionFind {
UnionFind(int n) : data(n, -1) {}
int root(int ver) { return data[ver] < 0 ? ver : data[ver] = root(data[ver]); }
void unite(int ver1, int ver2) {
ver1 = root(ver1);
ver2 = root(ver2);
if (ver1 != ver2) {
if (data[ver1] > data[ver2]) swap(ver1, ver2);
data[ver1] += data[ver2];
data[ver2] = ver1;
}
}
bool same(int ver1, int ver2) { return root(ver1) == root(ver2); }
int size(int ver) { return -data[root(ver)]; }
private:
vector<int> data;
};
using CostType = long long;
struct Edge {
int src, dst; CostType cost;
Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
inline bool operator<(const Edge &rhs) const {
return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
}
inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; }
inline bool operator>(const Edge &rhs) const {
return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src;
}
inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; }
};
CostType kruskal(const vector<vector<Edge> > &graph) {
int n = graph.size();
priority_queue<Edge, vector<Edge>, greater<Edge> > que;
REP(i, n) {
for (Edge e : graph[i]) que.emplace(e);
}
vector<vector<Edge> > res(n);
CostType total = 0;
UnionFind uf(n);
while (!que.empty()) {
Edge e = que.top(); que.pop();
if (!uf.same(e.src, e.dst)) {
res[e.src].emplace_back(e);
res[e.dst].emplace_back(Edge(e.dst, e.src, e.cost));
total += e.cost;
uf.unite(e.src, e.dst);
}
}
return total;
}
int main() {
cin.tie(0); ios::sync_with_stdio(false);
// freopen("input.txt", "r", stdin);
int n; cin >> n;
vector<vector<Edge> > graph(n + 1);
long long ans = 0;
REP(i, n) {
int c, d; cin >> c >> d;
ans += c;
graph[i].emplace_back(Edge(i, n, c));
graph[n].emplace_back(Edge(n, i, c));
if (i > 0) {
graph[i - 1].emplace_back(Edge(i - 1, i, d));
graph[i].emplace_back(Edge(i, i - 1, d));
}
}
cout << ans + kruskal(graph) << '\n';
return 0;
}
emthrm