坏的牛圈建筑

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

  这一题很简单了,就是找最大生成树,把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. CF453C Little Pony and Summer Sun Celebration (DFS)

    http://codeforces.com/contest/456  CF454E Codeforces Round #259 (Div. 1) C Codeforces Round #259 (Di ...

  2. Error: [ng:areq] Argument 'xxxx' is not a function, got undefined

    "Error: [ng:areq] Argument 'keywords' is not a function, got undefined" 代码类似这样的: <div n ...

  3. 两个伪类下特有的属性 content

    更加具体的可以参考:https://developer.mozilla.org/zh-CN/docs/Web/CSS/content

  4. OC第五节 ——点语法和@property

    一.setter和getter函数     1.回忆:如何访问对象中的成员变量    2.setter和getter函数的作用            setter  方法:   修改对象的字段/实例变 ...

  5. IDEA之web项目(maven项目)创建

    1.下载IDEA付费版,有30天的试用期,免费版创建不了web项目(导入不了tomcat). 网址:IntelliJ IDEA :: Download Latest Version of Intell ...

  6. W3Cschool菜鸟教程离线版下载链接

    请在电脑上打开以下链接进行下载w3cschool 离线版(chm):http://pan.baidu.com/s/1bniwRCV(最新,2014年10月21日更新)w3cschool 离线版(htm ...

  7. HDU 1532 最大流模板题

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532 最近在学网络流,学的还不好,先不写理解了,先放模板... 我觉得写得不错的博客:http://blo ...

  8. redhat 中安装rpm包时遇到异常 “error: Failed dependencies:xinetd is needed by .”

    redhat 中安装rpm包时遇到错误 “error: Failed dependencies:xinetd is needed by ....” redhat中安装rpm包时遇到“error: Fa ...

  9. CSU 1503 点到圆弧的距离(2014湖南省程序设计竞赛A题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 解题报告:分两种情况就可以了,第一种是那个点跟圆心的连线在那段扇形的圆弧范围内,这 ...

  10. 跟着百度学PHP[4]-OOP面对对象编程-3-实例化一个对象

    当定义好类后,我们使用new关键字来实例化一个对象! 格式: $object = new 类名; <?php class Person{ private $name; "; priva ...