我到现在都不知道STL怎么存自定义的数据类型 (我也不知道我怎么红的)
#include <bits/stdc++.h>
using namespace std;
int father[6000];
class SIDE
{
public:
int u;
int v;
int w;
};
inline bool cmp(SIDE a, SIDE b)
{
return a.w < b.w;
}
int find(int x)
{
if (father[x] == x)
return x;
else
return father[x] = find(father[x]);
}
int main()
{
int n, m, cnt, ans;
cin >> n >> m;
list <SIDE> side;
SIDE a;
list <SIDE> ::iterator it;
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &a.u, &a.v, &a.w);
cnt = 0, ans = 0;
for (it = side.begin(); it != side.end(); it++)
if (cmp(a, *it))
side.insert(a, it);//这里插入不了
if (side.size() < i)
side.push_back(a);
for (int j = 1; j <= n; j++)
father[i] = i;
while (cnt < n - 1)
{
if (it == side.end())
{
cout << "-1\n";
goto f1;
}
if (find(*it.u) == find(*it.v))
{
it++;
continue;
}
ans += *it.w;
cnt++;
father[find(*it.u)] = find(*it.v);
it++;
}
cout << ans <<endl;
f1:;
}
return 0;
}