问题 G: Watch Dog
时间限制: 1 Sec 内存限制: 128 MB提交: 44 解决: 14
题目描述
在2050年某城市,一个神秘组织通过一种名叫ctOS的控制系统掌控了全城所有人的联网设备(手机、电脑、监控、交通信号灯,甚至是银行警察局的安保系统),该系统通过很多遍布城市各地的基站来完成对电子设备的控制。
Aiden Pearce(艾登·皮尔斯)是一名超级黑客,因他叛出该组织而被组织报复,谋划了一场"车祸"害死了他的女儿。艾登为了给女儿报仇,决心铲除该组织。
首先他要先侵入stOS系统获取该组织的资料,然而这些资料被分散存到了所有的基站中,所以他要用自己改装的手机获取所有基站的访问权。他的手机电量有限,如果电量为0
以下,则无法继续进行工作。幸运的是,艾登可以通过已经侵入的基站,将其作为傀儡机,继续入侵其它的基站。所消耗的电量为两个设备(手机~基站
、基站~基站
)距离的平方。
举个例子,艾登的初始位置为(0,0)
,有两个基站的位置分别为(1,0)
(0,2)
,艾登可以先从他的位置入侵第一个基站,耗电为1
;再通过第一个基站,入侵第二个基站,耗电为5
。总耗电为6
。
输入
有多组测试数据。
每组数据第一行输入两个整数n
和k
,n
表示一共有n
个基站,k
表示现在手机的电量。
接下来一行输入两个整数p
,q
。表示艾登现在的位置。
接下来的n
行,每行输入两个两个整数:Xi
,Yi
。表示第i
个基站的位置。
1≤n≤500 1≤n≤500
0≤k≤109 0≤k≤109
−200≤x,y,Xi,Yi≤200 −200≤x,y,Xi,Yi≤200
输出
对于每组数据,输出占一行。
如果艾登可以入侵所有的基站,输出:"Go ahead!
"(不带引号)
如果艾登不能入侵所有的基站,输出:"Go a head!
"(不带引号)
样例输入
2 6
0 0
1 0
0 2
3 5
0 0
1 1
1 -1
-1 1
样例输出
Go ahead!
Go a head!
#include<queue>
#include<cmath>
#include<stack>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define CLR(a,b) memset(a,b,sizeof(a))
#define PI acos(-1.0)
struct Position
{
int x,y;
}p[505];
struct Edge
{
int u,v,cost;
bool friend operator < (Edge a,Edge b)
{
return a.cost < b.cost;
}
};
int f[505];
int find(int x)
{
if (x != f[x])
f[x] = find(f[x]);
return f[x];
}
bool join(int x,int y)
{
int fx = find(x);
int fy = find(y);
if (fx != fy)
{
f[fx] = fy;
return true;
}
return false;
}
int main()
{
//freopen("input3.txt","r",stdin);
//freopen("output3.txt","w",stdout);
int n,k;
while (~scanf ("%d %d",&n,&k))
{
n++;
for (int i = 1 ; i <= n ; i++)
{
scanf ("%d %d",&p[i].x,&p[i].y);
f[i] = i;
}
vector<Edge> d;
for (int i = 1 ; i < n ; i++)
{
for (int j = i+1 ; j <= n ; j++)
{
Edge t;
t.u = i;
t.v = j;
t.cost = (p[i].x-p[j].x)*(p[i].x-p[j].x) + (p[i].y-p[j].y)*(p[i].y-p[j].y);
d.push_back(t);
}
}
sort(d.begin(),d.end());
int ach = 0;
for (int i = 0 ; i < d.size() ; i++)
{
if (join(d[i].u,d[i].v))
{
k -= d[i].cost;
ach++;
}
if (k < 0)
{
puts("Go a head!");
break;
}
if (ach == n-1)
{
puts("Go ahead!");
break;
}
}
//***************************
// printf ("%d %d\n",n-1,-k);
// for (int i = 1 ; i <= n ; i++)
// {
// printf ("%d %d\n",p[i].x,p[i].y);
// }
// puts("");
//***************************
}
return 0;
}