1 solutions
-
1
麻了,数据范围是1e4
#include<bits/stdc++.h> using namespace std; #define ioio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); typedef long long ll; const double eps = 5e4 - 6; const int INF = 0x3f3f3f3f; const int N = 1e4 + 7; int fa[N]; int n, e, len = 1; int ma[N][N]; struct st { int a, b, k; } pre[N]; int cmp(st a, st b) { return a.k < b.k; } void init() { for (int i = 0; i <= n; i++) fa[i] = i; } int find(int x) { if (x != fa[x]) fa[x] = find(fa[x]); return fa[x]; } void kru() { int ans = 0; for (int i = 1; i <= len; i++) { int u = pre[i].a; int v = pre[i].b; int w = pre[i].k; int uu = find(u); int vv = find(v); if (uu != vv) { fa[vv] = uu; ans += w; } } printf("%d\n", ans); } void mer(int a, int b) { int x = find(a); int y = find(b); if (x != y) fa[x] = y; } int main() { scanf("%d", &n); init(); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { scanf("%d", &ma[i][j]); pre[len].a = i, pre[len].b = j, pre[len].k = ma[i][j]; len++; } sort(pre + 1, pre + len + 1, cmp); kru(); return 0; }
- 1
Information
- ID
- 1265
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 10
- Tags
- # Submissions
- 6
- Accepted
- 1
- Uploaded By