contesthunter 6201 走廊泼水节【克鲁斯卡尔+并查集】
2018-07-18 17:26
204 查看
很有意思的题,所以还是截lyddalao的课件
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int N=6005; int T,n,f ,s ; long long ans; struct qwe { int u,v,w; }a ; bool cmp(const qwe &a,const qwe &b) { return a.w<b.w; } int read() { int r=0,f=1; char p=getchar(); while(p>'9'||p<'0') { if(p=='-') f=-1; p=getchar(); } while(p>='0'&&p<='9') { r=r*10+p-48; p=getchar(); } return r*f; } int zhao(int x) { return x==f[x]?x:zhao(f[x]); } int main() { T=read(); while(T--) { n=read();ans=0; for(int i=1;i<n;i++) a[i].u=read(),a[i].v=read(),a[i].w=read(); sort(a+1,a+n,cmp); for(int i=1;i<=n;i++) f[i]=i,s[i]=1; for(int i=1;i<n;i++) { int fu=zhao(a[i].u),fv=zhao(a[i].v); ans+=1ll*s[fu]*s[fv]*(a[i].w+1)-a[i].w-1;//cerr<<ans<<endl; f[fu]=fv; s[fv]+=s[fu]; } printf("%lld\n",ans); } return 0; }
相关文章推荐
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
- 贪心生成最小生成树-克鲁斯卡尔(Kruskal)算法(归并排序)(并查集)
- HDU 3371 Connect the Cities (最小生成树 并查集+克鲁斯卡尔)
- HDU-3938 Portal 克鲁斯卡尔思想 (离线并查集)
- 深度优先DFS和广度优先BFS,破圈法,拓扑序列,prim,克鲁斯卡尔等生成算法(需要用到并查集)迪杰斯特拉算法和弗洛伊德的总结
- 最小生成树算法汇总 (普里姆 && 克鲁斯卡尔与并查集结合)
- hdu 1879(并查集+克鲁斯卡尔)
- POJ 1258 Agri-Net 克鲁斯卡尔(Kruskal)算法&并查集
- PKUACM 2018 D chocolate【并查集+克鲁斯卡尔】
- 51nod 1640天气晴朗的魔法(克鲁斯卡尔,并查集)
- hdu1102 克鲁斯卡尔+并查集
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
- bzoj 3732: Network【克鲁斯卡尔+树链剖分】
- tyvj 1391 走廊泼水节【最小生成树】By cellur925
- HDU-1233-还是畅通工程(最小生成树 克鲁斯卡尔)
- 普里姆算法(Prim)和克鲁斯卡尔(Kruskal)算法
- C语言-数据结构-克鲁斯卡尔kruskal
- 最小生成树,普里母和克鲁斯卡尔
- POJ 3522 Slim Span(生成树+克鲁斯卡尔)
- poj 1751 Highways 最小生成树之Kruskal(克鲁斯卡尔)算法