码迷,mamicode.com
首页 > 其他好文 > 详细

UVa10034/POJ2560_Freckles(最小生成树)(小白书图论专题)

时间:2014-08-19 11:02:54      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   使用   os   io   

解题报告

题意:

把所有点连起来,求使用的墨水最少。

思路:

裸最小生成树。

#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.

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


UVa10034/POJ2560_Freckles(最小生成树)(小白书图论专题),布布扣,bubuko.com

UVa10034/POJ2560_Freckles(最小生成树)(小白书图论专题)

标签:des   style   blog   http   color   使用   os   io   

原文地址:http://blog.csdn.net/juncoder/article/details/38677427

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!