简体   繁体   English

无法弄清楚我的程序在spoj上给出运行时错误但在ideone上给出运行时错误的原因

[英]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] . i1n包括1n变化,但是如果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.

相关问题 在 SPOJ 上获取运行时错误(SIGSEGV),无法找出我的代码中的错误 - Getting Runtime Error(SIGSEGV) on SPOJ , can't find out what's wrong in my code 我不知道为什么我的 while 循环没有给出预期的结果): - I can't figure out why my while loop is not giving the expected result ): 无法弄清楚为什么我的程序在条件不为真时执行if语句(数组) - Can't figure out why my program is executing a if statement when the condition is not true (arrays) 无法在以下程序中找出运行时错误 - Unable to figure out runtime error in below program 我的程序在完成后打印错误的东西,我不知道为什么。 C++ - My program is printing the wrong thing after completing, and I can't figure out why. C++ Spoj中的SIGSEGV错误,但在ideone中工作正常 - SIGSEGV error in spoj but working fine in ideone 这段代码可以在代码块中完美运行,但在ideone.com上给出了运行时错误 - This code is running perfectly in codeblocks but giving a runtime error on ideone.com 为什么我的代码在SPOJ上给出错误的答案? - Why my code is giving wrong answer on SPOJ? 我有一个错误与我的for循环,不能找出为什么,我有一个“预期声明”错误 - I am having an error with my for loop and can't figure out why, I have an “expected declaration” error 我的最后一个正则表达式不起作用,但是我无法弄清楚为什么 - My last regular expression won't work but i cannot figure out the reason why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM