Hi i wanted to solve "shortest cycle through a given edge " problem that is on this website http://rosalind.info/problems/cte/ . suppose our specific edge (that is our first edge in this problem) be 'E'. i wrote a program to solve this problem and my algorithm is to use DFS on end_node of 'E' and it goes till encounter start_node of 'E'. it works fine for sample given on that website but when i used a large data it goes to a loop. i tried many examples of simple directed graphs and i didn't find why it goes to a loop. can anyone show me if there is anyway that it goes to loop?
here is my program :
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <list>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <stack>
using namespace std;
#define ll long long int
#define pb push_back
#define mk make_pair
const ll maxn = 1e5 + 3;
const ll inf = 1e12 + 10;
vector <ll> cost;
void is_cyclic(int i, bool * recstack,ll cnt,vector <pair<ll,ll>> * vec,ll ind) {
recstack[i] = true;
vector <pair<ll, ll>>::iterator it = vec[i].begin();
for (; it != vec[i].end(); it++) {
if (it->first == ind) {
cnt += it->second;
cost.pb(cnt);
}
else {
if (recstack[it->first] == false) {
is_cyclic(it->first, recstack, cnt + it->second, vec, ind);
}
}
}
recstack[i] = false;
}
int main() {
ll k;
//file >> k;
//while (k--) {
ll n, m;
cin >> n >> m;
ll s;
vector <pair<ll, ll>> * vec;
vec = new vector<pair<ll, ll>>[n];
for (int j = 0; j < m; j++) {
ll a, b, c;
cin >> a >> b >> c;
if (j == 0) {
s = a - 1;
}
vec[a - 1].pb(mk(b - 1, c));
}
bool * recstack = new bool[n];
for (int j = 0; j < n; j++) {
recstack[j] = false;
}
ll cnt = 0;
pair <ll, ll> p;
p = vec[s][0];
cnt += p.second;
recstack[s] = true;
is_cyclic(p.first, recstack, cnt, vec,s);
if (cost.size() == 0) {
cout << -1 << " ";
}
else {
sort(cost.begin(), cost.end());
cout << cost[0] << " ";
}
cost.clear();
//}
return 0;
}