-
ACM Contest and Blackout
- UVA - 10600
- 确实是跑完最小生成树之后 无脑枚举所有边去找出一条次小生成树
-
#include<bits/stdc++.h> using namespace std; #define maxn 107 const int INF = 0x3f3f3f3f; int t,n,m,ans,ans2,pre[maxn],fa[maxn],x,y,cnt,cur; struct node { int a, b, c; } a[maxn*maxn]; bool cmp(node x,node y) { return x.c<y.c; } int fond(int x, int pre[]) { return pre[x] == x ? x : pre[x] = fond(pre[x], pre); } void solve() { vector<node> used; sort(a, a+m,cmp); cnt=ans= 0; for(int i=1; i<=n; i++) pre[i] = i; for(int i=0; i<m; i++) { x = fond(a[i].a, pre); y = fond(a[i].b, pre); if(x == y)continue; pre[x] = y; used.push_back(a[i]); ++cnt; ans += a[i].c; if(cnt == n-1) break; } ans2 = INF; int len =used.size(); for(int i=0; i<len; i++) { node v = used[i]; cur =cnt = 0; for(int j=1; j<=n; j++)fa[j]=j; for(int j=0; j<m; j++) { if(v.a == a[j].a && v.b == a[j].b && v.c == a[j].c) continue; int x = fond(a[j].a, fa), y = fond(a[j].b, fa); if(x == y)continue; fa[x] = y; ++cnt; cur += a[j].c; if(cnt == n-1) break; } if(cnt == n-1) ans2 = min(ans2, cur); } } int main() { cin>>t; while(t--) { cin>>n>>m; for(int i=0; i<m; i++) cin>>a[i].a>>a[i].b>>a[i].c; solve(); cout<<ans<<" "<<ans2<<endl; } return 0; }
ACM Contest and Blackout -次小生成树
最新推荐文章于 2021-05-30 15:51:54 发布