坏的牛圈建筑

  题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上

  这一题很简单了,就是找最大生成树,把Kruskal算法改一下符号就好了,把边从大到小排列,然后最后再判断是否联通(只要找到他们的根节点是否相同就可以了!)

  

 #include <iostream>
#include <algorithm>
#include <functional>
#define MAX_N 1005
#define MAX 20005 using namespace std; typedef int Position;
typedef struct _edge
{
Position from;
Position to;
int cost;
}Edge_Set;
int fcomp(const void *a, const void *b)
{
return (*(Edge_Set *)b).cost - (*(Edge_Set *)a).cost;
} static Edge_Set edge[MAX];
static Position Set[MAX_N]; Position Find(Position);
void Union(Position, Position);
void Kruskal(const int, const int);
bool Is_Connected(const int); int main(void)
{
int Node_Sum, Path_Sum, cost;
Position tmp_from, tmp_to; while (~scanf("%d%d", &Node_Sum, &Path_Sum))
{
for (int i = ; i < Path_Sum; i++)//读入边
{
scanf("%d%d%d", &tmp_from, &tmp_to, &cost);
edge[i].from = tmp_from; edge[i].to = tmp_to; edge[i].cost = cost;
}
qsort(edge, Path_Sum, sizeof(Edge_Set), fcomp);
Kruskal(Node_Sum, Path_Sum);
}
return ;
} Position Find(Position x)
{
if (Set[x] < )
return x;
else return Set[x] = Find(Set[x]);
} void Union(Position px, Position py)
{
if (Set[px] < Set[py])
{
Set[px] += Set[py];
Set[py] = px;
}
else
{
Set[py] += Set[px];
Set[px] = py;
}
} bool Is_Connected(const int Node_Sum)
{
Position p_u, p_tmp;
p_u = Find();
for (int i = ; i <= Node_Sum; i++)
{
p_tmp = Find(i);
if (p_u != p_tmp)
return false;
}
return true;
} void Kruskal(const int Node_Sum, const int Path_Sum)
{
Position px, py, from, to;
long long ans = ; fill(Set, Set + Node_Sum + , -);
for (int i = ; i < Path_Sum; i++)
{
from = edge[i].from; to = edge[i].to;
px = Find(from); py = Find(to); if (px != py)
{
ans += edge[i].cost;
Union(px, py);
}
}
if (Is_Connected(Node_Sum))
printf("%lld\n", ans);
else
printf("-1\n");
}

MST:Bad Cowtractors(POJ 2377)的更多相关文章

  1. poj 2377 Bad Cowtractors

    题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...

  2. poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)

    http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...

  3. poj 2377 Bad Cowtractors (最大生成树prim)

    Bad Cowtractors Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  4. POJ - 2377 Bad Cowtractors Kru最大生成树

    Bad Cowtractors Bessie has been hired to build a cheap internet network among Farmer John's N (2 < ...

  5. poj 2377 Bad Cowtractors(最大生成树!)

    Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...

  6. POJ 2377 Bad Cowtractors (Kruskal)

    题意:给出一个图,求出其中的最大生成树= =如果无法产生树,输出-1. 思路:将边权降序再Kruskal,再检查一下是否只有一棵树即可,即根节点只有一个 #include <cstdio> ...

  7. POJ 2377 Bad Cowtractors( 最小生成树转化 )

    链接:传送门 题意:给 n 个点 , m 个关系,求这些关系的最大生成树,如果无法形成树,则输出 -1 思路:输入时将边权转化为负值就可以将此问题转化为最小生成树的问题了 /************* ...

  8. POJ 2377

    #include<stdio.h> #define MAXN 1005 #include<iostream> #include<algorithm> #define ...

  9. poj 2377 拉最长的线问题 kruskal算法

    题意:建光纤的时候,拉一条最长的线 思路:最大生成树 将图的n个顶点看成n个孤立的连通分支,并将所有的边按权从大到小排 边权递减的顺序,如果加入边的两个端点不在同一个根节点的话加入,并且要将其连通,否 ...

随机推荐

  1. PHP弹出提示框并跳转到新页面即重定向到新页面

    本文为大家介绍下使用PHP弹出提示框并跳转到新页面,也就是大家所认为的重定向,下面的示例大家可以参考下   这两天写一个demo,需要用到提示并跳转,主要页面要求不高,觉得没必要使用AJAX,JS等, ...

  2. clang

    1.安装 clang 可以从官网下载,如果是CentOS 6 系统,也可以在 /etc/yum.repos.d/ 目录下增加一个 epel.repo 文件,内容如下: [epel] name=Extr ...

  3. EF上下文管理

  4. webpack 教程 那些事儿03-webpack两大精华插件,热加载

    本节主要讲述 webpack的两大经典开发调试插件,热插拔内存缓存机制 文章目录 1. html-webpack-plugin插件的使用 2. webpack-dev-middleware 插件登场 ...

  5. [lintcode] Binary Tree Maximum Path Sum II

    Given a binary tree, find the maximum path sum from root. The path may end at any node in the tree a ...

  6. Android学习笔记(七)——常见控件

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 给我们提供了大量的 UI控件,下面我们简单试试几种常用的控件. 一.TextView 在布局文 ...

  7. WC总结

    去了人生中第一次全国WC,在四川绵阳南山中学举行,去了这么一次,感受颇多,不忍心白白地让时间流逝,于是写篇随笔记录一下. 全程,共计8天. [第1天] 签到,拿餐票,看了看讲义,觉得要狗带. 开营仪式 ...

  8. pro*c调用过程

    数据库内有无参数过程名为procedure. pro*c调用过程 EXEC SQL EXECUTE   BEGIN     procedure;   END; END-EXEC; 需要在cfg配置文件 ...

  9. C++内存分配方式

    参考:http://www.cnblogs.com/daocaoren/archive/2011/06/29/2092957.html http://www.cnblogs.com/skydesign ...

  10. 2016年11月5日--marquee标签、插入百度地图

    <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee标记不仅可以移动文字,也可以移动图片,表格等. 语法:<marquee> ...