首页 > 代码库 > UVa10034/POJ2560_Freckles(最小生成树)(小白书图论专题)
UVa10034/POJ2560_Freckles(最小生成树)(小白书图论专题)
解题报告
题意:
把所有点连起来,求使用的墨水最少。
思路:
裸最小生成树。
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0x3f3f3f3f using namespace std; struct N { double x,y; } node[110]; int vis[110],n; double mmap[110][110],dis[110]; double disc(N a,N b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } void prim() { int u; double minn,ans=0; for(int i=0; i<n; i++) { dis[i]=mmap[0][i]; vis[i]=0; } dis[0]=0; vis[0]=1; for(int i=0; i<n-1; i++) { minn=(double)inf; for(int j=0; j<n; j++) { if(!vis[j]&&dis[j]<minn) { minn=dis[j]; u=j; } } vis[u]=1; ans+=minn; for(int j=0; j<n; j++) { if(!vis[j]&&mmap[u][j]<dis[j]) { dis[j]=mmap[u][j]; } } } printf("%.2lf\n",ans); } int main() { int t,i,j; // scanf("%d",&t); // while(t--) { scanf("%d",&n); 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++) { mmap[i][j]=mmap[j][i]=disc(node[i],node[j]); } } prim(); // if(t)printf("\n"); // } return 0; }
Freckles
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7004 | Accepted: 3448 |
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 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 amount of 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 a sequence of connected lines from any freckle to any other freckle.
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 amount of 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 a sequence 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 line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.
Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
Sample Output
3.41
Source
Waterloo local 2000.09.23
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。