首页 > 代码库 > BZOJ2429: [HAOI2006]聪明的猴子

BZOJ2429: [HAOI2006]聪明的猴子

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2429

题解:能否跨过只看最长边,那我们求一棵最小生成树,只要判断最长边和最远跳跃距离的远近就行了。

但是。。。我怎么觉得是次长边呢?我可以故意不走这条边,绕一圈T_T

代码:

技术分享
  1 #include<cstdio>  2   3 #include<cstdlib>  4   5 #include<cmath>  6   7 #include<cstring>  8   9 #include<algorithm> 10  11 #include<iostream> 12  13 #include<vector> 14  15 #include<map> 16  17 #include<set> 18  19 #include<queue> 20  21 #include<string> 22  23 #define inf 1000000000 24  25 #define maxn 1000+5 26  27 #define maxm 200000+5 28  29 #define eps 1e-10 30  31 #define ll long long 32  33 #define pa pair<int,int> 34  35 #define for0(i,n) for(int i=0;i<=(n);i++) 36  37 #define for1(i,n) for(int i=1;i<=(n);i++) 38  39 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 40  41 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 42  43 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 44  45 #define for5(n,m) for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) 46  47 #define mod 1000000007 48 #define sqr(x) (x)*(x) 49  50 using namespace std; 51  52 inline int read() 53  54 { 55  56     int x=0,f=1;char ch=getchar(); 57  58     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();} 59  60     while(ch>=0&&ch<=9){x=10*x+ch-0;ch=getchar();} 61  62     return x*f; 63  64 } 65 int n,m; 66 double mx,mxx,b[maxn],d[maxn]; 67 bool v[maxn]; 68 struct rec{int x,y;}a[maxn]; 69 priority_queue<pa,vector<pa>,greater<pa> >q; 70 inline double dist(int x,int y){return sqrt(sqr(a[x].x-a[y].x)+sqr(a[x].y-a[y].y));} 71 inline void prim() 72 { 73     for1(i,n)d[i]=inf; 74     d[1]=0; 75     q.push(pa(0,1)); 76     while(!q.empty()) 77     { 78         int x=q.top().second;q.pop(); 79         if(v[x])continue;v[x]=1; 80         if(d[x]>mx)mxx=mx,mx=d[x]; 81         else if(d[x]>mxx)mxx=d[x]; 82         for1(i,n)if(!v[i]&&dist(i,x)<d[i]) 83         { 84            d[i]=dist(i,x); 85            q.push(pa(d[i],i)); 86         } 87     } 88 } 89  90 int main() 91  92 { 93  94     freopen("input.txt","r",stdin); 95  96     freopen("output.txt","w",stdout); 97  98     m=read(); 99     for1(i,m)b[i]=read();100     n=read();101     for1(i,n)a[i].x=read(),a[i].y=read();102     prim();103     int ans=0;104     for1(i,m)if(b[i]>=mx)ans++;105     cout<<ans<<endl;106 107     return 0;108 109 }  
View Code

 

BZOJ2429: [HAOI2006]聪明的猴子