Building a Space Station POJ - 2031
Posted ssummerzzz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Building a Space Station POJ - 2031相关的知识,希望对你有一定的参考价值。
题目链接:https://vjudge.net/problem/POJ-2031
思路:最小生成树板子题
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 #include <stack> 7 #include <string> 8 #include <map> 9 #include <cmath> 10 using namespace std; 11 12 typedef long long LL; 13 #define inf 1e11 14 #define rep(i,j,k) for(int i = (j); i <= (k); i++) 15 #define rep__(i,j,k) for(int i = (j); i < (k); i++) 16 #define per(i,j,k) for(int i = (j); i >= (k); i--) 17 #define per__(i,j,k) for(int i = (j); i > (k); i--) 18 19 const int N = 110; 20 int head[N]; 21 int cnt; 22 double dis[N]; 23 bool vis[N]; 24 int n; 25 26 struct Point{ 27 double x,y,z,r; 28 }p[N]; 29 30 struct Edge{ 31 int to; 32 double w; 33 int nxt; 34 }e[N*N]; 35 36 struct node{ 37 int u; 38 double w; 39 friend bool operator<(const node& a,const node& b){ 40 return a.w > b.w; 41 } 42 }; 43 44 priority_queue<node > que; 45 46 void add(int u,int v,double w){ 47 e[cnt].to = v; 48 e[cnt].w = w; 49 e[cnt].nxt = head[u]; 50 head[u] = cnt++; 51 } 52 53 inline double get_dis(Point& a,Point& b){ 54 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z))-a.r-b.r; 55 } 56 57 double prime(){ 58 while(!que.empty()) que.pop(); 59 60 rep(i,1,n){ 61 vis[i] = false; 62 dis[i] = inf; 63 } 64 dis[1] = 0; 65 que.push(node{1,dis[1]}); 66 67 int u,v; 68 double w; 69 while(!que.empty()){ 70 u = que.top().u; 71 que.pop(); 72 if(vis[u]) continue; 73 vis[u] = true; 74 75 for(int o = head[u]; ~o; o = e[o].nxt){ 76 v = e[o].to; 77 w = e[o].w; 78 79 if(!vis[v] && dis[v] > w){ 80 dis[v] = w; 81 que.push(node{v,dis[v]}); 82 } 83 } 84 } 85 86 double ans = 0; 87 rep(i,1,n) ans += dis[i]; 88 return ans; 89 } 90 91 int main(){ 92 93 double tmp; 94 while(~scanf("%d",&n)){ 95 if(n == 0) break; 96 rep(i,1,n) head[i] = -1; 97 cnt = 0; 98 99 rep(i,1,n) scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z,&p[i].r); 100 // rep(i,1,n) printf("%.3f %.3f %.3f %.3f ",p[i].x,p[i].y,p[i].z,p[i].r); 101 102 rep(i,1,n) rep(j,i+1,n){ 103 tmp = get_dis(p[i],p[j]); 104 add(i,j,tmp > 0 ? tmp : 0); 105 add(j,i,tmp > 0 ? tmp : 0); 106 } 107 108 printf("%.3f ",prime()); 109 } 110 111 getchar(); getchar(); 112 return 0; 113 }
以上是关于Building a Space Station POJ - 2031的主要内容,如果未能解决你的问题,请参考以下文章
POJ - 2031C - Building a Space Station最小生成树
POJ 2031 Building a Space Station
Building a Space Station POJ 2031 最小生成树 prim