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. DreamWeaver文件保存时,提示"发生共享违例"问题的解决方法

    在学习牛腩老师的JS视频中,视频中的例子要求实现一个是23个3相乘的结果,在用Dreamweaver制作时,, <script language="javascript" t ...

  2. 动态修改UINavigationBar的背景色

    这是我们最终想要得到的效果: 思路 在UISrollView的delegate方法 1  - (void)scrollViewDidScroll:(UIScrollView *)scrollView ...

  3. 改动mac环境变量,并配置gradle

    由于项目中要用到gradle命令,可是没有配置环境变量.这里记录一下解决过程. 过程例如以下: 1. 启动终端Terminal 2. 进入当前用户的home文件夹 输入cd ~ 3. 创建.bash_ ...

  4. RMAN-使用catalog恢复目录进行备份与恢复

    RMAN ArchitectureThe RMAN architecture, shown in Figure 7-3, includes a target database, repository, ...

  5. 学习AJAX(二)

  6. JS软键盘代码

    页面代码如下: <HTML> <HEAD> <TITLE>一个不错的js软键盘代码</TITLE> <meta http-equiv=" ...

  7. 关于android的SQLiteDatabase和Cursor的一些疑问

    android数据库操作的基础有三个类:SQLiteOpenHelper,SQLiteDatabase和Cursor.其中,SQLiteOpenHelper会建立一个数据库连接,它虽然可以调用多次ge ...

  8. Josn转DataTable(转)

    使用UI框架开发的时候就常常用到DataTable转Json的情况,但是最近完成一个微信公众号开发的项目,需要把微信接口传过来的json值作为转为DataTable后绑定到服务器控件上. 在网上找了很 ...

  9. (转) c++ 迭代器

    原地址:http://www.cnblogs.com/marchtea/archive/2012/02/27/2370068.html 前言: 以下的内容为我阅读c++沉思录18,19,20章的笔记以 ...

  10. codeforces 342C Cupboard and Balloons(公式题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud C. Cupboard and Balloons A girl named Xen ...