Submission #660807

# Submission time Handle Problem Language Result Execution time Memory
660807 2022-11-23T09:10:55 Z 600Mihnea timeismoney (balkan11_timeismoney) C++17
100 / 100
436 ms 604 KB
bool home = 0;

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Edge
{
  int a;
  int b;
  int x;
  int y;
};

ll coef;
ll total;

bool operator < (Edge first, Edge second)
{
  return coef * first.x + (total - coef) * first.y < coef * second.x + (total - coef) * second.y;
}

int n;
int m;
vector<Edge> edges;
vector<int> t;
vector<int> P;
vector<int> S;

void clr()
{
  t.resize(n);
  iota(t.begin(), t.end(), 0);
}

int root(int a)
{
  if (t[a] == a)
  {
    return a;
  }
  else
  {
    return t[a] = root(t[a]);
  }
}

void join(int a, int b)
{
  t[root(a)] = root(b);
}

ll get(ll c)
{
  P.clear();
  coef = c;
  sort(edges.begin(), edges.end());
  clr();
  int X = 0, Y = 0;
  vector<pair<int, int>> T;
  for (auto &it : edges)
  {
    if (root(it.a) != root(it.b))
    {
      join(it.a, it.b);
      X += it.x;
      Y += it.y;
      T.push_back({it.a, it.b});
    }
  }
  P.push_back(X);
  P.push_back(Y);
  for (auto &IT : T)
  {
    P.push_back(IT.first);
    P.push_back(IT.second);
  }
  return 1LL * X * Y;
}

int main()
{
  if (home == 0)
  {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  }
  else
  {
    freopen ("input.txt", "r", stdin);
  }

  cin >> n >> m;
  edges.resize(m);
  for (auto &it : edges)
  {
    cin >> it.a >> it.b >> it.x >> it.y;
  }

  total = (ll) 1e9;

  ll sol = (ll) 1e18;

  for (ll c = 0; c <= total; c += (ll) 1e6)
  {
    ll cur = get(c);
    if (cur < sol)
    {
      sol = cur;
      S = P;
    }
  }

  ///cout << sol << "\n";
  for (int i = 0; i < (int) S.size(); i++)
  {
    cout << S[i] << " ";
    if (i % 2 == 1)
    {
      cout << "\n";
    }
  }

  return 0;
}

Compilation message

timeismoney.cpp: In function 'int main()':
timeismoney.cpp:91:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |     freopen ("input.txt", "r", stdin);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 3 ms 316 KB Output is correct
5 Correct 12 ms 328 KB Output is correct
6 Correct 12 ms 212 KB Output is correct
7 Correct 60 ms 340 KB Output is correct
8 Correct 269 ms 588 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 4 ms 212 KB Output is correct
13 Correct 3 ms 212 KB Output is correct
14 Correct 15 ms 340 KB Output is correct
15 Correct 15 ms 212 KB Output is correct
16 Correct 78 ms 368 KB Output is correct
17 Correct 77 ms 340 KB Output is correct
18 Correct 76 ms 380 KB Output is correct
19 Correct 424 ms 604 KB Output is correct
20 Correct 436 ms 596 KB Output is correct