hdu 4885TIANKENG’s travel bfs



TIANKENG’s travel

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 346    Accepted Submission(s): 85


Problem Description
TIANKENG has get a driving license and one day he is so lucky to find a car. Every day he drives the car around the city. After a month TIANKENG finds that he has got high driving skills and he wants to drive home. Assuming that we regard the map of Hangzhou as a two-dimensional Cartesian coordinate system, the coordinate of the school is (sx, sy), the coordinate of TIANKENG’s home is (ex,ey). There are n gas stations distributing along the road and the coordinate of the ith gas station is (xi, yi). Because there is something wrong with the car engine, TIANKENG can drive the car for L miles after every time he fills up the gas. Assuming that TIANKENG must fill up the gas when he passes by a gas station and the initial status of the tank is full. TIANKENG must drive on the straight line and he can only choose to drive to school or home or gas station. Your task is to tell TIANKENG the minimum times he needs to charge fuel to go home successfully.
 

Input
The first line contains a positive integer T(T<=30), referring to T test cases.
For each test case, the first line contains two integers n(0<=n<=1000), L(1<=L<=100000), which mean n gas stations in all and the distance TIANKENG can drive with the full tank respectively. The second line contains two integers sx, sy, which refers to the coordinate of the school.
The third line contains two integers ex, ey, which refers to the coordinate of TIANKENG’s home.
Then following n lines, each line contains two integer xi, yi, which refers to the coordinate of gas station.

Assuming that all the coordinates are different! All the coordinates satisfied [0,1000000].
 

Output
For each test case, if TIANKENG cannot go home successfully, print “impossible”(without quotes), otherwise print the minimum times of charging fuel.
 

Sample Input
  
  
2 2 1000 0 0 2000 1000 1000 0 2000 0 0 1000 0 0 2000 1000
 

Sample Output
  
  
2 impossible
 

Source
因为经过加油站必须要加油,就可以把边的权值视为1,最短路就成了bfs,要考虑的是三点共线时的情况,这里参考了别人的方法,将点按第一关键字为x,第二关键字为y排序,进行循环判断,利用set的查找记录已存在的斜率,若该斜率已有,则不能再添加边
#pragma warning(disable:4996)
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX_N = 1005;
typedef pair<int,int> p;
typedef __int64 ll;
vector<int> g[MAX_N];
int dis[MAX_N];
bool vis[MAX_N];
struct type{
	int x, y,id;
}v[MAX_N];
int n,cas;
ll l;
set<p> s;
int gcd(int a, int b){
	return b == 0 ? a : gcd(b,a%b);
}
bool cmp(const type&a, const type&b){
	if (a.x == b.x) return a.y < b.y;
	return a.x < b.x;
}

bool search(int x, int y){
	int temp = gcd(x, y);
	if (temp < 0) temp = -temp;
	x /= temp; y /= temp;
	if (s.find(make_pair(x, y)) != s.end()) return true;
	s.insert(make_pair(x,y));
	return false;
}
void addEdge(int i, int j){
	ll d = (ll)(pow(v[i].x - v[j].x, 2) + pow(v[i].y - v[j].y, 2));
	//cout << "add" << i << " " << j << endl;
	if (d <= l && !search(v[j].x - v[i].x, v[j].y - v[i].y)){
		g[v[i].id].push_back(v[j].id);
		g[v[j].id].push_back(v[i].id);
		//cout << "link:" << v[i].x << " " << v[i].y << "          " << v[j].x << " " << v[j].y << endl;
		
	}
}
void init(){
	scanf("%d%lld",&n,&l);
	l *= l;
	scanf("%d%d%d%d", &v[0].x, &v[0].y, &v[1].x, &v[1].y);
	v[0].id = 0;
	v[1].id = 1;
	n += 2;
	for (int i = 2; i < n; i++){
		scanf("%d%d", &v[i].x, &v[i].y);
		v[i].id = i;
	}
	sort(v, v + n,cmp);
	for (int i = 0; i < n; i++)
		g[i].clear();
	for (int i = 0; i < n; i++){
		s.clear();
		for (int j = i + 1; j < n; j++)
			addEdge(i,j);
		
	}
}
void solve(){
	memset(vis,0,sizeof(vis));
	memset(dis,-1,sizeof(dis));
	dis[0] = 0; vis[0] = 1;
	queue<int> que;
	que.push(0);
	while (!que.empty()){
		int now = que.front();
		que.pop();
		//cout << "now" << v[now].x << " " << v[now].y << endl;
		if (now == 1){
			printf("%d\n",dis[now]-1);
			return;
		}
		for (int i = 0; i < g[now].size(); i++){
			int j = g[now][i];
			if (!vis[j]){
				que.push(j);
				dis[j] = dis[now] + 1;
				vis[j] = 1;
			}
		}
	}
	printf("impossible\n");

}
int main(){
	//freopen("aaa.txt","r",stdin);
	scanf("%d",&cas);
	for (int i = 0; i < cas;i++){
		//cout << "case:" <<i<< endl;
		init();
		solve();
	}
	//while (1);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值