UVa10610 - Gopher and Hawks(Dijkstra)

Problem B
Gopher and Hawks
Input:
Standard Input

Output: Standard Output

Time Limit: 2 Seconds

 

A gopher sits in a hole located at (xs,ys) and wants to get to a hole located at (xt, yt). The gopher canrun at a constant speed of v m/sec. However, if the gopher isoutside of a hole for more than a m minutes he will become asupper to hawks flying over the holes. Can the gopher make it?

 

Input 

The input file contains several sets ofinput. The description of each set is given below:

Thefirst line of each set contains two positive integer numbers: v -- gopher's speed in meters persecond and m -- the timeafter which the gopher becomes prey to hawks if he stays outside a hole. Thesecond line of input contains two floating-point numbers: the (xs, ys) coordinates of the gopher’s startinghole. The third line contains the (xt,yt) coordinates of the target hole. Each Subsequent line of input containstwo floating point numbers: the (x,y)coordinates of a gopher hole. All distances are in meters, to the nearest mm. Ablank line terminates the input for each set.

Thelast input set starts with a line containing two zeroes. This set should not beprocessed.

Output 

For each set of input produce one line of output.The description of this line is given below:

If the gopher can make it to the target hole, theoutput line should read "Yes, visiting n other holes.",where n is the minimal number of intermediate holes the gopherhas to visit. If the gopher cannot make it the output line should read "No."There are not more than 1000 gopher holes and all coordinates arebetween -10000 and +10000. See the sample input and output fordetails.

Sample Input                             Output for SampleInput

3 1
0.000 0.000
500.000 0.000
179.000 0.000
358.000 0.000
 
5 1
0.000 0.000
0.000 550.000
179.000 0.000
0.000 301.000 
 
0 0
Yes, visiting 2 other holes.
No.

 


单源最短路问题:

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

const int N = 1010;
const int INF = 0x3f3f3f3f;

struct Point
{
	double x, y;
};

struct Node
{
	int u;
	int d;
	bool operator < (const Node &other) const
	{
		return d > other.d;
	}
};

char buf[N];
double v, m;
vector<Point> vPoint;
vector<int> g[N];
bool vis[N];
int d[N];

bool input();
void solve();
double dis2(const Point &a, const Point &b);

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("d:\\OJ\\uva_in.txt", "r", stdin);
	#endif

	while (input()) {
		solve();
	}
	return 0;
}

bool input()
{
	vPoint.clear();
	
	fgets(buf, N, stdin);
	sscanf(buf, "%lf%lf", &v, &m);
	
	if (v == 0 && m == 0) return false;
	
	while (fgets(buf, N, stdin) != NULL && strcmp(buf, "\n") != 0) {
		double x, y;
		sscanf(buf, "%lf%lf", &x, &y);
		vPoint.push_back((Point){x, y});
	}
	
	return true;
}

double dis2(const Point &a, const Point &b)
{
	double x = a.x - b.x, y = a.y - b.y;
	
	return x * x + y * y;
}

void solve()
{
	for (int i = 0; i < N; i++) g[i].clear();
	
	for (size_t i = 0, size = vPoint.size(); i < size; i++) {
		for (size_t j = i + 1; j < vPoint.size(); j++) {
			double dis = dis2(vPoint[i], vPoint[j]);
			if (dis < v * v * m * m * 3600) {
				g[i].push_back(j);
				g[j].push_back(i);
			}
		}
	}
	
	for (size_t i = 0; i < vPoint.size(); i++) {
		vis[i] = false;
		d[i] = INF;
	}
	
	d[0] = 0;
	
	priority_queue<Node> q;
	q.push((Node){0, 0});
	
	while (!q.empty()) {
		Node curNode = q.top(); q.pop();
		
		int u = curNode.u;
		if (vis[u]) continue;
		
		vis[u] = true;
		for (size_t i = 0; i < g[u].size(); i++) {
			int v = g[u][i];
			if (d[u] + 1 < d[v]) {
				d[v] = d[u] + 1;
				q.push((Node){v, d[v]});
			}
		}
	}
	
	if (d[1] == INF) {
		printf("No.\n");
	} else {
		printf("Yes, visiting %d other holes.\n", d[1] - 1);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值