bzoj1615 [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机

感觉自己像个智障

直接bfs

然而读入没判负号查了半小时。。

 1 #include<cstdio>
 2 #include<cctype>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 #define maxn 1100
 8 #define eps 1e-8
 9 int n,tx,ty;
10 int st,ed;
11 int x[maxn],y[maxn],r[maxn];
12 double v[maxn],ans;
13 bool vis[maxn];
14 int q[maxn*5];
15 int fa[maxn];
16 inline int read(){
17     int x=0,f=1;char ch=getchar();
18     while (!isdigit(ch)) {
19         if (ch=='-') f=-1;
20         ch=getchar();
21     }
22     while (isdigit(ch)) {
23         x=x*10+ch-'0';
24         ch=getchar(); 
25     }
26     return x*f;
27 }
28 inline bool ok(int i,int j){
29     double dis=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
30     double d2=r[i]+r[j];
31     if (dis==d2*d2) return 1;
32     else return 0;
33 } 
34 inline void bfs(){
35     int head=0,tail=1;
36     q[head]=st;v[st]=10000;vis[st]=1;
37     while (head<tail){
38         int now=q[head];head++;
39         for (int i=1;i<=n;i++){
40             if (!vis[i]&&ok(now,i)){
41                 vis[i]=1,q[tail++]=i;
42                 fa[i]=now;
43                 v[i]=v[now]/r[i]*r[now];
44                 if (i==ed) return;
45              }
46         }
47     
48     }
49 }
50 int main(){
51     n=read(),tx=read(),ty=read();
52     for (int i=1;i<=n;i++){
53         x[i]=read(),y[i]=read(),r[i]=read();
54         if (x[i]==0&&y[i]==0) st=i;
55         if (x[i]==tx&&y[i]==ty) ed=i;
56     }
57     memset(v,0,sizeof(v));
58     memset(vis,0,sizeof(vis));
59     memset(q,0,sizeof(q));
60     memset(fa,0,sizeof(fa));
61     bfs();
62     ans=0;
63     for (int i=ed;i;i=fa[i])
64       ans+=v[i];
65     printf("%d\n",(int)ans);
66     return 0;
67 }
View Code

 

posted @ 2017-09-09 12:14  Vincent_hwh  阅读(207)  评论(0编辑  收藏  举报