結果

問題 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
コンパイル時間 518 ms
コンパイル使用メモリ 61,568 KB
実行使用メモリ 12,352 KB
最終ジャッジ日時 2023-08-19 15:48:44
合計ジャッジ時間 20,482 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,776 KB
testcase_01 AC 3 ms
5,780 KB
testcase_02 AC 3 ms
5,788 KB
testcase_03 AC 2 ms
5,780 KB
testcase_04 AC 2 ms
5,796 KB
testcase_05 AC 2 ms
5,756 KB
testcase_06 AC 3 ms
5,880 KB
testcase_07 AC 3 ms
5,816 KB
testcase_08 AC 3 ms
5,792 KB
testcase_09 AC 9 ms
6,192 KB
testcase_10 AC 2 ms
5,776 KB
testcase_11 AC 3 ms
5,740 KB
testcase_12 AC 4 ms
5,848 KB
testcase_13 AC 2 ms
5,804 KB
testcase_14 AC 3 ms
5,848 KB
testcase_15 AC 3 ms
5,796 KB
testcase_16 AC 2 ms
5,772 KB
testcase_17 AC 8 ms
6,048 KB
testcase_18 AC 30 ms
7,048 KB
testcase_19 AC 52 ms
7,660 KB
testcase_20 AC 37 ms
7,768 KB
testcase_21 AC 109 ms
10,516 KB
testcase_22 AC 2 ms
5,716 KB
testcase_23 AC 3 ms
5,800 KB
testcase_24 AC 3 ms
5,804 KB
testcase_25 AC 3 ms
5,776 KB
testcase_26 AC 3 ms
5,744 KB
testcase_27 AC 4 ms
5,960 KB
testcase_28 AC 5 ms
5,944 KB
testcase_29 AC 9 ms
6,388 KB
testcase_30 AC 3 ms
5,844 KB
testcase_31 AC 2 ms
5,732 KB
testcase_32 AC 39 ms
7,604 KB
testcase_33 WA -
testcase_34 AC 30 ms
7,280 KB
testcase_35 AC 51 ms
8,156 KB
testcase_36 AC 48 ms
7,944 KB
testcase_37 AC 21 ms
6,552 KB
testcase_38 AC 314 ms
12,352 KB
testcase_39 AC 93 ms
11,172 KB
testcase_40 AC 267 ms
11,648 KB
testcase_41 AC 85 ms
10,908 KB
testcase_42 AC 8 ms
6,032 KB
testcase_43 AC 74 ms
9,804 KB
testcase_44 AC 2 ms
5,856 KB
testcase_45 AC 16 ms
6,240 KB
testcase_46 AC 56 ms
7,636 KB
testcase_47 AC 11 ms
6,124 KB
testcase_48 AC 14 ms
6,456 KB
testcase_49 AC 3 ms
5,840 KB
testcase_50 AC 4 ms
5,908 KB
testcase_51 AC 4 ms
5,844 KB
testcase_52 AC 6 ms
6,068 KB
testcase_53 AC 20 ms
6,824 KB
testcase_54 AC 23 ms
7,112 KB
testcase_55 AC 42 ms
7,100 KB
testcase_56 AC 29 ms
7,324 KB
testcase_57 WA -
testcase_58 AC 179 ms
10,936 KB
testcase_59 AC 155 ms
10,904 KB
testcase_60 AC 156 ms
10,992 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