1665: [Usaco2006 Open]The Climbing Wall 攀岩

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 229  Solved: 120
[Submit][Status][Discuss]

Description

One of the most popular attractions at the county fair is the climbing wall. Bessie wants to plan her trip up the wall in advance and needs your help. The wall is 30,000 millimeters wide and H (1001 <= H <= 30,000) millimeters high and has F (1 <= F <= 10,000) hoof-holds at unique X,Y coordinates expressed in millimeters. 0,0 is at the ground level on the left side of the wall. Hoof-holds are separated by at least 300 millimeters since no cow can maneuver them if they are spaced too close! Bessie knows there is at least one way up. Bessie, through techniques only she knows, uses successive single hoof-holds to climb the wall. She can only move from one hoof-hold to another if they are no more than one meter apart. She can, of course, move up, down, right, left or some combination of these in each move. Similarly, once she gets to a hoof-hold that is at least H-1000 millimeters above the ground, she can nimbly climb from there onto the platform atop the wall. Bessie can start at any X location that has a Y location <= 1000 millimeters. Given the height of the wall and the locations of the hoof-holds, determine the smallest number of hoof-holds Bessie should use to reach the top.

 

Bessie参加了爬墙比赛,比赛用的墙宽30000,高H(1001 <= H <= 30,000)。墙上有F(1 <= F <= 10,000)个不同的落脚点(X,Y)。 (0,0)在左下角的地面。所有的落脚点至少相距300。Bessie知道至少有一条路可以上去。 Bessie只能从一个落脚点爬到另一个距离不超过1000的落脚点,她可以向上下左右四个方向爬行。同样地,一旦她到达了一个高度 至少有H-1000的落脚点,她可以敏捷地爬到墙顶上。Bessie一开始可以在任意一个高度不超过1000的落脚点上。问Bessie至少攀爬多少次.这里两个点的距离都是欧几里得距离

Input

* Line 1: Two space-separated integers, H and F.

* Lines 2..F+1: Each line contains two space-separated integers (respectively X and Y) that describe a hoof-hold. X is the distance from the left edge of the climbing wall; Y is the distance from the ground.

Output

* Line 1: A single integer that is the smallest number of hoof-holds Bessie must use to reach the top of the climbing wall.

Sample Input

3000 5
600 800
1600 1800
100 1300
300 2100
1600 2300

INPUT DETAILS:

The wall is three meters high with 5 hoof-holds.

Sample Output

3

HINT

 分别经过(600,800), (100,1300), (300,2100)

Source

Silver

不难吧,看懂题意后就不难想到是最短路了,然后跑一遍spfa或者dijkstra就好了,比较喜欢spfa比较容易写

a-------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
const int nmax=3000005;
const int maxn=10005;
struct edge{
 int to,next;
};
edge e[nmax];
int x[maxn],y[maxn],head[maxn],d[maxn],v[maxn];
int n,cur=0,m;
double dis(int a,int b){
 return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
}
void update(int x,int y){
 cur++;
 e[cur].next=head[x];
 e[cur].to=y;
 head[x]=cur;
 cur++;
 e[cur].next=head[y];
 e[cur].to=x;
 head[y]=cur;
}
void insert(){
 for(int i=1;i<n;i++){
  for(int j=i+1;j<=n;j++){
   if(dis(i,j)<=1000)
               update(i,j);
           
  }
 }
 for(int i=1;i<=n;i++){
  if(y[i]+1000>=m)
    update(i,n+1);
  if(y[i]<=1000)
    update(i,0);
 }
}
void spfa(){
 queue<int>q;
 q.push(0);
 memset(d,0x3f,sizeof(d));
 d[0]=0;
 v[0]=1;
 while(!q.empty()){
  int tmp=q.front();
  q.pop();
  v[tmp]=0;
  for(int j=head[tmp];j;j=e[j].next){
   int temp=e[j].to;
   if(d[temp]>d[tmp]+1){
    d[temp]=d[tmp]+1;
    if(!v[temp]){
     v[temp]=1;
     q.push(temp);
    }
   }
  }
 }
}
int main(){
 memset(head,0,sizeof(head));
 scanf("%d%d",&m,&n);
 for(int i=1;i<=n;i++){
  scanf("%d%d",&x[i],&y[i]);
 }
 insert();
 spfa();
 printf("%d\n",d[n+1]-1);
 return 0;
}

----------------------------------------------------------------------------------