結果

問題 No.860 買い物
ユーザー 👑 emthrmemthrm
提出日時 2019-08-10 01:33:48
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 186 ms / 1,000 ms
コード長 3,083 bytes
コンパイル時間 1,361 ms
コンパイル使用メモリ 132,224 KB
実行使用メモリ 30,872 KB
最終ジャッジ日時 2023-09-26 22:24:43
合計ジャッジ時間 4,089 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 171 ms
28,704 KB
testcase_08 AC 156 ms
29,740 KB
testcase_09 AC 157 ms
29,472 KB
testcase_10 AC 177 ms
29,068 KB
testcase_11 AC 104 ms
28,464 KB
testcase_12 AC 109 ms
28,676 KB
testcase_13 AC 147 ms
28,300 KB
testcase_14 AC 186 ms
30,872 KB
testcase_15 AC 120 ms
28,752 KB
testcase_16 AC 151 ms
29,716 KB
testcase_17 AC 120 ms
29,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0