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. qt model/view 架构基础介绍之QTableWidget

    # -*- coding: utf-8 -*- # python:2.x #说明:QTreeWidget用于展示树型结构,也就是层次结构同前面说的 QListWidget 类似,这个类需要同另外一个辅 ...

  2. linux内核--内存管理(二)

    一.进程与内存     所有进程(执行的程序)都必须占用一定数量的内存,它或是用来存放从磁盘载入的程序代码,或是存放取自用户输入的数据等等.不过进程对这些内存的管理方式因内存用途不一而不尽相同,有些内 ...

  3. Vs2012于Linux应用程序开发(2):图案

    1.1     代码提示功能 在vs中开发中,Visual Assist是一个很优秀的插件,我们仍然能够使用它进行代码的分析,但它仅仅能支持vcxprojproject,因而我们选择对vcxproj的 ...

  4. Java中String直接赋字符串和new String的区别

    解析Java中的String对象的数据类型 1. String是一个对象.  因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ...

  5. js对象克隆, 深复制.

    亲测有效: //对象克隆 function clone(obj) { // Handle the 3 simple types, and null or undefined if (null == o ...

  6. vs2013 创建网站

    从文件菜单中选择新建网站,版本选择4,如果选择更高级的版本在发布的时候老是会报错,暂时找不到解决的方法,所以就选择4这个版本了.选择asp的空网站,在下面选择文件系统. 项目建好后如下,然后添加一个w ...

  7. struts2面试题

    由于找了很久的工作都没有找的,只能四处收集那个面试题的.和看面试题的 还有那个记忆力也不是很好了的,而那些公司面试的时候总会有一个面试题的!   在这里分享给大家(那个本来是想上传文件的,但是找不到的 ...

  8. ResourceBundle读取中文properties文件问题

    昨天遇到一个问题,用ResourceBundle读取中文字符串资源文件时,死活读不出来. 一开始以为是文件路径不对,后来发现如果默认properties文件时英文就没问题.我的项目代码是在src目录下 ...

  9. 如何 查看 WebLogic Server的版本号[转]

    如何 查看 WebLogic Server的版本号[转] WebLogic Server 10gR3为例: 1.查看$BEA_HOME/registry.xml         => <c ...

  10. zoj3231 Apple Transportation(最大流)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Apple Transportation Time Limit: 1 Second ...