《算法笔记》10.5小节——图算法专题->最小生成树 问题 B: Freckles
题目描述
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.
输入
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.
输出
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.
样例输入
3
2723.62 7940.81
8242.67 11395.00
4935.54 6761.32
9
10519.52 11593.66
12102.35 2453.15
7235.61 10010.83
211.13 4283.23
5135.06 1287.85
2337.48 2075.61
6279.72 13928.13
65.79 1677.86
5324.26 125.56
0
样例输出
8199.56
32713.73
题目大意:给出 n 个点的坐标 (x,y),问将这 n 个点组成的图连通起来的最小代价是多少。保留 2 位小数输出。
分析:本题实际上是一个 n 个点,n*(n-1) 条边的图,给出的是点的坐标,点之间的距离是隐含的欧几里得距离。转化为这样求最小生成树即可。
#include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#define INF 0x3fffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;typedef struct node
{double x,y;
}node;double graph[110][110];double prim(int n)
{int s=0;double ans=0.0;double d[n+5];for(int i=0;i<n;++i)d[i]=INF;d[s]=0;bool vis[n+5]={0};for(int times=0;times<n;++times){int u=-1;double mini=INF;for(int i=0;i<n;++i){if(!vis[i]&&d[i]<mini)u=i,mini=d[i];}if(u==-1)return -1;vis[u]=1,ans+=mini;for(int i=0;i<n;++i){if(!vis[i]&&graph[u][i]!=INF&&d[i]>graph[u][i])d[i]=graph[u][i];}}return ans;
}int main(void)
{#ifdef testfreopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);clock_t start=clock();#endif //testint n;while(scanf("%d",&n),n){for(int i=0;i<n;++i)for(int j=0;j<n;++j)graph[i][j]=INF;vector<node>freckles[n+5];for(int i=0;i<n;++i){double a,b;scanf("%lf%lf",&a,&b);node temp;temp.x=a,temp.y=b;freckles[i].push_back(temp);if(i){for(int j=0;j<i;++j){graph[i][j]=graph[j][i]=sqrt((freckles[i][0].x-freckles[j][0].x)*(freckles[i][0].x-freckles[j][0].x)+(freckles[i][0].y-freckles[j][0].y)*(freckles[i][0].y-freckles[j][0].y));}}}double ans=prim(n);printf("%.2f\n",ans);}#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位#endif //testreturn 0;
}