From bf3fb900434479f3db6892baefdd282869ba84cf Mon Sep 17 00:00:00 2001 From: Powfu <1875065753@qq.com> Date: Tue, 26 Dec 2023 13:36:54 +0000 Subject: [PATCH] =?UTF-8?q?add=202101040022/chapter=5F8/=E6=B1=82=E8=A7=A3?= =?UTF-8?q?=E5=BB=BA=E5=B7=A5=E8=B7=AF=E9=97=AE=E9=A2=98.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Powfu <1875065753@qq.com> --- ...5\350\267\257\351\227\256\351\242\230.cpp" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "2101040022/chapter_8/\346\261\202\350\247\243\345\273\272\345\267\245\350\267\257\351\227\256\351\242\230.cpp" diff --git "a/2101040022/chapter_8/\346\261\202\350\247\243\345\273\272\345\267\245\350\267\257\351\227\256\351\242\230.cpp" "b/2101040022/chapter_8/\346\261\202\350\247\243\345\273\272\345\267\245\350\267\257\351\227\256\351\242\230.cpp" new file mode 100644 index 00000000..00e2d0a1 --- /dev/null +++ "b/2101040022/chapter_8/\346\261\202\350\247\243\345\273\272\345\267\245\350\267\257\351\227\256\351\242\230.cpp" @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#define INF 0x3f3f3f3f +using namespace std; +struct position{ + int x,y; +}room[5001]; +bool vis[5001]; +double low[5001]; +int n; +double len(int i,int j){ + return sqrt((double)(room[i].x-room[j].x)*(room[i].x-room[j].x)+(double)(room[i].y-room[j].y)*(room[i].y-room[j].y)); +} + +void prim(){ + for(int i=1;i<=n;i++){ + low[i]=len(1,i); + } + low[1]=0;vis[1]=true; + for(int i=1;i<=n;i++){ + int minn=INF; + int temp; + for(int j=1;j<=n;j++){ + if(!vis[j]&&minn>low[j]){ + minn=low[j]; + temp=j; + } + } + vis[temp]=true; + for(int j=1;j<=n;j++){ + if(!vis[j]&&low[j]>len(temp,j)){ + low[j]=len(temp,j); + } + } + } +} +int main(){ + scanf("%d",&n); + for(int i=1;i<=n;i++){ + scanf("%d%d",&room[i].x,&room[i].y); + } + prim(); + double ans=0; + for(int i=1;i<=n;i++){ + ans+=low[i]; + } + printf("%.2lf\n",ans); + +} \ No newline at end of file -- Gitee