Description

Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie.

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

* Line 1: Two space-separated integers: N and M

* 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

* Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.

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

OUTPUT DETAILS:

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(最大生成树)的更多相关文章

  1. [POJ2377]Bad Cowtractors(最大生成树,Kruskal)

    题目链接:http://poj.org/problem?id=2377 于是就找了一道最大生成树的AC了一下,注意不连通的情况啊,WA了一次. /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏┓┃キリ ...

  2. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  3. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  4. poj图论解题报告索引

    最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...

  5. POJ - 2377 Bad Cowtractors Kru最大生成树

    Bad Cowtractors Bessie has been hired to build a cheap internet network among Farmer John's N (2 < ...

  6. BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(最大生成树)

    这很明显就是最大生成树= = CODE: #include<cstdio>#include<iostream>#include<algorithm>#include ...

  7. poj 2377 Bad Cowtractors (最大生成树prim)

    Bad Cowtractors Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  8. bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 -- 最大生成树

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MB Description     奶牛贝 ...

  9. bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复【最大生成树】

    裸的最大生成树,注意判不连通情况 #include<iostream> #include<cstdio> #include<algorithm> using nam ...

随机推荐

  1. mysql的docker化安装

    mysql版本有很多,先看下各类版本号说明: 3.X至5.1.X:这是早期MySQL的版本.常见早期的版本有:4.1.7.5.0.56等. 5.4.X到5.7.X:这是为了整合MySQL AB公司社区 ...

  2. Java : java基础(6) 反射与枚举

    类需要经过 加载, 连接, 初始化三个步骤来进行初始化. 加载是把class文件读入内存创建一个class对象, 连接分为三步,第一步是验证是否是正确的结构, 第二步是准备, 为类的静态成员分配内存, ...

  3. JZOJ 4273. 【NOIP2015模拟10.28B组】圣章-精灵使的魔法语

    4273. [NOIP2015模拟10.28B组]圣章-精灵使的魔法语 (File IO): input:elf.in output:elf.out Time Limits: 1000 ms  Mem ...

  4. 关于指针的笔记【1】【C语言程序设计-谭浩强】

    指针是什么? 一个 变量的地址称为该变量的"指针"[将地址形象化的称为“指针”].(指针是什么百度百科) 注意区分储存单元的地址和内容这两个概念的区别. 直接访问:直接按变量名进行 ...

  5. 虚拟机的三种联网模式(桥接模式、NAT 模式、仅主机模式)

    虚拟机的网络连接方式分为三种,分别是桥接模式.NAT 模式.和仅主机模式,三种连接模式存在着一定的差异,那么我们该如何选择适合自己的连接模式呢? 1.桥接模式:在此模式下,虚拟机相当于一台独立的电脑, ...

  6. Goland的常用快捷键

    文件相关快捷键: CTRL+E,打开最近浏览过的文件. CTRL+SHIFT+E,打开最近更改的文件. CTRL+N,可以快速打开struct结构体. CTRL+SHIFT+N,可以快速打开文件. 代 ...

  7. 【8086汇编-Day3】用debug做实验时的技巧与坑

    Ⅰ· 无病呻吟 学一门语言,不动手实验是学不好的,在实验中不断遇坑然后解决,才有进益.所以写一下我在第一次汇编实验中的所思所想(王爽<汇编语言>第二章章末实验). Ⅱ · 实验内容 题解思 ...

  8. 线上CPU飚高(死循环,死锁...)

    之前排除服务器内存暴增的问题,在此看到一篇类似的文章,做个类似的记录. 1.top基本使用 top 命令运行图: 第一行:基本信息 第二行:任务信息 第三行:CPU使用情况 第四行:物理内存使用情况 ...

  9. DE1-SOC工程helloworld-第一篇(未完成)

    1. 参考官方的文档,第一个问题就是电脑上需要安装ubuntu虚拟机吗? 2. 创建一个“Hello world”工程:在Linux terminal 上打印信息. 3. 说是让安装个EDS软件,先去 ...

  10. 追书神器API

    由于自己喜欢看小说,有的时候不方便手机看的时候希望在电脑上面看,但很多网站有广告啊,于是封装了套手机版的追书神器API 目前只做了搜索 详情 书评 换源 正文 调用方式: //搜索小说 var sea ...