POJ 2560

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<iomanip>
 5 #define MAXN 105
 6 #define inf 1000000000
 7 using namespace std;
 8
 9 typedef double elem_t;
10
11 double point[MAXN][2];
12 double _m[MAXN][MAXN];
13 int pre[MAXN];
14 elem_t prim(int n,elem_t mat[][MAXN],int* pre);
15 int main()
16 {
17     //freopen("acm.acm","r",stdin);
18     int num;
19     int i;
20     int j;
21     double x;
22     double y;
23     cin>>num;
24     for(i = 0; i < num; ++ i)
25     {
26         cin>>point[i][0]>>point[i][1];
27     }
28     for(i = 0; i < num; ++ i)
29     {
30         for(j = i+1; j < num; ++ j)
31         {
32             _m[i][j] = sqrt((point[i][0] - point[j][0])*(point[i][0] - point[j][0]) + (point[i][1] - point[j][1])*(point[i][1] - point[j][1]));
33             _m[j][i] = _m[i][j];
34         }
35     }
36     cout<<setiosflags(ios::fixed)<<setprecision(2)<<prim(num,_m,pre)<<endl;
37 }
38
39
40 elem_t prim(int n,elem_t mat[][MAXN],int* pre){
41     elem_t min[MAXN],ret=0;
42     int v[MAXN],i,j,k;
43     for (i=0;i<n;i++)
44         min[i]=inf,v[i]=0,pre[i]=-1;
45     for (min[j=0]=0;j<n;j++){
46         for (k=-1,i=0;i<n;i++)
47             if (!v[i]&&(k==-1||min[i]<min[k]))
48                 k=i;
49         for (v[k]=1,ret+=min[k],i=0;i<n;i++)
50             if (!v[i]&&mat[k][i]<min[i])
51                 min[i]=mat[pre[i]=k][i];
52     }
53     return ret;
54 }
时间: 2024-10-13 20:15:06

POJ 2560的相关文章

POJ 2560 Freckles Prime算法题解

本题是求最小生成树. 给出的是坐标节点,然后需要根据这些坐标计算出各个点之间的距离. 除此就是标准的Prime算法了,能使用Prime的基本上都可以使用Kruskal. 这些经典的算法一定要多写,熟练掌握,否则很难灵活运用的. 而且经典的算法之所以为经典,原因之一是没那么容易自己凭空想象出来的,所以要熟练. #include <stdio.h> #include <string.h> #include <queue> #include <float.h> #

poj 2560 Freckles

题目连接 http://poj.org/problem?id=2560 Freckles Description In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ri

Poj(2560),最小生成树,Prim

题目链接:http://poj.org/problem?id=2560 只想说“全都是套路”,关键建图. #include <stdio.h> #include <string.h> #include <math.h> #define INF 0x3f3f3f3f int n; double maps[300][300]; bool vis[300]; double dis[300]; struct Point { double x; double y; }points

POJ 2560 Freckles(最小生成树)

原题地址:http://poj.org/problem?id=2560 Freckles Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7863   Accepted: 3776 Description In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a pictu

POJ 2560 Freckles Prime问题解决算法

这个问题正在寻求最小生成树. 给定节点的坐标,那么我们需要根据各个点之间的这些坐标来计算距离. 除了这是标准的Prime算法的,能源利用Prime基本上,你可以使用Kruskal. 经典的算法必须填写,熟练度.否则它是非常困难的利用. 并且经典的算法之所以为经典.原因之中的一个是没那么easy自己凭空想象出来的,所以要熟练. #include <stdio.h> #include <string.h> #include <queue> #include <floa

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

POJ 1135.Domino Effect

Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10325   Accepted: 2560 Description Did you know that you can use domino bones for other things besides playing Dominoes? Take a number of dominoes and build a row by standing

POJ 2355 Find a multiple(组合数学-抽屉原理)

Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5881   Accepted: 2560   Special Judge Description The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&