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 ...
随机推荐
- 转:30分钟学会如何使用Shiro
引自:http://www.cnblogs.com/learnhow/p/5694876.html 本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinniansh ...
- phpstudy lamp
phpStudy for Linux (lnmp+lamp一键安装包 现在不考虑安装这个 (完整版:http://lamp.phpstudy.net/phpstudy-all.bin) 安装: wg ...
- 用Python生成词云
词云以词语为基本单元,根据词语在文本中出现的频率设计不同大小的形状以形成视觉上的不同效果,从而使读者只要“一瞥“即可领略文本的主旨.以下是一个词云的简单示例: import jieba from wo ...
- java并发(1)
hashmap效率高单线程不安全,hashTable效率低但线程安全 因为hashTable使用synchronized来保证线程安全,所以效率十分低,比如线程1使用put插入数据时,线程2既不能使用 ...
- 北京Uber优步司机奖励政策(12月28日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 成都Uber优步司机奖励政策(1月25日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- orm4sqlite
//-------------------------------------------------------------------------- // // Copyright (c) BUS ...
- #define NULL ((void *)0)引起的风波
1. 看下宏定义的结构体 typedef struct { ]; //CMEI/IMEI ]; //server ]; //CMEI/IMEI } Options; 2. 定义的NULL #defin ...
- CC3200底板测试-烧写CC3200-LAUNCHXL
1. 拿到板子,先研究一下几个跳线帽的作用.我在底板上测到VCC_DCDC_3V3和VCC_BRD之间应该有一个跳线帽的,但是在原理上找不到. 2. LED灯的用途,测试的时候,发现这个灯有时候亮,有 ...
- docker社区的geodata/gdal镜像dockerfile分析
对应从事遥感与地理信息的同仁来说,gdal应该是所有工具中使用频度最高的库了,那么在docker中使用gdal时,面临的第一步就是构建gdal基础镜像,社区中引用最多的就是geodata提供的gdal ...