POJ-2377 Bad Cowtractors---最大生成树
题目链接:
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---最大生成树的更多相关文章
- poj 2377 Bad Cowtractors
题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...
- poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)
http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...
- POJ - 2377 Bad Cowtractors Kru最大生成树
Bad Cowtractors Bessie has been hired to build a cheap internet network among Farmer John's N (2 < ...
- poj 2377 Bad Cowtractors (最大生成树prim)
Bad Cowtractors Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- poj 2377 Bad Cowtractors(最大生成树!)
Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...
- POJ 2377 Bad Cowtractors (Kruskal)
题意:给出一个图,求出其中的最大生成树= =如果无法产生树,输出-1. 思路:将边权降序再Kruskal,再检查一下是否只有一棵树即可,即根节点只有一个 #include <cstdio> ...
- POJ 2377 Bad Cowtractors( 最小生成树转化 )
链接:传送门 题意:给 n 个点 , m 个关系,求这些关系的最大生成树,如果无法形成树,则输出 -1 思路:输入时将边权转化为负值就可以将此问题转化为最小生成树的问题了 /************* ...
- POJ:2377-Bad Cowtractors
传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- MST:Bad Cowtractors(POJ 2377)
坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...
- poj 2377 最大生成树
#include<stdio.h> #include<stdlib.h> #define N 1100 struct node { int u,v,w; }bian[11000 ...
随机推荐
- Zero Sum Subarray
Given an integer array, find a subarray where the sum of numbers is zero. Your code should return th ...
- Troubleshooting ORA-201 and ORA-202 Error
---- 3. When lowering the value of COMPATIBLE: You cannot start the database with lower compatibilit ...
- python3与python2的区别 记录一波
1.性能 Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可 以取得很好的优化结果. Py3.1性能比Py2 ...
- 数据结构---Java---HashSet
1.概述 1.1 HashSet不是线程安全的: 1.2 当向HashSet存入元素时,调用该对象的hashCode()值,根据hashCode()值来决定元素的存储位置: 如果hashCode()值 ...
- .NET接收邮件下载邮件附件——openpop.net
使用OpenPop.Net接收邮件很方便,下面是接收下载邮件附件的代码 OpenPop.Net下载地址 https://sourceforge.net/projects/hpop/ public cl ...
- Transform控制的物体移动
Transform控制的物体移动:public class TKMove : MonoBehaviour { public float HSpeed; public float VSpeed; pri ...
- (转)DNS解析过程详解
DNS解析过程详解 原文:http://blog.csdn.net/crazw/article/details/8986504 先说一下DNS的几个基本概念: 一. 根域 就是所谓的“.”,其实我们的 ...
- 学习JVM-GC原理
1. 前言 Java和C++之间显著的一个区别就是对内存的管理.和C++把内存管理的权利赋予给开发人员的方式不同,Java拥有一套自动的内存回收系统(Garbage Collection,GC)简称G ...
- vue学习笔记 vue安装
一.安装步骤:(用cmd命令用管理身份安装比较顺利) 1.安装node,安装后可以输入npm -v 查看版本,升级npm可用 cnpm install npm -g 2.安装vue 输入cnpm in ...
- maven课程 项目管理利器-maven 4-1 使用maven创建web项目 5星
本节主要讲了使用maven创建web项目 主要分这三大类: 1 新建maven web项目 2 后续处理普通java项目转web项目需要关注的点 3 maven特色转web需要关注的点 1 新建ma ...