Bad Cowtractors(最大生成树)
Description
Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
Input
* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
Output
Sample Input
5 8
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5 17
Sample Output
42
Hint
The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,sum;
struct node
{
int start;///起点
int end;///终点
int power;///权值
} edge[20050];
int pre[20050];
int cmp(node a,node b)
{
return a.power<b.power;///按权值降序排列
// return a.power<b.power
}
int find(int x)///并查集找祖先
{
int a;///循环法
a=x;
while(pre[a]!=a)
{
a=pre[a];
}
return a;
}
void merge(int x,int y,int n)
{
int fx =find(x);
int fy =find(y);
if(fx!=fy)
{
pre[fx]=fy;
sum+=edge[n].power;
}
}
int main()
{
int i,x,count;
while(scanf("%d%d",&n,&m)!=EOF)
{
sum=0;
count=0;
for(i=1; i<=m; i++)
{
scanf("%d%d%d",&edge[i].start,&edge[i].end,&x);
edge[i].power=x;
//edge[i].power=-x;
}
for(i=1; i<=m; i++) ///并查集的初始化
{
pre[i]=i;
}
sort(edge+1,edge+m+1,cmp);
for(i=1; i<=m; i++)
{
merge(edge[i].start,edge[i].end,i);
}
for(i=1; i<=n; i++)
{
if(pre[i]==i)
{
count++;
}
}
if(count==1)
{
printf("%d\n",sum);
// printf("%d\n",-sum);
}
else
{
printf("-1\n");
}
}
return 0;
}
///普里姆算法
#include<stdio.h>
#include<string.h>
#define MAX 0x3f3f3f3f
using namespace std;
int logo[1010];///用0和1来表示是否被选择过
int map1[1010][1010];
int dis[1010];///记录任意一点到这一点的最近的距离
int n,m;
int prim()
{
int i,j,now;
int sum=0;
for(i=1; i<=n; i++) ///初始化
{
dis[i]=MAX;
logo[i]=0;
}
for(i=1; i<=n; i++)
{
dis[i]=map1[1][i];
}
dis[1]=0;
logo[1]=1;
for(i=1; i<n; i++) ///循环查找
{
now=-MAX;
int max1=-MAX;
for(j=1; j<=n; j++)
{
if(logo[j]==0&&dis[j]>max1)
{
now=j;
max1=dis[j];
}
}
if(now==-MAX)///防止不成图
{
break;
}
logo[now]=1;
sum=sum+max1;
for(j=1; j<=n; j++) ///填入新点后更新最小距离,到顶点集的距离
{
if(logo[j]==0&&dis[j]<map1[now][j])
{
dis[j]=map1[now][j];
}
}
}
if(i<n)
{
printf("-1\n");
}
else
{
printf("%d\n",sum);
}
}
int main()
{
int i,j;
int a,b,c;
while(scanf("%d%d",&n,&m)!=EOF)///n是点数
{
for(i=1; i<=n; i++)
{
for(j=i; j<=n; j++)
{
if(i==j)
{
map1[i][j]=map1[j][i]=0;
}
else
{
map1[i][j]=map1[j][i]=-MAX;
}
}
}
for(i=0; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
if(map1[a][b]<c)///防止出现重边
{
map1[a][b]=map1[b][a]=c;
}
}
prim();
}
return 0;
}
Bad Cowtractors(最大生成树)的更多相关文章
- [POJ2377]Bad Cowtractors(最大生成树,Kruskal)
		题目链接:http://poj.org/problem?id=2377 于是就找了一道最大生成树的AC了一下,注意不连通的情况啊,WA了一次. /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏┓┃キリ ... 
- 杭电ACM分类
		杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ... 
- 转载:hdu 题目分类 (侵删)
		转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ... 
- poj图论解题报告索引
		最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ... 
- POJ - 2377 Bad Cowtractors Kru最大生成树
		Bad Cowtractors Bessie has been hired to build a cheap internet network among Farmer John's N (2 < ... 
- BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(最大生成树)
		这很明显就是最大生成树= = CODE: #include<cstdio>#include<iostream>#include<algorithm>#include ... 
- poj 2377 Bad Cowtractors (最大生成树prim)
		Bad Cowtractors Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ... 
- bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复  -- 最大生成树
		3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MB Description 奶牛贝 ... 
- bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复【最大生成树】
		裸的最大生成树,注意判不连通情况 #include<iostream> #include<cstdio> #include<algorithm> using nam ... 
随机推荐
- Resharp常用设置收集整理
			F12跳转的问题: 
- Dubbo 安装监控中心
			一.Dubbo 安装服务管理控制台 1.在官方Github下载Dubbo OPS 2.下载incubator-dubbo-ops源码后,解压修改配置文件Zookeeper注册中心地址 3.maven打 ... 
- Quote Helper
			using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk.Que ... 
- LinkedList的源码分析(基于jdk1.8)
			1.初始化 public LinkedList() { } 并未开辟任何类似于数组一样的存储空间,那么链表是如何存储元素的呢? 2.Node类型 存储到链表中的元素会被封装为一个Node类型的结点.并 ... 
- openwrt procd启动流程和脚本分析
			Linux内核执行start_kernel函数时会调用kernel_init来启动init进程,流程如下图: graph LR A[start_kernel] -->B(rest_init) B ... 
- OpenWrt超时检测
			参考http://www.right.com.cn/forum/thread-261702-1-1.html vim /home/ihid/chaos_calmer/feeds/luci/module ... 
- python爬虫#数据存储#JSON/CSV/MYSQL/MongoDB/
			Json数据处理 JSON支持数据格式: 对象(字典).使用花括号. 数组(列表).使用方括号. 整形.浮点型.布尔类型还有null类型. 字符串类型(字符串必须要用双引号,不能用单引号). 多个数据 ... 
- python中的rabbitmq
			介绍 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议.MQ全称为Message Queue, 消息队列(MQ)是一种应用 ... 
- QOS-交换机拥塞管理
			QOS-交换机拥塞管理 2018年7月7日 20:29 优先级映射: 根据信任的优先级,查找映射表,标记丢弃优先级和本地优先级 如果信任端口优先级,不同产品优先级标记方式可能不同,S3610处理过程如 ... 
- java-执行流程控制语句
			就像有知觉的生物一样,程序必须在执行的过程中控制它的世界,并做出选择.java使用执行流程控制语句做出选择. 1.选择语句 if if(布尔表达式){ 业务逻辑1; }else{ 业务逻辑2: } s ... 
