【CCF】无线网络 搜索+思维

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<string>
  5 #include<cmath>
  6 #include<algorithm>
  7 #include<queue>
  8 #include<map>
  9 #include<stack>
 10 #include<vector>
 11 
 12 using namespace std;
 13 typedef long long ll;
 14 const double eps=1e-6;
 15 const int maxn=2e2+2;
 16 const int maxm=maxn*maxn;
 17 int n,m,k;
 18 ll r;
 19 struct node{
 20     ll x;
 21     ll y;
 22     node(ll _x,ll _y):x(_x),y(_y){}
 23 };
 24 vector<node> g;
 25 bool vis[maxn];
 26 struct Node{
 27     int id;
 28     int step;
 29     int k;
 30     Node(int _id,int _step,int _k):id(_id),step(_step),k(_k){}
 31 };
 32 bool judge(int u,int v){
 33     ll tmp=(g[u].x-g[v].x)*(g[u].x-g[v].x)+(g[u].y-g[v].y)*(g[u].y-g[v].y);
 34     if(tmp<=r*r) return true;
 35     return false; 
 36 }
 37 struct edge{
 38     int to;
 39     int nxt;
 40 }e[2*maxm];
 41 int tot;
 42 int head[maxn];
 43 void init(){
 44     g.clear();
 45     memset(head,-1,sizeof(head));
 46     tot=0;
 47     memset(vis,false,sizeof(vis));
 48 }
 49 void add(int u,int v){
 50     e[tot].to=v;
 51     e[tot].nxt=head[u];
 52     head[u]=tot++;
 53 }
 54 int bfs(){
 55     int s=1,t=2;
 56     queue<Node> Q;
 57     Q.push(Node(s,0,0));
 58     while(!Q.empty()){
 59         Node q=Q.front();
 60         Q.pop();
 61         if(q.id==t){
 62             return q.step;
 63         }
 64         if(vis[q.id]) continue;
 65         vis[q.id]=true;
 66         int u=q.id;
 67         for(int i=head[u];i!=-1;i=e[i].nxt){
 68             int v=e[i].to;
 69             if(v<=n){
 70                 Q.push(Node(v,q.step+1,q.k)); 
 71             }else{
 72                 if(q.k+1<=k){
 73                     Q.push(Node(v,q.step+1,q.k+1));
 74                 }
 75             }
 76         }
 77         
 78     }
 79     return -1;
 80 }
 81 int work(){
 82     return bfs()-1;
 83 }
 84 int main(){
 85     while(~scanf("%d%d%d%lld",&n,&m,&k,&r)){
 86         init();
 87         ll x,y;
 88         g.push_back(node(0,0));
 89         for(int i=1;i<=n;i++){
 90             scanf("%lld%lld",&x,&y);
 91             g.push_back(node(x,y));
 92         }
 93         for(int i=1;i<=m;i++){
 94             scanf("%lld%lld",&x,&y);
 95             g.push_back(node(x,y));
 96         }
 97         for(int i=1;i<=n+m;i++){
 98             for(int j=i+1;j<=n+m;j++){
 99                 if(judge(i,j)){
100                     add(i,j);
101                     add(j,i);
102                 }
103             }
104         }
105         int ans=work();
106         printf("%d\n",ans);
107     }
108     return 0;
109 }

 

posted @ 2018-06-17 23:27  shulin15  阅读(313)  评论(0编辑  收藏  举报