这很明显就是最大生成树= =

CODE:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 1010
#define maxm 20010
struct edges{
 int x,y,z;
}edge[maxm];
bool cmp(edges a,edges b){return (a.z>b.z);}
int f[maxn];
int fin(int x){if (f[x]!=x) f[x]=fin(f[x]);return f[x];}
int main(){
 int n,m;
 scanf("%d%d",&n,&m);
 for (int i=1;i<=n;i++) f[i]=i;
 for (int i=1;i<=m;i++) scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].z);
 sort(edge+1,edge+1+m,cmp);
 int sum=0,ans=0;
 for (int i=1;i<=m;i++){
  int x=fin(edge[i].x),y=fin(edge[i].y);
  if (x!=y) { f[x]=y;sum++;ans+=edge[i].z;if (sum==n-1) {printf("%d",ans);return 0;}}
 }
 printf("-1\n");
 return 0;
}

BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(最大生成树)的更多相关文章

  1. bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 -- 最大生成树

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MB Description     奶牛贝 ...

  2. BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    题目 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 53  Solve ...

  3. bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复【最大生成树】

    裸的最大生成树,注意判不连通情况 #include<iostream> #include<cstdio> #include<algorithm> using nam ...

  4. 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 69  Solved:  ...

  5. 【BZOJ】3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(kruskal)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3390 .. #include <cstdio> #include <cstring ...

  6. BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 43  Solved:  ...

  7. BZOJ 3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    题目 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MB Description      ...

  8. Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 218  Solved: ...

  9. BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏( dfs )

    因为是棵树 , 所以直接 dfs 就好了... ---------------------------------------------------------------------------- ...

随机推荐

  1. MPU6050程序(转)

    源:MPU6050程序 初始化定义 #ifndef _MPU6050_H #define _MPU6050_H #define PORT_USED 0 #define MPU6050_ADDRESS_ ...

  2. C++的封装性

    C++的封装性 C++的阶段,我想根据C++的一些特有的特性分别写一些专题,每个专题我都捎带讲一些语法,当然不会很多,我还是会像C语言那样,内存结构贯穿始终,有汇编就有真相…… 本专题,我们讲述封装性 ...

  3. Python3基础 setdefault() 根据键查找值,找不到键会添加

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...

  4. 解callback嵌套

    function checkPassword(username,password,callback){ var pwdHash; var queryStr = 'select * from user ...

  5. 013-Cookie状态保持

    常用的状态(信息)保持方式(重点) ViewState: ASP.NET 的 .aspx页面特有,页面级的: 就是在页面上的一个隐藏域中保存客户端单独使用的数据的一种方式: 服务器端控件的值都自动保存 ...

  6. mongoDB查询及游标

    find文档 1.find简介 使用find查询集合中符合条件的子集合 db.test.blog.find(); 类似于sql查询 select * from test.blog 上面的查询是返回多有 ...

  7. 怎样在iis中发布asp.net网站

    以windows server2003.vs2008和sql servber2005为例.将开发完成的asp.net网站发布,将发布包放在windows server2003服务器的文件夹下.将web ...

  8. Spring AOP切面的时候参数的传递

    Spring AOP切面的时候参数的传递 Xml: <?xml version="1.0" encoding="UTF-8"?> <beans ...

  9. loadrunner:参数类型及其取值机制

    参数类型 参数名随意取,建议取通俗易懂的名字,下面我们重点介绍一下参数的类型. ●DateTime: 很简单, 在需要输入日期/时间的地方, 可以用DateTime 类型来替代. 其属性设置也很简单, ...

  10. Js获取复选框checkbox的值

    var emps= $("input[name='emp']:checked"); var employee= ""; emps.each(function() ...