the reason of failure:1、MAXN定义太小了,虽然每条线的距离为100以内,但是有1000以内的线,所以存在1000*100的长度.
thinking:简单BFS找最短,但要注意长度这个问题.
题目:
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
#include <iostream>
#include <queue>
#define MAXN 99999
using namespace std;
int main(){
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
queue<int>qq;
int n,m,i,j,k,t1,t2,t3;
cin >> m >> n;
int map1[n+5][n+5];
int lowcost[n+5];
for(i=1;i<=n;i++){
for(j=1;j<=n;j++)
map1[i][j]=MAXN;
lowcost[i]=MAXN;
}
for(i=0;i<m;i++){
cin >> t1 >> t2 >> t3;
if(t3<map1[t1][t2]){
map1[t1][t2]=t3;
map1[t2][t1]=t3;
}
}
lowcost[n]=0;
qq.push(n);
int a;
while(!qq.empty()){
a=qq.front();
qq.pop();
for(i=1;i<n;i++){
if(map1[a][i]+lowcost[a]<lowcost[i]&&map1[a][i]!=MAXN&&lowcost[a]!=MAXN){
lowcost[i]=lowcost[a]+map1[a][i];
qq.push(i);
}
}
}
cout << lowcost[1] << endl;
return 0;
}