QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#398401#3770. Minimum Spanning TreeHjcc#AC ✓530ms22312kbC++14972b2024-04-25 11:40:042024-04-25 11:40:04

Judging History

你现在查看的是最新测评结果

  • [2024-04-25 11:40:04]
  • 评测
  • 测评结果:AC
  • 用时:530ms
  • 内存:22312kb
  • [2024-04-25 11:40:04]
  • 提交

answer

# include <bits/stdc++.h>
# define ll long long

using namespace std;

const int N = 2e6 + 5;
int n, m;
ll l, r, a[N], b[N], ans;
int f[N], u[N], v[N];

int find(int x) {
  return f[x] == x ? x : f[x] = find(f[x]);
}

struct E {
  int u, v;
  ll w;
} e[N];

void marge(E e) {
  int x = find(e.u), y = find(e.v);
  if (x != y) {
    f[x] = y;
    ans += e.w;
  }
}

ll sov(ll k) {
  ans = 0;
  for (int i = 1; i <= n; i++) {
    f[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    e[i] = {u[i], v[i], a[i] + b[i] * k};
  }
  sort(e + 1, e + 1 + m, [](E x, E y){return x.w < y.w;});
  for (int i = 1; i <= m; i++) {
    marge(e[i]);
  }
  return ans;
}

int main() {
  // freopen("in.txt", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  while (cin >> n >> m >> l >> r) {
    for (int i = 1; i <= m; i++) {
      cin >> u[i] >> v[i] >> a[i] >> b[i];
    }
    cout << min(sov(l), sov(r)) << '\n';
  }
}

详细

Test #1:

score: 100
Accepted
time: 530ms
memory: 22312kb

input:

5 6 1 5
1 2 3 1
2 3 5 -1
3 4 1 2
4 5 1 -1
5 1 5 3
2 4 3 -1
5 6 1 5
1 2 1 1
2 3 1 2
3 4 1 -10
3 4 2 10
5 1 3 10
2 4 5 -10
10 10 23 47
5 8 43 13
9 5 55 43
4 9 84 -35
5 10 78 -70
6 4 15 70
7 3 15 67
1 7 2 58
6 7 86 20
5 7 77 7
2 9 32 -91
4 15 82 98
1 2 95 12
4 3 28 100
3 1 98 -25
2 3 39 12
1 2 21 5
1 4...

output:

2
-35
748
-24308
-5125
-4533
-10093
-13414
-13237
-8499
-6782
-6151
-15031
-13554
-50866
-2745
-14186
-5363
-13339
-23410
-5161
-12841
-7280
-22485
-15045
6008
-13410
-17388
-21254
-19576
-10606
-16471
-9741
-37226
-24158
-9383
-20665
-17547
-7919
-11201
-6651
-18673
-5076
-12809
-7285
-18708
-5878
...

result:

ok 53406 lines