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. cookielib模块基础学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import cookielib #主要用于处理http客户端的co ...

  2. sql执行顺序整理

    sql的执行顺序,是优化sql语句执行效率必须要掌握的.各个数据库可能有细小的差别,但大体顺序是相同的,这里只做大致说明. 一.总体执行顺序 在sql语句执行之前,还有SQL语句准备执行阶段,这里不做 ...

  3. Velocity知识点总结

    Velocity知识点总结 1. 变量 (1)变量的定义: #set($name = "hello") 说明:velocity中变量是弱类型的. 当使用#set 指令时,括在双引號 ...

  4. QtXlsxWriter

    Code Issues26 Pull requests2   Pulse Graphs HTTPS clone URL You can clone with HTTPS orSubversion. C ...

  5. iOS方法类:CGAffineTransform的使用

    CoreGraphics框架中的CGAffineTransform类可用于设定UIView的transform属性,控制视图的缩放.旋转和平移操作: 另称放射变换矩阵,可参照线性代数的矩阵实现方式0. ...

  6. Emacs入门快捷键

    打开emacs开始一个程序最基本操作: 1.打开Emacs,执行 $ emacs 2.建立一个新的程序文件. 按C-x C-f 然后在屏幕的底部出现minibuffer,光标提示你输入文件名称, 文件 ...

  7. netCDF

    NetCDF started in 1989 most used in geoscience community array-oriented self-describing header, desc ...

  8. shell中的退出状态码

    shell中的退出状态码最大只有255,如果超过这个值,就会进行取余运算,即如果执行如下命令: exit exitCode 如果exitCode大于255,那么实际的状态码为exitCode % 25 ...

  9. git configuration

    git的配置文件由section名和变量名组成: [user] name = abc emial = example.com []里面的user就是section名,section只能由字母,数字,- ...

  10. DOM2定位与高宽类属性专题学习【DOM专题学习系列(一)】

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...