HDU-1162-Eddy's picture
来源:互联网 发布:牛股宝 平果软件下载 编辑:程序博客网 时间:2024/05/29 08:38
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162
Eddy's picture
Problem Description
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
Input
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.
Input contains multiple test cases. Process to the end of file.
Input contains multiple test cases. Process to the end of file.
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
Sample Input
31.0 1.02.0 2.02.0 4.0
Sample Output
3.41
给出n个点的坐标,问使他们连通的最短距离。
prim算法
#include<iostream>#include<cstring>#include<algorithm>#include<cmath>using namespace std;double dis[111],e[111][111],x[111],y[111];int visit[111];const double inf=999999;int n;void init(){for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(i==j)e[i][j]=0;elsee[i][j]=inf;}}}void prim(){int i,j,k;for(i=1;i<=n;i++){dis[i]=e[1][i];}memset(visit,0,sizeof(visit));visit[1]=1;double min,sum=0.0;int count=1;while(count<n){min=inf;for(i=1;i<=n;i++){if(visit[i]==0&&dis[i]<min){min=dis[i];j=i;}}visit[j]=1;count++;sum+=dis[j];for(k=1;k<=n;k++){if(visit[k]==0&&dis[k]>e[j][k])dis[k]=e[j][k];}}printf("%.2lf\n",sum);}int main(){while(cin>>n){int i,j;if(n==0)break;for(i=1;i<=n;i++){cin>>x[i]>>y[i];}init();for(i=1;i<=n;i++){for(j=1;j<=n;j++){if(i==j)continue;double ww=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));e[i][j]=ww;}}prim();}return 0; }
kruskal算法
#include<iostream>#include<algorithm>#include<cmath>using namespace std;int pre[111];int n;struct edge{double u,v,w;}e[11111];void init(){for(int i=1;i<=n;i++)pre[i]=i;}int find(int r){if(pre[r]==r)return r;else{pre[r]=find(pre[r]);return pre[r];} } int merge(int u,int v) { int t1=find(u); int t2=find(v); if(t1!=t2) { pre[t2]=t1; return 1; } return 0; } bool cmp(edge a,edge b) { return a.w<b.w; } int main() { int i,j; double x[111],y[111]; while(cin>>n) { for(i=1;i<=n;i++) cin>>x[i]>>y[i]; init(); int m=0; for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { double ww=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); e[m].u=i; e[m].v=j; e[m].w=ww; m++; } } sort(e+1,e+m,cmp);//注意这是e+m int count=0; double sum=0.0; for(i=1;i<m;i++)//循环到m-1就好了 { if(merge(e[i].u,e[i].v)) { count++; sum+=e[i].w; } } if(count==n-1) printf("%.2lf\n",sum); } return 0; }
阅读全文
0 0
- Hdu-1162 Eddy's picture
- hdu 1162 Eddy's picture
- hdu 1162 Eddy's picture
- hdu 1162 Eddy's picture
- hdu 1162 Eddy's picture
- Hdu 1162 - Eddy's picture
- hdu 1162 Eddy's picture
- HDU 1162 Eddy's picture
- hdu 1162 Eddy's picture
- hdu 1162 Eddy's picture
- HDU 1162 Eddy's picture
- hdu-1162-Eddy's picture
- hdu 1162 Eddy's picture
- hdu 1162 Eddy's picture
- hdu 1162 Eddy's picture
- HDU 1162 Eddy's picture
- hdu 1162 Eddy's picture
- hdu 1162 Eddy's picture
- 51nod 1062 && URAL 1079 Maximum RMQ
- Visual Studio编译错误
- ( UVALive 7720)J
- $.ajax()用法——json传值
- RAM、ROM、FLASH的存储原理及区别
- HDU-1162-Eddy's picture
- Fragment getActivity()空指针?
- 应用图标在不同手机相对应不同分辨率的图标
- 策略优化算法PPO
- 网络摄像头
- cocos2dx中可以用两种方式输出日志文件
- Ubuntu常用系统安装
- idea搭建项目中出现扫描不到mapper配置文件
- Git(四)