[英]Can't figure out the reason why my program is giving runtime error on spoj but not on ideone
I am solving the problem mice and maze on spoj http://www.spoj.com/problems/MICEMAZE/ I am using a simple recursive approach because the constraints are very small. 我正在解决spoj上的老鼠和迷宫的问题http://www.spoj.com/problems/MICEMAZE/我正在使用一种简单的递归方法,因为约束很小。
My code runs without any error on ideone but it gives a runtime error on spoj (segmentation fault). 我的代码在ideone上运行没有任何错误,但在spoj上却给出了运行时错误(分段错误)。 I can't figure out the problem in my code.
我无法在我的代码中找出问题。
Here is my C++ program and I am using adjacency list to represent the graph. 这是我的C ++程序,我使用邻接表来表示图形。
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
vector<pair<int,int> > graph[100]; //adjacency list , pair stores the destination node of an edge and its weight
int e,t,n,count=0,cost,visited[105];
void maze(int v,int c) //recurssive routine which checks the cost of travelling to each node until it finds the exit node.
{
if(c>t)
return;
if(v==e)
{
if(c<=cost)
cost=c;
return;
}
for(int i=0;i<graph[v].size();i++)
{
if(!visited[graph[v][i].first])
{
visited[graph[v][i].first]=1;
maze(graph[v][i].first,graph[v][i].second+c);
visited[graph[v][i].first]=0;
}
}
}
int main()
{
int a,b,w,m;
cin>>n;
cin>>e;
cin>>t;
cin>>m;
for(int i=0;i<m;i++)
{
cin>>a;
cin>>b;
cin>>w;
graph[a].push_back(make_pair(b,w));
}
for(int i=1;i<=n;i++)
{
cost=INT_MAX;
if(!visited[i])
{
visited[i]=1;
maze(i,0);
visited[i]=0;
}
if(cost<=t)
count++;
}
cout<<count;
return 0;
}
You are going from 1 to n in your 2nd loop. 您将在第二个循环中从1转到n。 If n is 105 then your program will get a segmentation fault.
如果n为105,则您的程序将出现分段错误。
You need to go from 0 to 104 (i=0 ; i< n). 您需要从0到104(i = 0; i <n)。
You run maze(i,0);
您运行
maze(i,0);
with i
varying from 1
to n
inclusively, but if n
is equal to 100
you'll go beyond graph
bounds when you access graph[v]
. i
从1
到n
包括1
和n
变化,但是如果n
等于100
,则在访问graph[v]
时将超出graph
范围。
But even if you fix this, you'll get a time limit error, because although the constraints are small, you effectively check all possible paths with given length in a graph, which is roughly O(n!)
complexity. 但是即使您解决了这个问题,也会遇到时间限制错误,因为尽管约束很小,但是您可以有效地检查图中给定长度的所有可能路径,这大约是
O(n!)
复杂性。
It will be hard to tell why on one system it works and on the other don't without some more info. 很难说清楚为什么在一个系统上可以运行,而在另一个系统上却没有更多信息。 My guess is: memory exceeded.
我的猜测是:内存超出。 Note that exercises pages like SPOJ gives usually memory limit, in your case too.
请注意,在您的情况下,SPOJ之类的练习页面通常也会提供内存限制。 They wrote that N<= 100, So you can make your array fixed since 100 is not very big, but they didn't gave you limit for M!
他们写道N <= 100,所以您可以使数组固定,因为100不是很大,但是他们没有给您M的限制! I bet they prepared tricky input with huge number for M. Memory management for vector is killer in this case because of reallocations (google for more or take a look here Does std::vector *have* to move objects when growing capacity? Or, can allocators "reallocate"? ).
我敢打赌,他们为M准备了数量庞大的棘手输入。在这种情况下,由于重新分配(矢量,Google的管理是杀手(谷歌提供更多信息,或在此处查看), std :: vector是否*在容量增加时可以移动对象?分配器可以“重新分配”吗? )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.