hdoj (1162) 最小生成树

Problem B

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

用最短路径做的(错误)代码:

#include <stdio.h>
#include <math.h>
#include <string.h>

const int maxnum = 105;
const int maxint = 999999;
double dist[maxnum];
double prev[maxnum];
double c[maxnum][maxnum];

double ju(double x1,double x2,double y1,double y2)
{
    return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}

void dijkstra(int n,int v)
{
    bool s[maxnum];
    for(int i=1;i<=n;i++)
    {
        s[i] = 0;
        dist[i] = c[v][i];
    }
    s[v] = 1;
    for(int i = 2; i<=n; i++)
    {
        int u=v;
        double temp = maxint;
        for(int j = 1; j<=n; j++)
        {
            if(!s[j]&&dist[j]<temp)
            {
                temp = dist[j];
                u = j;
            }
        }
        s[u] = 1;
        for(int j = 1; j<=n; j++)
        {
            if(!s[j])
            {
                double newdist = dist[j] + c[u][j];
                if(newdist<c[u][j])
                {
                    dist[j] = newdist;
                }
            }
        }
    }
}

int main()
{
    int n;
    double x[105],y[105];
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf",&x[i],&y[i]);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                c[i][j] = ju(x[i],x[j],y[i],y[j]);
                c[j][i] = c[i][j];
            }
            c[i][i] = 0;
        }
        dijkstra(n,1);
        printf("%.2lf\n",dist[n]);
    }
}

为什么结果不正确?

hdoj (1162) 最小生成树

时间: 2024-08-12 05:37:44

hdoj (1162) 最小生成树的相关文章

hdoj 1162 Eddy&#39;s picture(最小生成树)

Eddy's picture http://acm.hdu.edu.cn/showproblem.php?pid=1162 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7521    Accepted Submission(s): 3821 Problem Description Eddy begins to like painti

hdoj 1162 Eddy&#39;s picture【最小生成树】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8117    Accepted Submission(s): 4114 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

hdoj 1162 Eddy&#39;s picture

并查集+最小生成树 Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7669    Accepted Submission(s): 3882 Problem Description Eddy begins to like painting pictures recently ,he is sure of hi

hdu 1162(最小生成树)

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8797    Accepted Submission(s): 4476 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to b

[hdoj]1301最小生成树prim

1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int arr[28][28]; 5 int judge[28]; 6 7 int main() 8 { 9 10 int n; 11 while(cin >> n&&n) 12 { 13 int i,j,pri,count; 14 char C; 15 memset(arr,0,sizeof(arr)); 16 memset

HDOJ 1301最小生成树的Kruskal算法

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 将结点的字符信息处理成点信息即可,代码如下: 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned int ui; 4 typedef long long ll; 5 typedef unsigned long long ull; 6 #define pf printf 7 #define mem(a,b) m

hdu1162Eddy&#39;s picture

http://acm.hdu.edu.cn/showproblem.php?pid=1162 最小生成树 1 #include<iostream> 2 #include<stdio.h> 3 #include<math.h> 4 #include<string.h> 5 #include<stdlib.h> 6 using namespace std; 7 const int N=5005; 8 struct stu{ 9 int u; 10 i

hdoj 1863 畅通工程 【最小生成树】+【kruskal】

题意:... 难点:如何判断是不是信息不全:在输入的时候建立并查集,之后判断有几个节点就可以了,剩下的就是kruskal算法. 代码: #include<stdio.h> #include<string.h> #include<algorithm> #define MAXN 105 #define INF 0x3f3f3f3f using std::sort; struct node{ int from; int to; int w; }edges[MAXN*MAXN]

hdu 1162 Eddy&#39;s picture 最小生成树入门题 Prim+Kruskal两种算法AC

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7428    Accepted Submission(s): 3770 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to