为什么这个 Dijkstra(图)实现不起作用?
我针对这个问题做了这个实现: http://www.spoj.pl/problems/SHOP/
#include<iostream>
#include<stdio.h>
#include<queue>
#include<conio.h>
#include<string.h>
using namespace std;
struct node
{
int x;
int y;
int time;
};
bool operator <(const node &s,const node &r)
{
if(s.time>r.time)
return true;
else return false;
}
node beg,src,dest,tempa;
int b,a,temp;
int map[25][25];
bool vis[25][25];
int X[]={1,0,-1,0};
int Y[]={0,1,0,-1};
int djs_bfs(node src,node dest)
{
int result=0;
priority_queue<node>pq;
pq.push(src);
while(!pq.empty())
{
node top = pq.top();
pq.pop();
if(top.x==dest.x && top.y==dest.y) return result;
if(top.x<0 || top.x>=a) continue;
if(top.y<0 || top.y>=b) continue;
if(vis[top.x][top.y]) continue;
vis[top.x][top.y]=true;
result+=map[top.x][top.y];
for(int i=0;i<4;i++)
{
tempa.x=top.x+X[0];
tempa.y=top.y+Y[0];
tempa.time=map[top.x+X[0]][top.y+Y[0]];
pq.push(tempa);
}
}
return -1;
}
int main()
{
memset(vis,false,sizeof(vis));
scanf("%d %d",&a,&b);
while(a != 0)
{
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
{
scanf("%c",&temp);
if(temp=='X') {map[i][j]=0;vis[i][j]=true;}
if(temp=='S') {src.x=i;src.y=j;src.time=0;}
if(temp=='D') {dest.x=i;dest.y=j;dest.time=0;}
else map[i][j]=temp-'0';
}
cout<<djs_bfs(src,dest)<<endl;
scanf("%d %d",&a,&b);
}
return 0;
getch();
}
我不知道为什么我的代码没有为测试用例生成正确的答案。 如果有人可以帮助我改进代码,请这样做:D
I made this implementation for this problem :
http://www.spoj.pl/problems/SHOP/
#include<iostream>
#include<stdio.h>
#include<queue>
#include<conio.h>
#include<string.h>
using namespace std;
struct node
{
int x;
int y;
int time;
};
bool operator <(const node &s,const node &r)
{
if(s.time>r.time)
return true;
else return false;
}
node beg,src,dest,tempa;
int b,a,temp;
int map[25][25];
bool vis[25][25];
int X[]={1,0,-1,0};
int Y[]={0,1,0,-1};
int djs_bfs(node src,node dest)
{
int result=0;
priority_queue<node>pq;
pq.push(src);
while(!pq.empty())
{
node top = pq.top();
pq.pop();
if(top.x==dest.x && top.y==dest.y) return result;
if(top.x<0 || top.x>=a) continue;
if(top.y<0 || top.y>=b) continue;
if(vis[top.x][top.y]) continue;
vis[top.x][top.y]=true;
result+=map[top.x][top.y];
for(int i=0;i<4;i++)
{
tempa.x=top.x+X[0];
tempa.y=top.y+Y[0];
tempa.time=map[top.x+X[0]][top.y+Y[0]];
pq.push(tempa);
}
}
return -1;
}
int main()
{
memset(vis,false,sizeof(vis));
scanf("%d %d",&a,&b);
while(a != 0)
{
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
{
scanf("%c",&temp);
if(temp=='X') {map[i][j]=0;vis[i][j]=true;}
if(temp=='S') {src.x=i;src.y=j;src.time=0;}
if(temp=='D') {dest.x=i;dest.y=j;dest.time=0;}
else map[i][j]=temp-'0';
}
cout<<djs_bfs(src,dest)<<endl;
scanf("%d %d",&a,&b);
}
return 0;
getch();
}
I don't know why my code doesn't generate the right answer for the testcases.
If someone can help me improve the code, please do so :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,图解析代码不正确。第一行指定宽度和高度,其中宽度是每行的字符数,高度是行数。因此,请在第一个 scanf 中交换
&a
和&b
,或者交换嵌套for
循环的顺序(但不能同时交换两者) 。另外,我必须在不同的地方添加虚拟scanf("%c", &dummy);
调用来过滤掉换行符。像这样的简单转储将有助于确定您的地图是否已正确解析:注意:我还将“S”和“D”的
map[i][j]
设置为 0,同时也进行了更改将重复的if
语句放入if 中;否则如果; else
链。这使得算法更加稳健,因为您通常可以添加来自源或目的地的时间。现在,讨论算法本身......
算法的每个循环都会将结果增加当前地图位置权重。然而,该算法同时搜索多条路径(即优先级队列中的条目数),因此结果最终是所有已处理节点权重的总和,而不是当前路径权重。当前路径权重为
top.temp
,因此您可以消除result
变量,并在到达目的地时简单地返回top.temp
。另外,正如其他答案所述,您需要在内部循环中使用
X[i]
和Y[i]
,否则您只能朝一个方向搜索。现在,由于
X[i]
和Y[i]
的加/减,您可能会访问map[][]
out范围(-1 或 25)。因此,我建议将if
防护移至内部for
循环,以防止超出范围的访问。这也避免了优先级队列被非法可能性填满。这是我的算法版本,经过最少的修正,供参考:
我希望这会有所帮助。
First of all, the graph parsing code is incorrect. The first line specifies width and height, where the width is the number of characters per line the height is the number of lines. Therefore, swap
&a
and&b
in the first scanf, or swap the order of the nestedfor
loops (but not both). Also, I had to add dummyscanf("%c", &dummy);
calls at various places to filter out newlines. A simple dump, such as this, will help determine if your map was parsed correctly:Note: I also set
map[i][j]
to 0 for 'S' and 'D', also changing the repeatedif
statements into anif; else if; else
chain. This makes the algorithm more robust, since you can generically add time from the source or destination.Now, on to the algorithm itself....
Each loop of the algorithm increments
result
by the current map location weight. However, the algorithm is searching multiple paths simultaneously (i.e., the number of entries in the priority queue), and thereforeresult
ends up being the sum of all processed node weights, not the current path weight. The current path weight istop.temp
, and therefore you can eliminate theresult
variable and simply returntop.temp
when you reach the destination.Also, as other answers noted, you need to use
X[i]
andY[i]
in your inner loop, otherwise you are only searching in one direction.Now, because of the addition/subtraction from
X[i]
andY[i]
, you will likely accessmap[][]
out of range (-1 or 25). Therefore, I recommend moving theif
guards to the innerfor
loop to guard against the out-of-range access. This also avoids filling the priority queue with illegal possibilities.Here is my version of the algorithm, with minimal corrections, for reference:
I hope this helps.
为什么你有0个索引?
挑剔:
这不是更易读吗:
Why do you have 0 indexes?
Nitpick:
Isn't this more readable:
您使用的是
X[0]
和Y[0]
,而不是X[i]
和Y[i] 在该内部循环中。
顺便说一句,除此之外,你的 Dijkstra 效率非常低。首先,即使节点已经被访问过,您也将节点推送到队列中;其次,队列中可能有多个相同的节点,只是时间不同。最终,这些事情都不会影响结果,但你正在改变复杂性。
编辑:哦,tempa.time应该等于top.time加上边权重,而不仅仅是边权重。
You're using
X[0]
andY[0]
instead ofX[i]
andY[i]
in that inner loop.By the way, other than that your Dijkstra's is very inefficient. First, you're pushing nodes onto the queue even when they have already been visited, and secondly, you may have several of the same node in the queue, just with different times. Ultimately neither of these things effect the outcome, but you're changing the complexity.
Edit: Oh,
tempa.time
should equaltop.time
plus the edge weight, not just the edge weight.