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.
My code runs without any error on ideone but it gives a runtime error on spoj (segmentation fault). 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.
#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;
}