BZOJ_1615_[Usaco2008_Mar]_The Loathesome_Hay Baler_麻烦的干草打包机_(模拟+宽搜/深搜)
描述
http://www.lydsy.com/JudgeOnline/problem.php?id=1615
一个主动轮带着一些轮子转,轮子带着轮子转,轮子带着轮子转...一个非主动轮只会被一个轮子带着转.求从主动轮到某一个轮子的路上所有轮子的转速的绝对值之和.
分析
从起点开始,枚举相接触的轮子,只要不是之前路上的(带着当前轮子转的)轮子,就继续往下走.宽搜深搜都可以.
注意:
1.%.0lf是会四舍五入的!所以要强制转化成int.
宽搜:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=1050+5; 5 const double eps=1e-10; 6 struct node{ double x,y,r; }a[maxn]; 7 int n,s,t; 8 int q[maxn]; 9 bool vis[maxn]; 10 double xt,yt; 11 double s_[maxn],ans[maxn]; 12 inline bool c(node a,node b){ return fabs((sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2))-a.r-b.r))<eps; } 13 int main(){ 14 scanf("%d%lf%lf",&n,&xt,&yt); 15 for(int i=1;i<=n;i++){ 16 scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].r); 17 if(a[i].x==0.0&&a[i].y==0.0) s=i; 18 else if(a[i].x==xt&&a[i].y==yt) t=i; 19 } 20 int front=0,tail=0; 21 q[tail++]=s; vis[s]=true; s_[s]=ans[s]=10000; 22 while(front!=tail){ 23 int u=q[front++]; 24 if(u==t){ printf("%d\n",(int)ans[u]); return 0; } 25 for(int v=1;v<=n;v++)if(!vis[v]&&c(a[u],a[v])){ 26 s_[v]=-s_[u]*a[u].r/a[v].r; 27 ans[v]+=fabs(s_[v])+ans[u]; 28 vis[v]=true; 29 q[tail++]=v; 30 } 31 } 32 return 0; 33 }
深搜:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=1050+5; 5 const double eps=1e-8; 6 struct node{ double x,y,r; }a[maxn]; 7 int n,s,t; 8 int q[maxn]; 9 double xt,yt; 10 bool vis[maxn]; 11 inline bool c(node a,node b){ return fabs(sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2))-a.r-b.r)<eps; } 12 double dfs(int u,double sp,double ans){ 13 if(u==t) return ans; 14 for(int v=1;v<=n;v++)if(!vis[v]&&c(a[u],a[v])){ 15 vis[v]=true; 16 double S=-sp*a[u].r/a[v].r; 17 return dfs(v,S,ans+fabs(S)); 18 } 19 } 20 int main(){ 21 scanf("%d%lf%lf",&n,&xt,&yt); 22 for(int i=1;i<=n;i++){ 23 scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].r); 24 if(a[i].x==0.0&&a[i].y==0.0) s=i; 25 else if(a[i].x==xt&&a[i].y==yt) t=i; 26 } 27 vis[s]=true; 28 printf("%d\n",(int)dfs(s,10000,10000)); 29 return 0; 30 }