HDU 2682 Tree(Kruskal算法求解MST)
题目:
Now we want to connecte all the cities together,and make the cost minimal.
InputThe first will contain a integer t,followed by t cases.
Each case begin with a integer N,then N integer Vi(0<=Vi<=1000000).OutputIf the all cities can be connected together,output the minimal cost,otherwise output "-1";Sample Input
2
5
1
2
3
4
5 4
4
4
4
4
Sample Output
4
-1
题意描述:
题目还是有点意思的,但是还是改变不了使水题的本质。
输入结点的个数以及每个结点的权值
计算并输出有特殊要求的最小生成树
解题思路:
属于最小生成树问题,按照数据的格式,使用Kruskal算法。
代码实现:
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct edge
{
int u,v,w;
};
struct edge e[*];//大数组申请全局变量
int f[];
bool cmp(struct edge x,struct edge y)
{
return x.w<y.w;
}
int getf( int v);
int merge(int c,int u);
int isp(int n); int main()
{
int t,i,n,c[],j,k,count,sum;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&c[i]);
k=;
for(i=;i<=n;i++)
{
for(j=i+;j<=n;j++)
{
if(isp(c[i]) || isp(c[j]) || isp(c[i]+c[j]))
{
e[k].u=i;
e[k].v=j;
e[k++].w = min(min(c[i],c[j]),abs(c[i]-c[j]));
}
}
}
sort(e+,e+k,cmp);
for(i=;i<=n;i++)
f[i]=i;
count = ;
sum=;
for(i=;i<k;i++)
{
if( merge(e[i].u,e[i].v) )
{
count++;
sum += e[i].w;
}
if(count == n-)
break;
} if(count == n-)
printf("%d\n",sum);
else
printf("-1\n");
}
return ;
} int getf( int v)
{
if(f[v]==v)
return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}
int merge(int v,int u)
{
int t1,t2;
t1=getf(v);
t2=getf(u);
if(t1 != t2)
{
f[t2]=t1;
return ;
}
return ;
}
int isp(int n)
{
if(n==)//1不是素数
return ;
int k,i;
k=(int)sqrt(n);
for(i=;i<=k;i++)
{
if(n % i==)
return ;
}
return ;
}
易错分析:
1、程序直接强制结束,可能是数组开的太大,放在全局变量的位置就可以了。
2、判断素数的过程中,1不是素数(代码功力)
HDU 2682 Tree(Kruskal算法求解MST)的更多相关文章
- HDU 5253 连接的管道(Kruskal算法求解MST)
题目: 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老天格外的不开眼,大旱.所以老 Jack 决定用管道将他的所有相邻的农田全部都串联起来,这样他就可以从远处引水过来进行灌溉了.当老 J ...
- ZOJ 1586 QS Network(Kruskal算法求解MST)
题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...
- POJ 1251 Jungle Roads(Kruskal算法求解MST)
题目: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money w ...
- HDU 1863 畅通工程(Prim算法求解MST)
题目: 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本.现 ...
- HDU 2682 Tree
题目: There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and ...
- POJ 1258 Agri-Net(Prim算法求解MST)
题目链接: http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...
- ZOJ 1203 Swordfish(Prim算法求解MST)
题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...
- hdu 2988(最小生成树 kruskal算法)
Dark roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 克鲁斯卡尔(Kruskal)算法求最小生成树
/* *Kruskal算法求MST */ #include <iostream> #include <cstdio> #include <cstring> #inc ...
随机推荐
- [UWP]爱恋动漫BT开发小记
在七月和某个人相识,在七月又和这个人重回陌路,在这个伤感的七月,让我来水一篇博客. 已经很久没有写博客了,最近现在来写一篇,总结一下这个七月. 今年的暑假特别的长,大概六月中旬就考完试了,而开学一直要 ...
- day 64 Django 第五天 多表对多表的对应关系ORM
一.查 设置 Author表 在 views文件中 # 作者表 class Author(models.Model): id =models.AutoField(primary_key=True) ...
- 廖雪峰Python学习笔记——使用元类
元类(MetaClasses) 元类提供了一个改变Python类行为的有效方式. 元类的定义是“一个类的类”.任何实例是它自己的类都是元类. class demo(object): pass obj ...
- 日常一些出现bug的问题
1.Fatal signal 4 (SIGILL), code 1, fault addr 0xca31569e in tid 8033 (r.myapplication) fault addr : ...
- Python3.5 学习十二 数据库介绍
MYSQL介绍: 主流三种数据库:Oracle.Mysql.Sqlserver Mysql安装和启动: windows 1安装 2启动服务 3进入bin目录,打开命令行 4 mysqladmin -u ...
- series dataframe 的 idxmax()
返回最大值的索引
- 简述项目中优化sql语句执行效率的方法,从哪些方面,sql语句性能如何分析?
(1)尽量选择较小的列: (2)将where中用的比较频繁的字段建立索引: (3)select中避免使用*: (4)避免在索引列上使用计算.not in和<>等操作: (5)当只需要一行数 ...
- mybatis中单个参数的引用
单个参数时在test条件中不能用参数名来引用,可以使用_parameter
- python打造渗透工具集
python是门简单易学的语言,强大的第三方库让我们在编程中事半功倍,今天我们就来谈谈python在渗透测试中的应用,让我们自己动手打造自己的渗透工具集. 难易程度:★★★阅读点:python;web ...
- Java 日期加减
import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date; public class Test ...