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 0000000000000000000000000000000000000000..00e2d0a1ada8105cbd68c7efafd8edfdb0b303bc --- /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