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. python threading基础学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' """ python是支持多线程的,并 ...

  2. CSS彻底研究(2)

    Github pages 博文 一 . CSS盒模型 1.盒子的结构 margin-border-padding结构 + 内容content 组成盒模型 注意 width,height 取的是cont ...

  3. SQLServer 跨服务器查询的两个办法

    网上搜了跨服务器查询的办法,大概就是Linked Server(预存连接方式并保证连接能力)和OpenDataSource(写在语句中,可移植性强).根据使用函数的不同,性能差别显而易见...虽然很简 ...

  4. css解决文字垂直居中

    参考链接: http://www.cnblogs.com/lufy/archive/2012/09/12/2681972.html   http://zhidao.baidu.com/question ...

  5. 在SQL SERVER中批量替换字符串

    update [表名] set [字段名]=replace([字段名],'被替换原内容','替换后内容')

  6. Glide的加载图片的帮助类,用来把图片圆角或者改成圆形图片

    Glide虽然非常好用但是没找到把图片圆角的方法,所以百度了一个非常不错的加载类自己实现圆角图 感谢原文章作者:http://blog.csdn.net/weidongjian/article/det ...

  7. IIS Hang Troubleshoot

    Your website maybe stop working and response very lowly. How to find out the reason? Below are the g ...

  8. latex 模版

    texlive2012 通过 \documentclass[a4paper]{article} %\documentclass[twocolumn]{article}   %\usepackage{g ...

  9. 【转】cocos2d-x windows开发环境配置

    声明:本教程在参考了以下博文,并经过自己的摸索后实际操作得出,本教程系本人原创,由于升级后的cocos2d-x有了一些变化,目前的博文还没有关于Cocos2d-x 2.2.1最新版搭建Android交 ...

  10. mac 更改word的默认显示比例为125

    1.打开或新建一个word文档 2.按 fn + option + F11 键,会弹出一个[项目]窗口,选中Normal, 双击[模块], 修改为125 Sub AutoOpen() ActiveWi ...