zoukankan      html  css  js  c++  java
  • ACM HDU 1162 Eddy's picture (简单最小生成树,开始练习使用模板了)

    Eddy's picture

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2454    Accepted Submission(s): 1184


    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
     

    Author
    eddy
     

    Recommend
    JGShining
     
    /*
    HDU1162 Eddy's picture
    题意:给定画上n个点,求最短的线段把所有点连起来,简单的最小生成树
    */
    #include
    <stdio.h>
    #include
    <math.h>
    #include
    <string.h>
    #define MAXN 110
    struct Node//定义点
    {
    double x,y;
    }node[MAXN];
    double cost[MAXN][MAXN];//耗费矩阵
    double distance(Node a,Node b)//两点的距离
    {
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    //***********************************************************************
    //套用prim模板
    //***********************************************************************
    #define typec double
    const typec INF=0x3f3f3f3f;
    int vis[MAXN];
    typec lowc[MAXN];
    typec prim(typec cost[][MAXN],
    int n)//点从0~n-1
    {
    int i,j,p;
    typec minc,res
    =0;
    memset(vis,
    0,sizeof(vis));
    vis[
    0]=1;
    for(i=1;i<n;i++) lowc[i]=cost[0][i];
    for(i=1;i<n;i++)
    {
    minc
    =INF;
    p
    =-1;
    for(j=0;j<n;j++)
    if(vis[j]==0&&minc>lowc[j])
    {minc
    =lowc[j];p=j;}
    if(minc==INF)return -1;//原图不连通
    res+=minc;vis[p]=1;
    for(j=0;j<n;j++)
    if(vis[j]==0&&lowc[j]>cost[p][j])
    lowc[j]
    =cost[p][j];
    }
    return res;
    }
    //********************************************************************
    int main()
    {
    int i,j,n;
    while(scanf("%d",&n)!=EOF)
    {
    for(i=0;i<n;i++)
    scanf(
    "%lf%lf",&node[i].x,&node[i].y);
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    {
    if(i==j)cost[i][j]=0;
    else
    cost[i][j]
    =distance(node[i],node[j]);
    }
    printf(
    "%.2lf\n",prim(cost,n));
    }
    return 0;
    }

  • 相关阅读:
    day08
    day07
    day06
    day06
    day05
    第三次作业
    第三次作业
    第二次作业
    java 数字和日期处理
    jsp文件导包
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2129047.html
Copyright © 2011-2022 走看看