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. 持续集成(CI – Continuous Integration)

    持续集成(CI – Continuous Integration) 在传统的软件开发中,整合过程通常在每个人完成工作之后.在项目结束阶段进行.整合过程通常需要数周乃至数月的时间,可能会非常痛苦.持续集 ...

  2. 用JQ实现的一个简单轮播

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>lb ...

  3. 邮箱,ip,叠词的正则处理方式

    package com.regexTest; import java.util.TreeSet; public class RegexTest { public static void main(St ...

  4. vi模式下的编辑、删除、保存和退出

    vi + 文件名:进入 vi 模式 编辑模式:shift+: 退出编辑模式:Esc 退出编辑模式后可进行光标的上下左右移动(偶尔会出现ABCD,还不知道怎么解决,目前只能出来一个删除一个) 光标处:按 ...

  5. python教程(一)·python环境搭建

    python的环境搭建总的来说分为两大步:下载.安装(废话@_@).在这里以windows为例(Linux通常内置了python,就算没有内置,相信Linux用户也非常清楚软件的安装方法) 第一步-下 ...

  6. Python 爬虫 (四)

    requests: 练手 雪qiu网 import requests import json import re import pymysql url = 'https://xueqiu.com/v4 ...

  7. linux下使用汇编语言编写hello world!程序

    最近公司需要完成安全方面的测试,随之带来需要更深入地学习攻击方法和漏洞分析的技术,总感觉有点像黑客:),不过不能只知道一些安全测试工具的方法和工具的使用,更需要基础功夫,首先从大学学过的汇编语言(呵呵 ...

  8. 天津Uber优步司机奖励政策(12月14日到12月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 【机器学习笔记】EM算法及其应用

    极大似然估计 考虑一个高斯分布\(p(\mathbf{x}\mid{\theta})\),其中\(\theta=(\mu,\Sigma)\).样本集\(X=\{x_1,...,x_N\}\)中每个样本 ...

  10. sql中的几种连接类型

    一.连接类型简介 在sql中单表查询的几率相对来说比较少,随着数据库的日益复杂,多表关联的情况越来越多,在多表关联的情况下存在多种关联的类型, 1.自关联(join或inner join) 2.左外关 ...