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

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. MonkeyRecorder

    http://www.cnblogs.com/lynn-li/p/5894953.html

  2. 在GitHub上创建上传下载开源项目代码

    1.注册GitHub帐号,创建GitHub项目代码仓库 1.1.注册GitHub帐号 在使GitHub之前,需要先登录其官网注册一个免费使用的账号.登录 https://github.com/join ...

  3. FlexGrid简单demo

    1.首先加入以下代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <hea ...

  4. php sprintf函数

    定义和用法 sprintf() 函数把格式化的字符串写写入一个变量中.sprintf(format,arg1,arg2,arg++) 参数 描述 format 必需.转换格式. arg1 必需.规定插 ...

  5. 用JS的数组缓存一些东西

    var cache_index = new Array(); //首页的ajax缓存 //ajax 推荐的游戏和软件 function change_tuijian(sid,div_class){ i ...

  6. Visual Studio 2015的安装与基本使用

    为什么要使用Visual Studio 2015? 它是中文的.界面友好.自动补全.实时语法错误提示(上图中波浪线部分).单步调试……最重要的社区版是免费的!所以你不必再使用破解的.老旧的的不兼容现代 ...

  7. 使用ActionBar实现Tab导航

    ActionBar还有常用的功能,实现Tab导航.ActionBar在顶端生成多个Tab标签,当用户单击点击某个Tab标签时,系统根据用户点击事件导航指定Tab页面. 为了使用ActionBar实现T ...

  8. win8系统换win7系统

    吐槽一下先,win8换win7好费事~第一次弄,不过总算弄好了,记录一下吧. 首先,最坑人的就是,win8没法像win7那样按F1或者别的,直接进入BIOS,也就没法设置U盘引导,据说是由于win8的 ...

  9. Oracle 11gR2 RAC ohasd failed to start 解决方法

    rcrCRS-4124: Oracle High Availability Services startup failed. CRS-4000: Command Start failed, or co ...

  10. Windows 7 下 PHP 开发环境搭建(手动)

    Windows 7 下 PHP 开发环境搭建 1.说明 做开发的都知道一句话,就是“站在巨人的肩膀上”.确实现在打开浏览器随便一搜很多一键安装PHP环境的软件,比如wamp.xampp.AppServ ...