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 ...
随机推荐
- 竞赛题解 - [CF 1080D]Olya and magical square
Olya and magical square - 竞赛题解 借鉴了一下神犇tly的博客QwQ(还是打一下广告) 终于弄懂了 Codeforces 传送门 『题目』(直接上翻译了) 给一个边长为 \( ...
- Ubuntu 16.04LTS 更新清华源
1 备份原来的更新源 cp /etc/apt/sources.list /etc/apt/sources.list.backup 如果提示权限不够就输入下面两行,先进入到超级用户,再备份 sudo - ...
- Hbase过滤器
Hbase过滤器简介 HBase的基本API,包括增.删.改.查等,增.删都是相对简单的操作,与传统的RDBMS相比,这里的查询操作略显苍白,只能根据特性的行键进行查询(Get)或者根据行键的范围来查 ...
- Python基本语法元素
静态语言(C/C++.Java):脚本语言(python.JavaScript.PHP) IPO(Input.Process.Output) #:python中的注释符号:''' ''':多 ...
- 《TCP/IP详解 卷1:协议》第3章 IP:网际协议
3.1 引言 IP是TCP/IP协议族中最为核心的协议.所有的TCP.UDP.ICMP及IGMP数据都以IP数据报格式传输(见图1-4).许多刚开始接触TCP/IP的人对IP提供不可靠.无连接的数据报 ...
- golang for循环里面创建协程问题的研究
原本想在一个for里面创建10个协程,这些协程顺序拿到for的递增变量,把这10个递增变量都打印出来.但事与愿违,于是做实验,查书,思考,写出以下记录. golang里,在for循环里面起协程,如下代 ...
- BAT-运行程序
@echo offrem copy C:\Users\Administrator\Desktop\0000\123.txt C:\Users\Administrator\Desktop\0000\45 ...
- java常见类
- extjs+MVC4+PetaPoco+AutoFac+AutoMapper后台管理系统(附源码)
前言 本项目使用的开发环境及技术列举如下:1.开发环境IDE:VS2010+MVC4数据库:SQLServer20082.技术前端:Extjs后端:(1).数据持久层:轻量级ORM框架PetaPoco ...
- spark中数据倾斜解决方案
数据倾斜导致的致命后果: 1 数据倾斜直接会导致一种情况:OOM. 2 运行速度慢,特别慢,非常慢,极端的慢,不可接受的慢. 搞定数据倾斜需要: 1.搞定shuffle 2.搞定业务场景 3 搞定 c ...