题目
1665: [Usaco2006 Open]The Climbing Wall 攀岩
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 197 Solved: 95
[Submit][Status]
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
600 800
1600 1800
100 1300
300 2100
1600 2300
INPUT DETAILS:
The wall is three meters high with 5 hoof-holds.
Sample Output
HINT
题解
处理点的关系,最短路跑一遍!
代码
1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 #include<iostream> 4 #include<cstring> 5 #include<string> 6 #include<algorithm> 7 #include<queue> 8 #include<set> 9 #include<bitset> 10 #include<cstdlib> 11 #include<ctime> 12 #include<cmath> 13 #include<map> 14 using namespace std; 15 16 #define LL long long 17 #define Inf 2147483647 18 #define InfL 10000000000LL 19 20 inline int abs(int x){if (x<0) return -x;return x;} 21 inline int abs(LL x){if (x<0) return -x;return x;} 22 inline void swap(int &x,int &y){int tmp=x;x=y;y=tmp;} 23 inline void swap(LL &x,LL &y){LL tmp=x;x=y;y=tmp;} 24 inline int remin(int a,int b){if (a<b) return a;return b;} 25 inline int remax(int a,int b){if (a>b) return a;return b;} 26 inline LL remin(LL a,LL b){if (a<b) return a;return b;} 27 inline LL remax(LL a,LL b){if (a>b) return a;return b;} 28 inline void read(int &x){x=0;int f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}x=x*f;} 29 inline void read(LL &x){x=0;LL f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}x=x*f;} 30 inline void read(int &x,int &y){read(x);read(y);} 31 inline void read(LL &x,LL &y){read(x);read(y);} 32 inline void read(int &x,int &y,int &z){read(x,y);read(z);} 33 inline void read(int &x,int &y,int &n,int &m){read(x,y);read(n,m);} 34 inline void read(LL &x,LL &y,LL &z){read(x,y);read(z);} 35 inline void read(LL &x,LL &y,LL &n,LL &m){read(x,y);read(n,m);} 36 37 const int Maxf=10000; 38 struct Node{ 39 int x,y; 40 }; 41 Node p[Maxf+10]; 42 int h,f; 43 44 struct Edge{ 45 int v; 46 int dist; 47 int nxt; 48 Edge(){} 49 Edge(int a,int b,int c){v=a;dist=b;nxt=c;} 50 }; 51 const int Maxn=5000000; 52 Edge e[Maxn+10]; 53 int head[Maxf+10]; 54 int nume; 55 inline void addEdge(int x,int y,int dist){ 56 e[++nume]=Edge(y,dist,head[x]); 57 head[x]=nume; 58 e[++nume]=Edge(x,dist,head[y]); 59 head[y]=nume; 60 } 61 inline int getDist(Node a,Node b){ 62 return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); 63 } 64 const int mDist=1000000; 65 66 queue<int> que; 67 bitset<Maxf+10> inque; 68 int dist[Maxf+10]; 69 inline void SPFA(int src){ 70 inque.reset(); 71 memset(dist,127,sizeof(dist)); 72 que.push(src); 73 inque[src]=true; 74 dist[src]=0; 75 while(!que.empty()){ 76 int now=que.front(); 77 que.pop(); 78 for (int i=head[now];i;i=e[i].nxt){ 79 int v=e[i].v; 80 int w=e[i].dist; 81 if (dist[v]>dist[now]+w){ 82 dist[v]=dist[now]+w; 83 if (inque[v]==false){ 84 inque[v]=true; 85 que.push(v); 86 } 87 } 88 } 89 inque[now]=false; 90 } 91 92 } 93 94 int main(){ 95 read(h,f); 96 for (int i=1;i<=f;i++) read(p[i].x,p[i].y); 97 for (int i=1;i<f;i++){ 98 for (int j=i+1;j<=f;j++){ 99 int tDist=getDist(p[i],p[j]); 100 if (tDist<=mDist){ 101 addEdge(i+1,j+1,1); 102 //cout<<i<<" "<<j<<endl; 103 } 104 } 105 } 106 107 //cout<<nume<<endl; 108 for (int i=1;i<=f;i++){ 109 if (h-1000<=p[i].y){ 110 addEdge(1,i+1,0); 111 //cout<<0<<" "<<i<<endl; 112 } 113 if (p[i].y<=1000){ 114 addEdge(i+1,f+2,0); 115 //cout<<i<<" "<<f+1<<endl; 116 } 117 } 118 119 SPFA(1); 120 121 cout<<dist[f+2]+1<<endl; 122 123 return 0; 124 }