题目链接:

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 AD 域验证

    1.获取环境变量 string strAuthUser = request.ServerVariables["AUTH_USER"] 以上这行代码是用来获取当前用户的登录名 2.I ...

  2. Angular JS ng-model对<select>标签无效的情况

    使用场景一: <select ng-if="item.award_type==1" id="award{{$index+1}}" name="X ...

  3. nodejs的异步非阻塞IO

    简单表述一下:发启向系统IO操作请求,系统使用线程池IO操作,执行完放到事件队列里,node主线程轮询事件队列,读取结果与调用回调.所以说node并非真的单线程,还是使用了线程池的多线程. 上个图看看 ...

  4. ubuntu下搭建android开发环境核心篇安装AndroidStudio、sdk、jdk

    本文系转载http://blog.csdn.net/lsyz0021/article/details/52215996 一.安装前的准备 1.1.如果你还没有安装ubuntu 14.04 LTS系统, ...

  5. pat06-图6. 公路村村通(30)

    06-图6. 公路村村通(30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的 ...

  6. Session [php]

    1.启动会话 session_start() 通过session_register()函数创建会话 session_register()函数用来为会话登录一个变量来隐含地启动会话,但要求php.ini ...

  7. jQuery Validate验证框架详解(转)

    jQuery校验官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一.导入js库 <script type=& ...

  8. 弹性布局 Flexible Box

    页面中任何一个元素都可以指定为 弹性布局(Flex) 属性:display 取值: 1.flex     将块级元素变为弹性布局容器 2.inline-flex   将行内元素变为弹性布局容器 兼容性 ...

  9. 【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect

    Redirection示例涉及到三个Acitivity: RedirectEnter, RedirectMain,RedirectGetter. 示例的主Activity为 RedirectEnter ...

  10. 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01

    本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...