日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(kruskal算法复习+模板)Eddy's picture

發布時間:2025/3/12 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (kruskal算法复习+模板)Eddy's picture 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目:

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?
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.
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
3
1.0 1.0
2.0 2.0
2.0 4.0
Sample Output
3.41

分析與解答

題意:就是給你幾個點的坐標,讓你求一下每個點都經過的最短距離。我們知道點坐標,就可以把兩點之間的距離求出來,此時我們可以看作給了兩點和一個邊,求最短路,這就轉化成連通工程那一道題。我寫這個突然發現我把那個kruskal算法給忘了
先上一個kruskal模板

int pre[505]; struct data {int x,y;double len; } a[25005]; void init() {for(int i=0; i<505; i++)pre[i]=i; } int Find(int x) {if(pre[x]!=x)return pre[x]=Find(pre[x]);return x; } void Merge(int x,int y) {int X=Find(x);int Y=Find(y);if(X!=Y)pre[X]=Y; } int cmp(data a,data b) {return a.len<b.len; } //main里面把數據先輸入到a里面,輸入完之后, //如果u,v不在一個連通分量里,就把uv連通并且最短路加上他們間的距離 //注意初始化init();sort(a,a+cnt,cmp);double res=0;for(int i=0;i<cnt;i++){int X=Find(a[i].x);int Y=Find(a[i].y);if(X!=Y){pre[X]=Y;res+=a[i].len;}}

代碼參考:https://blog.csdn.net/qq_30591245/article/details/77094961

#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int pre[505]; struct data {int x,y;double len; } a[25005]; struct Point {double x,y; }b[25005]; void init() {for(int i=0; i<505; i++)pre[i]=i; } int Find(int x) {if(pre[x]!=x)return pre[x]=Find(pre[x]);return x; } //void Merge(int x,int y) //{ // int X=Find(x); // int Y=Find(y); // if(X!=Y) // pre[X]=Y; //} int cmp(data a,data b) {return a.len<b.len; } double Distance(Point a,Point b) {return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int main() {int n;while(~scanf("%d",&n)){init();for(int i=1;i<=n;i++){scanf("%lf%lf",&b[i].x,&b[i].y);}int cnt=0;for(int i=1;i<n;i++){for(int j=i+1;j<=n;j++){a[cnt].x=i;a[cnt].y=j;a[cnt].len=Distance(b[i],b[j]);//printf("%f\n",a[cnt].len);++cnt;}}//printf("%d\n",cnt);sort(a,a+cnt,cmp);double res=0;for(int i=0;i<cnt;i++){int X=Find(a[i].x);int Y=Find(a[i].y);if(X!=Y){pre[X]=Y;// printf("%f\n",a[i].len);res+=a[i].len;}}printf("%.2f\n",res);} }

總結

以上是生活随笔為你收集整理的(kruskal算法复习+模板)Eddy's picture的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。