好吧这个题没什么可说的,有点....
一开始还和一位大佬在讨论,会不会有多余的边,后面看了题发现没有多于的边和自环,所以之间一波最大生成树把最大的边去掉,把最小的边推倒就行了。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100000 + 5, maxm = 200000 + 5; int n, m, ans, head[maxn]; bool vis[maxm]; struct Edge { int u, v, w; } edge[maxm]; bool cmp(const Edge &a, const Edge &b) { return a.w > b.w; } int Find(int x) { if(x == head[x]) return x; else return head[x] = Find(head[x]); } int main() { int x, y; while(~scanf("%d %d", &n, &m)) { ans = 0; memset(vis, false, sizeof vis); for(int i = 1; i <= n; i ++) { scanf("%d %d", &x, &y); head[i] = i; } for(int i = 0; i < m; i ++) { scanf("%d %d %d", &edge[i].u, &edge[i].v, &edge[i].w); } sort(edge, edge + m, cmp); int cnt = 0, num = 0; for(int i = 0; i < m; i ++) { int fx = Find(edge[i].u), fy = Find(edge[i].v); if(fx != fy) { cnt ++; head[fx] = fy; vis[i] = true; } if(cnt == n - 1) break; } for(int i = 0; i < m; i ++) { if(!vis[i]) { ans += edge[i].w; num ++; } } printf("%d %d ", num, ans); } return 0; }