1 solutions
-
0
C++ :
#include <bits/stdc++.h> using namespace std; /* n个点,m个已知关系,要组成k个集合,最小代价是多少 */ struct node{ int x,y,len; }; node a[10010]; int f[1010]; int n,m,k; bool cmp(node n1,node n2){ return n1.len < n2.len; } int find(int x){ return x == f[x]?x:f[x] = find(f[x]); } int main(){ cin>>n>>m>>k; for(int i = 1;i <= m;i++){ cin>>a[i].x>>a[i].y>>a[i].len; } sort(a+1,a+1+m,cmp); for(int i = 1;i <= n;i++){ f[i] = i; } //修路 int s = 0,c = 0; for(int i = 1;i <= m;i++){ int fx = find(a[i].x); int fy = find(a[i].y); if(fx != fy){ f[fx] = fy; c++; s = s + a[i].len; } if(c == n - k){ cout<<s; return 0; } } cout<<"No Answer"; return 0; }
- 1
Information
- ID
- 10032
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By