题目链接:

https://vjudge.net/problem/POJ-2377

题目大意:

给一个图,求最大生成树权值,如果不连通输出-1

思路:

kruskal算法变形,sort按边从大到小排序,就可以了,或者用一个maxn-w[u][v]作为<u, v>边的权值,直接用原来的kruskal算法求出权值,然后用maxn*(n-1)-sum就为最大生成树权值

 #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<map>
#include<cmath>
using namespace std;
typedef pair<int, int> Pair;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int T, n, m;
const int maxn = 2e4 + ;
struct edge
{
int v, u, w;
bool operator < (const edge a)const
{
return w > a.w;
}
};
edge e[maxn];
int pa[maxn];
int Find(int x)
{
return x == pa[x] ? x : pa[x] = Find(pa[x]);//路径压缩
}
void kruskal()
{
for(int i = ; i <= n; i++)pa[i] = i;
sort(e, e + m);
ll ans = ;
for(int i = ; i < m; i++)
{
ll v = e[i].v, u = e[i].u, w = e[i].w;
ll x = Find(v), y = Find(u);
if(x != y)
{
pa[x] = y;
ans += w;
}
}
int tot = ;
for(int i = ; i <= n; i++)//判断是否联通,是否只有一个父节点是自己的点
{
if(pa[i] == i)tot++;
}
if(tot > )cout<<"-1"<<endl;
else cout<<ans<<endl;
}
int main()
{
cin >> n >> m;
for(int i = ; i < m; i++)cin >> e[i].v >> e[i].u >> e[i].w;
kruskal();
}
 #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<map>
#include<cmath>
using namespace std;
typedef pair<int, int> Pair;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int T, n, m;
const int maxn = 2e4 + ;
struct edge
{
int v, u, w;
bool operator < (const edge a)const
{
return w < a.w;
}
};
edge e[maxn];
int pa[maxn];
int Find(int x)
{
return x == pa[x] ? x : pa[x] = Find(pa[x]);//路径压缩
}
void kruskal()
{
for(int i = ; i <= n; i++)pa[i] = i;
sort(e, e + m);
ll ans = ;
for(int i = ; i < m; i++)
{
ll v = e[i].v, u = e[i].u, w = e[i].w;
ll x = Find(v), y = Find(u);
if(x != y)
{
pa[x] = y;
ans += w;
}
}
int tot = ;
for(int i = ; i <= n; i++)//判断是否联通,是否只有一个父节点是自己的点
{
if(pa[i] == i)tot++;
}
ans = 100000LL * ((ll)n - ) - ans;
if(tot > )cout<<"-1"<<endl;
else cout<<ans<<endl;
}
int main()
{
cin >> n >> m;
for(int i = ; i < m; i++)
{
cin >> e[i].v >> e[i].u >> e[i].w;
e[i].w = - e[i].w;
}
kruskal();
}

POJ-2377 Bad Cowtractors---最大生成树的更多相关文章

  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 Kru最大生成树

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

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

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

  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-Bad Cowtractors

    传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...

  9. MST:Bad Cowtractors(POJ 2377)

    坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...

  10. poj 2377 最大生成树

    #include<stdio.h> #include<stdlib.h> #define N 1100 struct node { int u,v,w; }bian[11000 ...

随机推荐

  1. asp.net mvc AjaxHelper 获取 JSON 的方法

    默认的 AjaxHelper 没有提供获取 JSON 的方法,只提供获取 html 然后更新指定元素的方法,不过,经测试发现还是有办法的,由于 AjaxOptions 对象的 OnSuccess 属性 ...

  2. IntelliJ Save Action

    https://blog.csdn.net/hustzw07/article/details/82824713

  3. javascript中对两个对象进行排序 和 java中的两个对象排序

    javascript中的对象数组排序 一 定义一个对象数组 var text = [{"name":"张","age":24},{" ...

  4. spring aop execution用法

    代码结构: 1. "execution(* com.ebc..*.*(..))" 与 "execution(*  com.ebc..*(..))" 2019-0 ...

  5. java——HashMap、Hashtable

    Map:类似Python的字典 HashMap: 不支持线程的同步,即同一时刻不能有多个线程同时写HashMap: 最多只允许一条记录的键值为null,不允许多条记录的值为null HashMap遍历 ...

  6. ubuntu ifconfig 不显示IP地址

    本文转载:https://blog.csdn.net/cmh477660693/article/details/52760236 ubuntu终端下命令ifconfig的问题解决 问题一. ifcon ...

  7. 使用codesmith无法连接mysql问题

    最近研究codesmith的用法,遇到了如题的问题,记录一下解决的方法. 1.问题描述: 在codesmith中选择MySQLSchemaProvider并连接数据库时,会报以下错误: Test fa ...

  8. java编程--02日期格式化

    第一篇,介绍日期的比较 第二篇,介绍日期的格式化 第三篇,介绍关于日期常用的计算 第四篇,比较几个常用的日期时间相关类的区别 第五篇,jdk9对日期类进行了更新,写一些i自己的学习心得. 日期的格式化 ...

  9. LeetCode 110.平衡二叉树(C++)

    给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,nu ...

  10. NASM在Ubuntu上的安装与简单使用

    一 .安装NASM 1. 下载安装文件 地址是:http://www.nasm.us/pub/nasm/releasebuilds/2.11.08/ 2.解压(具体命令要根据压缩包的类型来选用) 3. ...