답안 #619855

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
619855 2022-08-02T16:34:41 Z chuangsheep 시간이 돈 (balkan11_timeismoney) C++11
50 / 100
464 ms 808 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using pii = pair<ll, ll>;
using Edge = array<ll, 5>;

struct DSU
{
  vector<ll> g;
  DSU(ll n) : g(n, -1) {}
  ll find(ll a)
  {
    if (g[a] < 0)
      return a;
    else
      return g[a] = find(g[a]);
  }
  bool unite(ll a, ll b)
  {
    a = find(a);
    b = find(b);
    if (a == b)
      return false;
    if (g[a] > g[b])
      swap(a, b);
    g[a] += g[b];
    g[b] = a;
    return true;
  }
};

pair<pii, vector<ll>> kruskal(ll n, vector<Edge> &edges, vector<ll> &weights)
{
  sort(begin(edges), end(edges), [&](const Edge &e1, const Edge &e2)
       { return weights[e1[4]] < weights[e2[4]]; });
  DSU dsu(n);
  vector<ll> mst;
  ll t = 0;
  ll c = 0;
  for (Edge &e : edges)
  {
    if (dsu.unite(e[0], e[1]))
    {
      n--;
      t += e[2];
      c += e[3];
      mst.push_back(e[4]);
    }
  }
  return {{t, c}, mst};
}

ll cross(pii A, pii B, pii C)
{
  pii AB = {B.first - A.first, B.second - A.second};
  pii AC = {C.first - A.first, C.second - A.second};
  return AB.first * AC.second - AB.second * AC.first;
}

int main()
{
#if defined(DEBUG) && !defined(ONLINE_JUDGE)
  // DEBUG
  cerr << "DEBUG flag set - see test.out for output\n";
  freopen("/home/chuang/shared-drives/G:/repos/cp/ois/boi11/timeismoney/test.in", "r", stdin);
  freopen("/home/chuang/shared-drives/G:/repos/cp/ois/boi11/timeismoney/test.out", "w", stdout);
#else
  cin.tie(0)->sync_with_stdio(false);
#endif

  ll N, M;
  cin >> N >> M;

  vector<Edge> edges(M);
  for (ll i = 0; i < M; i++)
  {
    ll x, y, t, c;
    cin >> x >> y >> t >> c;
    edges[i] = {x, y, t, c, i};
  }

  vector<ll> weights(M);

  for (ll i = 0; i < M; i++)
  {
    weights[edges[i][4]] = edges[i][2];
  }
  pii A = kruskal(N, edges, weights).first;
  for (ll i = 0; i < M; i++)
  {
    weights[edges[i][4]] = edges[i][3];
  }
  pii B = kruskal(N, edges, weights).first;

  stack<pair<pii, pii>> to_search;
  to_search.push({A, B});

  vector<ll> best_MST;
  pii best_V = {1e8, 1e8};
  while (!to_search.empty())
  {
    tie(A, B) = to_search.top();
    to_search.pop();

    for (ll i = 0; i < M; i++)
    {
      weights[edges[i][4]] = edges[i][3] * (B.first - A.first) -
                             edges[i][2] * (B.second - A.second);
    }
    pii C;
    vector<ll> current_MST;
    tie(C, current_MST) = kruskal(N, edges, weights);

    if (cross(A, B, C) >= 0)
      continue;

    if ((ll)C.first * C.second < (ll)best_V.first * best_V.second)
    {
      best_V = C;
      best_MST = current_MST;
    }

    to_search.push({A, C});
    to_search.push({C, B});
  }

  cout << best_V.first << " " << best_V.second << "\n";
  sort(begin(edges), end(edges), [](const Edge &e1, const Edge &e2)
       { return e1[4] < e2[4]; });
  for (ll &id : best_MST)
  {
    cout << edges[id][0] << " " << edges[id][1] << "\n";
  }
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Incorrect 0 ms 212 KB Output isn't correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Incorrect 0 ms 212 KB Output isn't correct
5 Incorrect 1 ms 340 KB Output isn't correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Incorrect 1 ms 340 KB Output isn't correct
8 Incorrect 5 ms 724 KB Output isn't correct
9 Incorrect 0 ms 212 KB Output isn't correct
10 Incorrect 0 ms 212 KB Output isn't correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 4 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 50 ms 408 KB Output is correct
17 Correct 52 ms 340 KB Output is correct
18 Correct 49 ms 340 KB Output is correct
19 Correct 428 ms 808 KB Output is correct
20 Correct 464 ms 724 KB Output is correct