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 ...
随机推荐
- django基础学习
{{forloop.counter}} 这是html的自增序号 GET请求可以直接从URL中获取信息,POST请求不可以,可以把信息藏到一个隐藏的input文本框中 orm 的概念就是对象关系映射 ...
- 缓冲区 粘包 029 send 和sendall 的区别 find 和 findall 的区别
一.tcp : 属于长连接 与客户端连接了之后 其他客户端需要等待 要连接另外一个 必须优雅的断开前面这个客户的连接. 二.缓冲区 :为了避免网络传输信号不通畅而是程序一直停留在消息发送状态而不向下进 ...
- Linux中的netstat命令详解
功能说明 netstat是基于Netstat这个命令行工具的指令,它可以用来查询系统上的网络套接字连接情况,包括tcp,udp以及Unix套接字:另外它还能列出路由表,接口状态和多播成员等信息. 主要 ...
- 1.Exadata技术演进
V1-v2 和 HP Exadata 2-2 和 SUN 2011 3-2 4-2 5-2 2014底 2-2 混合运算 2-8 是大数据运算 问题1. 随着系统规模增加,传统数据库架 ...
- Nuxt 2.3.X 配置babel
1. 在package.json中修改运行脚本 添加--exec babel-node 添加之后的效果为:(修改了8/10行) { "name": "nuxt-learn ...
- junit使用中的一些问题
之前开发过程中的测试,不是使用main方法,就是启动项目调用地址,尤其是后者,测试起来非常不方便,今天配置了下junit,中间遇到些问题,记录如下. 首先下载spring-test.jar包和juni ...
- JVM调优总结-Xmx -Xms -Xmn -Xss
堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64为操作 ...
- LoadScene场景异步加载
LoadScene场景异步加载 using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; usin ...
- 当前activity透明度的获取与修改
WindowManager.LayoutParams lp = getWindow().getAttributes();//layoutparams是静态类不能通过new来完成. lp.alpha = ...
- Java执行Shell脚本
Linux 系统下采用 Java 执行 Shell 脚本,直接上代码: package com.smbea.demo; import java.io.BufferedReader; import ja ...