Fork me on GitHub

HDU ACM 1162 Eddy's picture(MST)

Eddy's picture

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


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
 
复制代码
#include<stdio.h>
#include<string.h>
#include<math.h>

double path[102][102];
int flag[102];
double closedge[102];
double max;
double cnt;

typedef struct{
    double x, y;
}input;

input temp[102];

double calculate(double x1, double y1, double x2, double y2)
{// 两点之间的距离 
    
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

double CreatMST(int n)
{
    int i, j, x;
    double k;
    flag[0] = 1;
    for(i=1; i<n; ++i)
    closedge[i] = path[0][i];
    for(i=1; i<n; ++i)
    {
        k = max, x = 1;
        for(j=1; j<n; ++j)
        if(!flag[j] && closedge[j] < k)
            x = j, k = closedge[j];
        flag[x] = 1;
        cnt += k;
        for(j=1; j<n; ++j)
        if(!flag[j] && closedge[j] > path[x][j])
        closedge[j] = path[x][j];
    }
    return cnt;
}

int main()
{
    int i, j, k, t, x, y, n, m;
    while(scanf("%d", &n) != EOF)
    {
        max = 0;
        cnt = 0;
        memset(temp, 0, sizeof(input)*102);
        memset(flag, 0, sizeof(flag));
        memset(closedge, 0, sizeof(closedge));
        memset(path, 0, sizeof(double)*102*102);
        for(i=0; i<n; ++i)
        scanf("%lf%lf", &temp[i].x, &temp[i].y);
        
        // 计算N*(N+1)条路径的权重 
        for(i=0; i<n; ++i)
        for(j=0; j<n; ++j)
        {
            path[i][j] = calculate(temp[i].x, temp[i].y, temp[j].x, temp[j].y);
            if(max < path[i][j]) max = path[i][j];
        }
        printf("%.2lf\n", CreatMST(n));
    }
    return 0;
}
复制代码

解题报告:1y

最小生成树不会叫你就写一个函数就行了。

posted @   Gifur  阅读(397)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
TOP
点击右上角即可分享
微信分享提示