Eddy's picture

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 1
Problem Description
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
 
Input
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point. Input contains multiple test cases. Process to the end of file.
 
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
 
Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
 
Sample Output
3.41
 类似与畅通工程里面的百岛湖那个题,刚开始没理解题意,写错了,最后又wa两遍,point数组没开大;
克鲁斯卡尔代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int map[];
struct Point{
int start,end;
double len;
}point[];
int cmp(Point a,Point b){
return a.len<b.len;
}
struct dot{
double x,y;
}dot[];
int find(int x){
int r=x;
while(r!=map[r])r=map[r];
int i=x,j;
while(i!=r)j=map[i],map[i]=r,i=j;
return r;
}
double distant(double x1,double y1,double x2,double y2){
double x=x2-x1,y=y2-y1,L;
L=sqrt(x*x+y*y);
return L;
}
int main(){
int n,c;
double distance;
while(~scanf("%d",&n)){
// memset(map,0,sizeof(map));
// memset(point,0,sizeof(point));
for(int i=;i<=n;++i)map[i]=i;
for(int i=;i<=n;++i)scanf("%lf%lf",&dot[i].x,&dot[i].y);c=;
for(int i=;i<=n;++i){
for(int j=;j<i;++j){point[c].start=j;point[c].end=i;
point[c].len=distant(dot[j].x,dot[j].y,dot[i].x,dot[i].y);
c++;
}
}
sort(point,point+c,cmp);distance=;
for(int i=;i<c;++i){
int f1,f2;
f1=find(point[i].start);f2=find(point[i].end);
if(f1!=f2)map[f1]=f2,distance+=point[i].len;
}
printf("%.2lf\n",distance);
}
return ;
}

prime:

 #include<stdio.h>
#include<string.h>
#include<math.h>
const int INF=0x3f3f3f3f;
const int MAXN=;
int n;
double answer;
double map[MAXN][MAXN],low[MAXN];
int vis[MAXN];
double gd(double ax,double ay,double bx,double by){
double x=bx-ax,y=by-ay;
double s=x*x+y*y;
return sqrt(s);
}
void prime(){
int k;
double temp;
memset(vis,,sizeof(vis));
vis[]=;
for(int i=;i<n;i++)low[i]=map[][i];
for(int i=;i<n;i++){
temp=INF;
for(int j=;j<n;j++)
if(!vis[j]&&temp>low[j])
temp=low[k=j];//k=写错地方了,又错了半天
if(temp==INF){
printf("%.2lf\n",answer);
break;
}
answer+=temp;
vis[k]=;
for(int j=;j<n;j++)
if(!vis[j]&&low[j]>map[k][j])
low[j]=map[k][j];
}
}
int main(){
double dx[MAXN],dy[MAXN];
while(~scanf("%d",&n)){
answer=;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
map[i][j]=INF;
for(int i=;i<n;i++)
scanf("%lf%lf",&dx[i],&dy[i]);
for(int i=;i<n;i++)
for(int j=i+;j<n;j++){
double dis=gd(dx[i],dy[i],dx[j],dy[j]);
//printf("%lf\n",dis);
if(dis<map[i][j])
map[i][j]=map[j][i]=dis;
}
prime();
}
return ;
}

Eddy's picture(prime+克鲁斯卡尔)的更多相关文章

  1. hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)

    还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. prim算法,克鲁斯卡尔算法---最小生成树

    最小生成树的一个作用,就是求最小花费.要在n个城市之间铺设光缆,主要目标是要使这 n 个城市的任意两个之间都可以通信,但铺设光缆的费用很高,且各个城市之间铺设光缆的费用不同,因此另一个目标是要使铺设光 ...

  3. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  4. HDUOJ-----(1162)Eddy's picture(最小生成树)

    Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. 贪心算法(Greedy Algorithm)之最小生成树 克鲁斯卡尔算法(Kruskal&#39;s algorithm)

    克鲁斯卡尔算法(Kruskal's algorithm)是两个经典的最小生成树算法的较为简单理解的一个.这里面充分体现了贪心算法的精髓.大致的流程能够用一个图来表示.这里的图的选择借用了Wikiped ...

  6. hdoj 1162 Eddy's picture

    并查集+最小生成树 Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  7. HDU 1162 Eddy's picture

    坐标之间的距离的方法,prim算法模板. Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32 ...

  8. 【类克鲁斯卡尔做法+枚举最小边】【HDU1598】【find the most comfortable road】

    题意:  给你一个图,有边权,K个询问:u到v 的路径中   边权最大值-边权最小值的最小值是多少 http://acm.hdu.edu.cn/showproblem.php?pid=1598 题解( ...

  9. 克鲁斯卡尔(Kruskal)算法

    # include <stdio.h> # define MAX_VERTEXES //最大顶点数 # define MAXEDGE //边集数组最大值 # define INFINITY ...

随机推荐

  1. fragment的入门DEMO

    效果图: 代码如下: 1.main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  2. 动态载入TreeView时让TreeView节点前显示加号

    解释下标题,我这里通过webservice获取数据并动态载入TreeView节点.那么某个节点展开前它是没有子节点的.那么它就不显示加号.这样会让用户误以为此节点不能展开.我是这样做的,每次创建节点a ...

  3. 解决getJdbcTemplate往oracle数据库中插入数据返回主键出错问题

    我们使用Spring中的JdbcDaoSupport往Mysql中插入数据并返回主键代码,我们使用的mysql数据库,主键在数据库中设置为自增长:该类继承自JdbcDaoSupport,所以能直接使用 ...

  4. 解决数据库Operation not allowed when innodb_forced_recovery > 0

    解决数据库Operation not allowed when innodb_forced_recovery > 0 请修改my.cnf innodb_force_recovery = 1 修改 ...

  5. AE分级渲染

    分级渲染classbreakrenderer位于carto类库中,进行分级渲染时,首先要将相应图层按照某一Field分级.可使用esrisystem类库中的iclassifyGEN类的classify ...

  6. HTML 表格入门

    每个表格都是由 table 标签开始. 每个表格行由 tr 标签开始. 每个表格数据由 td 标签开始. 这样是一行三列: <table border="1"> < ...

  7. TreeView中右击直接获取节点的方法

    在TreeView中无法直接右击得到一个节点,因为当你选中其中一个右击时(不能是第一个)他会默认跳到第一个. 有时我们要想直接右击得到选中的节点,又时我们又想选中直接右击跳出一个快捷菜单怎么办了! 在 ...

  8. UML学习-状态图

    1.状态图概述 状态图(Statechart Diagram)主要用于描述一个对象在其生存期间的动态行为,表现为一个对象所经历的状态序列,引起状态转移的事件(Event),以及因状态转移而伴随的动作( ...

  9. Linux 与 unix shell编程指南——学习笔记

    第一章    文件安全与权限 文件访问方式:读,写,执行.     针对用户:文件属主,同组用户,其它用户.     文件权限位最前面的字符代表文件类型,常用的如         d 目录:l 符号链 ...

  10. jBPM4.4与SSH2整合

    整合jBPM的目的就是能够通过注入的方式得到ProcessEngine实例,因为ProcessEngine是jbpm 的核心. 整合步骤: 1.新建web程,搭建好SSH2环境 2.导入jbpm相关的 ...