17
3 3
1 9
7 8
4 0
2 6
12 5
3 0
0 10
0 11
0 0
5 3
10 10
6 5
8 0
9 2
8 1
5 0
5 1
7
<2829398 bytes omitted>
系统信息
Killed: Segmentation fault
编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#49317 | #1008. H. 开疆辟土,公司成立 | Runtime Error | 0 | 72 ms | 6748 K | C++ 17 (Clang) / 2.0 K | ymz | 2021-06-24 0:39:04 |
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ulli;
#define F(i, n, m) for (int i = n; i <= m; i++)
inline int read() {
bool w = 0;
int s = 0;
char ch = 0;
while (!isdigit(ch)) w = ch == '-', ch = getchar();
while (isdigit(ch)) s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
return w ? -s : s;
}
inline void write(int x) {
if (x < 0)
putchar('-'), x = ~x + 1;
if (x > 9)
write(x / 10);
putchar((x % 10) | 48);
}
class Edge {
public:
int be, ed, co;
Edge(int i, int j, int k) : be(i), ed(j), co(k){};
Edge(){};
friend bool operator>(const Edge &E, const Edge &F) { return E.co > F.co; }
};
int be[25010], n, m;
vector<Edge> edges;
int index(int i, int j);
int kruskal(int n);
int find(int x);
int main() {
int h, ans;
for (int t = read(); t; t--) {
edges.clear();
ans = 0;
n = read();
m = read();
F(i, 1, n) {
F(j, 1, m) {
h = read();
if (i != n) {
ans += h;
edges.push_back(Edge(index(i, j), index(i + 1, j), h));
}
h = read();
if (j != m) {
ans += h;
edges.push_back(Edge(index(i, j), index(i, j + 1), h));
}
}
}
write(ans - kruskal(index(n, m)));
}
return 0;
}
inline int index(int a, int b) { return (a - 1) * m + b; }
int kruskal(int n) {
F(i, 1, n)
be[i] = i;
sort(edges.begin(), edges.end(), greater<Edge>());
int all = 0, ans = 0;
Edge e;
F(i, 0, edges.size() - 1) {
e = edges[i];
if (find(e.be) != find(e.ed)) {
all++;
ans += e.co;
be[e.be] = be[e.ed];
if (all == n - 1)
break;
}
}
return ans;
}
int find(int x) {
if (be[x] == x)
return x;
be[x] = find(be[x]);
return be[x];
}
17
3 3
1 9
7 8
4 0
2 6
12 5
3 0
0 10
0 11
0 0
5 3
10 10
6 5
8 0
9 2
8 1
5 0
5 1
7
<2829398 bytes omitted>
系统信息
Killed: Segmentation fault
1
464 491
795 867
164 87
803 106
734 583
278 855
600 628
824 780
992 364
778 598
747 423
<1999727 bytes omitted>
系统信息
Killed: Segmentation fault
1
491 462
33 971
433 904
185 869
103 87
507 127
238 315
994 56
215 73
771 129
430 505
281
<1990927 bytes omitted>
系统信息
Killed: Segmentation fault