您的位置:首页 > 其它

poj_2560 Freckles

2012-07-27 15:53 323 查看
Freckles

Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 5248
Accepted: 2720
题目:http://poj.org/problem?id=2560
Description
In an episode of the Dick VanDyke show, little Richie connects the freckles on his Dad's back to form apicture of the Liberty Bell. Alas, one of the freckles turns out to be a scar,so his Ripley's engagement falls through.

Consider Dick's back to be a plane with freckles at various (x,y) locations.Your job is to tell Richie how to connect the dots so as to minimize the amountof ink used. Richie connects the dots by drawing straight lines between pairs,possibly lifting the pen
between lines. When Richie is done there must be asequence of connected lines from any freckle to any other freckle.
Input
The first line contains 0 <n <= 100, the number of freckles on Dick's back. For each freckle, a linefollows; each following line contains two real numbers indicating the (x,y)coordinates of the freckle.
Output
Your program prints a singlereal number to two decimal places: the minimum total length of ink lines thatcan connect all the freckles.
Sample Input
3
1.0 1.0
2.0 2.0
2.0 4.0
Sample Output
3.41
Source
Waterloolocal 2000.09.23
题意:
输入n个点的坐标位置,求走完全部点的最小距离。
解题思路:
这是一个求图的最小生成树的问题,权值就是两个点之间的距离,用prin()算法来求解——先以一个节点(1)作为最小生成树的初始节点,然后在一次选择与这点相连权值最小的边放在集合中,这样不断的向集合中增加节点,直到每个节点添加完成
代码:

#include <iostream>

#include<cstdio>

#include<cstring>

#include<cmath>

#define MAX 103

#define VALUE 0xffffff

using namespace std;

struct coor

{

float x;

float y;

};

coor cor[MAX];

float g[MAX][MAX];

bool used[MAX];

float d[MAX];

int ts;



float distinct(coor x1,coor x2)

{

return sqrt((x2.x-x1.x)*(x2.x-x1.x)+(x2.y-x1.y)*(x2.y-x1.y));

}



float prim()

{

int i;

for(i=0;i<=ts;i++)

{

used[i]=false;

d[i]=VALUE;

}

d[1]=0;

float res=0;

while(true)

{

int t=-1;

for(i=1;i<=ts;i++)

{

if(!used[i] && (t==-1 || d[i]<d[t]))

t=i;

}

if(t==-1)

break;

used[t]=true;

res+=d[t];

for(i=0;i<=ts;i++)

{

if(d[i]>g[t][i])

d[i]=g[t][i];

}

}

return res;

}





int main()

{



scanf("%d",&ts);

//建图

memset(g,0,sizeof(g));

for(int i=1;i<=ts;i++)

{

scanf("%f%f",&cor[i].x,&cor[i].y);

for(int j=1;j<i;j++)

{

g[i][j]=distinct(cor[i],cor[j]);

g[j][i]=g[i][j];

}



}

printf("%.2f\n",prim());

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: