Problem B
Gopher and Hawks
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds
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);
}
}