poj 2560 Freckles
2017-10-15 10:26
274 查看
Freckles
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8749 Accepted: 4177
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.
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
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8749 Accepted: 4177
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.
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
#include <stdio.h> #include <algorithm> #include <math.h> #include <string.h> #define maxn 1100 using namespace std; double INF = 999999999; double edge[maxn][maxn]; double lowcost[maxn]; bool vis[maxn]; struct node { double x,y; }a[maxn]; int n; double prim(int num) { vis[num] = 1; double sum = 0; for(int i = 1; i <= n; i++) { lowcost[i] = edge[i][num]; } for(int i = 1; i < n; i++) { double minn = INF; int temp; for(int j = 1; j <= n; j++) { if(vis[j] == 0 && lowcost[j] < minn) { minn = lowcost[j]; temp = j; } } vis[temp] = 1; sum += minn; for(int j = 1; j <= n; j++) { if(vis[j] == 0 && edge[j][temp] < lowcost[j]) { lowcost[j] = edge[j][temp]; } } } return sum; } int main() { while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; i++) { scanf("%lf%lf",&a[i].x,&a[i].y); } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { edge[i][j] = sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y)); } } double ans = prim(1); printf("%.2f\n",ans); } }
相关文章推荐
- POJ 2560Freckles(并查集)
- poj 2560 Freckles
- POJ 2560 Freckles
- POJ 2560 Freckles
- POJ2560_Freckles_最小生成树
- Poj 2560 Freckles
- poj_2560 Freckles
- poj 2560_Freckles_并查集
- POJ2560 Freckles(prim最小生成树)
- poj 2560 Freckles(并查集)
- POJ 2560 Freckles(最小生成树-Kruskal)
- poj 2560 Freckles
- POJ-2560 Freckles
- POJ 2560 Freckles
- poj 2560 Freckles
- poj 2560 Freckles 雀斑
- POJ 2560 Freckles
- poj 2560 Freckles
- POJ-2560-Freckles 解题报告
- poj_2560 Freckles