HDU 4313 Matrix 类似 克鲁斯卡尔算法
2012-08-04 21:37
405 查看
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <set> #include<map> #include<ctime> using namespace std; set<int> Set; struct Edge { int x,y,val; bool operator<(const Edge & a)const { return val>a.val; } }edge[100009]; int n,k,fa[100009]; int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); } void solve() { int x,y,now=0; long long ans=0; for(int i=0;i<=n;i++) { fa[i]=i; } for(int i=0;i<n-1;i++) { x=edge[i].x; y=edge[i].y; x=find(x); y=find(y); if(Set.count(x)!=0&&Set.count(y)!=0) { now++; ans+=edge[i].val; if(now==k-1)break; continue; } if(Set.count(x)!=0) { fa[y]=x; } else { fa[x]=y; } } printf("%I64d\n",ans); } int main() { int t,f; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&k); Set.clear(); for(int i=0;i<n-1;i++) { scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].val); } sort(edge,edge+n-1); for(int i=0;i<k;i++) { scanf("%d",&f); Set.insert(f); } solve(); } }
相关文章推荐
- hdu 4313 Matrix
- HDU 4313 Matrix 树形dp
- HDU 4313 Matrix(并查集/破坏边使得k个点两两不连通的最少代价)
- HDOJ 题目4313 Matrix(类似克鲁斯卡尔)
- HDU 4313 Matrix(并查集|最小生成树变种)
- hdu 4313 Matrix(并查集)
- HDU 4313 Matrix
- HDU 4313 Matrix (贪心)
- hdu 4313 Matrix 并查集 多校联合赛(二) 第四题
- HDU 4313 Matrix (贪心+并查集)
- HDU 4313 Matrix 树状DP
- HDU 4313 Matrix
- HDU 4313 Matrix 贪心 || 树形dp
- HDU 4313 Matrix (最小生成树)
- hdu 4313 - Matrix(最小生成树,并查集)
- HDU 4313 Matrix(贪心+并查集)
- HDU 4313 Matrix (Kruskal应用)
- HDU 4313 Matrix
- hdu 4313 Matrix 树形dp
- hdu 4313 Matrix