結果

問題 No.1898 Battle and Exchange
ユーザー tnakao0123tnakao0123
提出日時 2022-04-09 12:02:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,392 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 62,536 KB
実行使用メモリ 12,436 KB
最終ジャッジ日時 2024-05-06 22:52:47
合計ジャッジ時間 15,017 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 4 ms
6,976 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 10 ms
7,104 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 4 ms
7,232 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 4 ms
7,620 KB
testcase_15 AC 3 ms
6,980 KB
testcase_16 AC 3 ms
7,232 KB
testcase_17 AC 9 ms
6,940 KB
testcase_18 AC 31 ms
8,256 KB
testcase_19 AC 56 ms
8,128 KB
testcase_20 AC 41 ms
8,252 KB
testcase_21 AC 122 ms
10,560 KB
testcase_22 AC 3 ms
7,612 KB
testcase_23 AC 3 ms
7,960 KB
testcase_24 AC 3 ms
7,364 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 AC 4 ms
6,948 KB
testcase_27 AC 4 ms
7,488 KB
testcase_28 AC 5 ms
6,944 KB
testcase_29 AC 11 ms
7,108 KB
testcase_30 AC 4 ms
7,356 KB
testcase_31 AC 3 ms
6,940 KB
testcase_32 AC 46 ms
8,384 KB
testcase_33 WA -
testcase_34 AC 35 ms
8,516 KB
testcase_35 AC 57 ms
9,152 KB
testcase_36 AC 55 ms
8,256 KB
testcase_37 AC 22 ms
7,104 KB
testcase_38 AC 336 ms
12,436 KB
testcase_39 AC 101 ms
11,420 KB
testcase_40 AC 288 ms
12,188 KB
testcase_41 AC 91 ms
11,160 KB
testcase_42 AC 10 ms
6,940 KB
testcase_43 AC 77 ms
10,260 KB
testcase_44 AC 4 ms
6,944 KB
testcase_45 AC 18 ms
7,232 KB
testcase_46 AC 60 ms
7,744 KB
testcase_47 AC 12 ms
7,232 KB
testcase_48 AC 14 ms
6,940 KB
testcase_49 AC 5 ms
6,940 KB
testcase_50 AC 5 ms
6,944 KB
testcase_51 AC 5 ms
7,104 KB
testcase_52 AC 7 ms
6,948 KB
testcase_53 AC 23 ms
8,004 KB
testcase_54 AC 29 ms
8,256 KB
testcase_55 AC 44 ms
7,872 KB
testcase_56 AC 33 ms
8,644 KB
testcase_57 WA -
testcase_58 AC 191 ms
11,072 KB
testcase_59 AC 164 ms
11,072 KB
testcase_60 AC 332 ms
11,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1898.cc:  No.1898 Battle and Exchange - yukicoder
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_S = 300000000;

/* typedef */

typedef vector<int> vi;
typedef pair<int,int> pii;

struct Card {
  int a, b, c;
  Card() {}
  Card(int _a, int _b, int _c): a(_a), b(_b), c(_c) {}
  Card(int s): a(1), b(1), c(s - 2) {}

  void sort() {
    if (a > b) swap(a, b);
    if (b > c) swap(b, c);
    if (a > b) swap(a, b);
  }
  
  void read() { scanf("%d%d%d", &a, &b, &c); sort(); }

  void takeone(Card &cd) {
    if (a < cd.c) {
      swap(a, cd.c);
      sort(), cd.sort();
    }
  }

  void takeall(Card &cd) {
    while (a < cd.c) takeone(cd);
  }

  int sum() const { return a + b + c; }
  bool operator<(const Card &cd) const { return sum() < cd.sum(); }
  bool operator>(const Card &cd) const { return sum() > cd.sum(); }
  bool operator<=(const Card &cd) const { return sum() <= cd.sum(); }
  bool operator>=(const Card &cd) const { return sum() >= cd.sum(); }
};

/* global variables */

vi nbrs[MAX_N];
Card cds[MAX_N], vcds[MAX_N];
bool ds[MAX_N];

/* subroutines */

bool check(int n, int s) {
  Card ucd(s);
  if (cds[0] >= ucd) return false;

  copy(cds, cds + n, vcds);
  fill(ds, ds + n, false);
  ds[0] = true;
  int dn = 1;

  priority_queue<pii> q;
  q.push(pii(-vcds[0].sum(), 0));

  bool gl = false;
  while (! q.empty()) {
    pii up = q.top(); q.pop();
    int us = -up.first, u = up.second;
    if (us >= ucd.sum()) break;
    if (u == n - 1) {
      gl = true;
      break;
    }

    if (dn == 1) ucd.takeone(vcds[u]);
    else ucd.takeall(vcds[u]);

    for (auto v: nbrs[u]) {
      if (! ds[v]) {
	ds[v] = true;
	dn++;
	q.push(pii(-vcds[v].sum(), v));
      }
    }

    if (u == 0 && dn > 1) ucd.takeall(vcds[0]);
  }

  //printf("check(%d) = %d\n", s, gl);
  return gl;
}

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    nbrs[u].push_back(v);
    nbrs[v].push_back(u);
  }

  for (int i = 0; i < n; i++) cds[i].read();

  int s0 = 3, s1 = MAX_S + 1;
  while (s0 + 1 < s1) {
    int s = (s0 + s1) / 2;
    if (check(n, s)) s1 = s;
    else s0 = s;
  }
  //printf("s1=%d\n", s1);

  printf("1 1 %d\n", s1 - 2);
  return 0;
}
0