zoj 1203 Swordfish

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1203

解题思路:最小生成树(kruskal 算法 + 优先队列 + 并查集)

  1 /**************************************************************************
  2 user_id: SCNU20102200088
  3 problem_id: zoj 1203
  4 problem_name: Swordfish
  5 **************************************************************************/
  6 
  7 #include <algorithm>
  8 #include <iostream>
  9 #include <iterator>
 10 #include <iomanip>
 11 #include <sstream>
 12 #include <fstream>
 13 #include <cstring>
 14 #include <cstdlib>
 15 #include <climits>
 16 #include <bitset>
 17 #include <string>
 18 #include <vector>
 19 #include <cstdio>
 20 #include <cctype>
 21 #include <ctime>
 22 #include <cmath>
 23 #include <queue>
 24 #include <stack>
 25 #include <list>
 26 #include <set>
 27 #include <map>
 28 using namespace std;
 29 
 30 //线段树
 31 #define lson l,m,rt<<1
 32 #define rson m+1,r,rt<<1|1
 33 
 34 //手工扩展栈
 35 #pragma comment(linker,"/STACK:102400000,102400000")
 36 
 37 const double EPS=1e-9;
 38 const double PI=acos(-1.0);
 39 const double E=2.7182818284590452353602874713526;  //自然对数底数
 40 const double R=0.5772156649015328606065120900824;  //欧拉常数:(1+1/2+...+1/n)-ln(n)
 41 
 42 const int x4[]={-1,0,1,0};
 43 const int y4[]={0,1,0,-1};
 44 const int x8[]={-1,-1,0,1,1,1,0,-1};
 45 const int y8[]={0,1,1,1,0,-1,-1,-1};
 46 
 47 typedef long long LL;
 48 
 49 typedef int T;
 50 T max(T a,T b){ return a>b? a:b; }
 51 T min(T a,T b){ return a<b? a:b; }
 52 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
 53 T lcm(T a,T b){ return a/gcd(a,b)*b; }
 54 
 55 ///////////////////////////////////////////////////////////////////////////
 56 //Add Code:
 57 int n,s[105];
 58 
 59 struct Node{
 60     int u,v;
 61     double w;
 62     bool operator <(const Node &a) const{
 63         return w>a.w;
 64     }
 65 }node;
 66 
 67 priority_queue<Node> pq;
 68 
 69 double dist(double x1,double y1,double x2,double y2){
 70     return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
 71 }
 72 
 73 void Union(int x,int y){
 74     s[y]=x;
 75 }
 76 
 77 int Find(int x){
 78     if(s[x]<0) return x;
 79     return s[x]=Find(s[x]);
 80 }
 81 
 82 double Kruskal(){
 83     int num=0;
 84     double ret=0;
 85     memset(s,-1,sizeof(s));
 86     while(!pq.empty() && num<n-1){
 87         node=pq.top();
 88         pq.pop();
 89         if(Find(node.u)!=Find(node.v)){
 90             Union(Find(node.u),Find(node.v));
 91             ret+=node.w;
 92             num++;
 93         }
 94     }
 95     while(!pq.empty()) pq.pop();
 96     return ret;
 97 }
 98 ///////////////////////////////////////////////////////////////////////////
 99 
100 int main(){
101     std::ios::sync_with_stdio(false);
102     //freopen("in.txt","r",stdin);
103     //freopen("out.txt","w",stdout);
104     ///////////////////////////////////////////////////////////////////////
105     //Add Code:
106     int Case=1,i,j;
107     double x[105],y[105];
108     while(scanf("%d",&n)!=EOF){
109         if(n==0) break;
110         for(i=1;i<=n;i++) scanf("%lf%lf",&x[i],&y[i]);
111         for(i=1;i<n;i++){
112             for(j=i+1;j<=n;j++){
113                 node.u=i;
114                 node.v=j;
115                 node.w=dist(x[i],y[i],x[j],y[j]);
116                 pq.push(node);
117             }
118         }
119         double ans=Kruskal();
120         if(Case>1) printf("
");
121         printf("Case #%d:
",Case++);
122         printf("The minimal distance is: %.2lf
",ans);
123     }
124     ///////////////////////////////////////////////////////////////////////
125     return 0;
126 }
127 
128 /**************************************************************************
129 Testcase:
130 Input:
131 5
132 0 0
133 0 1
134 1 1
135 1 0
136 0.5 0.5
137 0
138 Output:
139 Case #1:
140 The minimal distance is: 2.83
141 **************************************************************************/
原文地址:https://www.cnblogs.com/linqiuwei/p/3358491.html