题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 2010, M = 10010;
int n, m;
struct Edge{
int a, b, c;
bool operator<(const Edge& W)const
{
return c < W.c;
}
}e[M];
int p[N];
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i ++) p[i] = i;
int cnt = 0, res = 0;
for (int i = 0; i < m; i ++)
{
int u, a, b, c;
cin >> u >> a >> b >> c;
if (u == 1)
{
res += c;
p[find(a)] = find(b);
}
else e[cnt ++] = {a, b, c};
}
sort(e, e + cnt);
for (int i = 0; i < cnt; i ++)
{
int a = find(e[i].a), b = find(e[i].b), w = e[i].c;
if (a != b)
{
res += w;
p[a] = b;
}
}
cout << res;
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla