给出N个点的坐标 边的权值为两点间的距离 删掉其中某点 求最小生成树的权值和 要求这权值最小

因为最多50个点 所以具体是删哪个点 用枚举
假如有4个点 就要求4次最小生成树 分别是2 3 4 | 1 3 4 | 1 2 4 | 1 2 3 这些点的

Sample Input
2
5
0 0
1 0
18 0
0 1
1 1
3
0 0
1 0
0 1

Sample Output
3.00
1.00

prim

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; const int INF=0x3f3f3f3f;
const int MAXN=;
bool vis[MAXN];
double lowc[MAXN];
double cost[MAXN][MAXN] ; struct poin
{
int x ;
int y ;
}p[] , q[]; double Prim(int n)
{
int i , j ;
for (i = ; i < n ; i++)
for (j = ; j < n ; j++)
cost[i][j] = INF ;
for (i = ; i < n ; i++)
for (j = i+ ; j < n ; j++)
{
double t = sqrt((double)(q[i].x - q[j].x) * (q[i].x - q[j].x) + (q[i].y - q[j].y) * (q[i].y - q[j].y)) ;
cost[i][j] = t ;
cost[j][i] = t ;
}
double ans=;
memset(vis,false,sizeof(vis));
vis[]=true;
for(int i=;i<n;i++)lowc[i]=cost[][i];
for(int i=;i<n;i++)
{
double minc=INF;
int p=-;
for(int j=;j<n;j++)
if(!vis[j]&&minc>lowc[j])
{
minc=lowc[j];
p=j;
}
if(minc==INF)return -;//原图不连通
ans+=minc;
vis[p]=true;
for(int j=;j<n;j++)
if(!vis[j]&&lowc[j]>cost[p][j])
lowc[j]=cost[p][j];
}
return ans;
} int main()
{ // freopen("in.txt","r",stdin) ;
int T ;
scanf("%d" , &T) ;
while(T--)
{
int n ;
scanf("%d" , &n) ;
int i , j ;
double MIN = INF ;
for (i = ; i < n ; i++)
scanf("%d %d" , &p[i].x , &p[i].y) ; for (i = ; i < n ; i++)
{
int k = ;
for (j = ; j < n ; j++)
{
if (i == j)
continue ;
q[k].x = p[j].x ;
q[k].y = p[j].y ;
k++ ;
}
double ans = Prim(n-) ;
if (ans < MIN)
MIN = ans ;
}
printf("%.2lf\n" , MIN) ; }
return ;
}

最新文章

  1. Wo的书单
  2. 回归 从注释开始 appledoc
  3. Linux入侵检测工具 - RKHunter
  4. 设计模式之Builder模式
  5. unity项目实现“再按一次退出程序”提示功能
  6. textarea中限制输入字符长度
  7. 【剑指Offer学习】【面试题14 :调整数组顺序使奇数位于偶数前面】
  8. 反调试技术(Delphi版)
  9. IMSDroid遇到注册问题(蘼1S 计3等一下 Android4.4)
  10. %type的用法
  11. 深入分析Java单例模式的各种方案
  12. MySQL &#183; 引擎特性 &#183; InnoDB 事务系统
  13. [译]《Sphinx权威指南》 - Sphinx入门
  14. 高级shell 脚本
  15. 本地项目提交到github和提交更新(转)
  16. Confluence 6 管理协同编辑
  17. 使用python做简单的接口性能测试
  18. 解决在IE浏览器中JQuery.resize()执行多次的方法(转)
  19. Android屏幕适配工具
  20. 4-2 R语言函数 apply

热门文章

  1. Spark记录-SparkSql官方文档中文翻译(部分转载)
  2. Centos6.8-hadoop-2.7.2 64 bit源码编译(伪分布-5大守护进程在本机上)
  3. 源码分析之CountDownLatch
  4. 常用的Date对象和Math对象方法
  5. urllib和urllib2之间的区别
  6. nxlog 日志采集
  7. node.js 找不到 xxx 模块解决办法
  8. Java EE之Hibernate异常总结【2】Field &#39;id&#39; doesn&#39;t have a default value
  9. HDU2444 The Accomodation of Students【匈牙利算法】
  10. 三维dp