is there a way to read inputs from txt file
6
0 1 1
2 3 1
1 2 1
3 0 1
4 0 1
4 5 1
3 4 1
5 3 1
for the part
int V = 6;
Graph g(V);
g.insertEdge(0,1,1);
g.insertEdge(2,3,1);
g.insertEdge(1,2,1);
g.insertEdge(3,0,1);
g.insertEdge(4,0,1);
g.insertEdge(4,5,1);
g.insertEdge(3,4,1);
g.insertEdge(5,3,1);
#include<bits/stdc++.h>
using namespace std;
# define INF 0x3f3f3f3f
// creating a struct for an edge
struct Edge
{
int u, v, wt;
};
class Graph
{
int V;
// adjacency list
list < pair <int, int > >*adjacency;
vector < Edge > edges;
public :
Graph( int V )
{
this->V = V ;
adjacency = new list < pair <int, int > >[V];
}
// declaring all the functions
// inserting an edge
void insertEdge ( int u, int v, int w );
// deleting an edge
void deleteEdge( int u, int v, int w );
// finding minimum path
int minimumPath (int u, int v );
// deleting an edge
void deleteEdge( int u, int v );
// finding min cycles
int FindMinCycle();
};
// inserting an edge
void Graph :: insertEdge ( int u, int v, int w )
{
adjacency[u].push_back( make_pair( v, w ));
adjacency[v].push_back( make_pair( u, w ));
Edge e = { u, v, w };
edges.push_back ( e );
}
// deleting an edge
void Graph :: deleteEdge( int u, int v, int w )
{
adjacency[u].remove(make_pair( v, w ));
adjacency[v].remove(make_pair(u, w ));
}
// finding minimum path function
int Graph :: minimumPath ( int u, int v )
{
// storing vertices
set< pair<int, int> > setds;
// vector for distances
vector<int> dist(V, INF);
/* insert self source at first and initialize its distance as 0 */
setds.insert(make_pair(0, u));
dist[u] = 0;
while (!setds.empty())
{
/* The first vertex in Set is the one with the shortest distance; remove it from Set. */
pair<int, int> tmp = *(setds.begin());
setds.erase(setds.begin());
/* To preserve the vertices sorted distance, vertex label must be put in second of pair (distance must be first item in pair) */
int u = tmp.second;
list< pair<int, int> >::iterator i;
for (i = adjacency[u].begin(); i != adjacency[u].end(); ++i)
{
int v = (*i).first;
int weight = (*i).second;
if (dist[v] > dist[u] + weight)
{
/* If the distance of v is not INF, then it must be in our set; therefore, it should be removed and reinserted with an updated shorter distance. We only remove from Set the vertices for which the distance has been determined. Therefore, they would never see us arrive here. */
if (dist[v] != INF)
setds.erase(setds.find(make_pair(dist[v], v)));
dist[v] = dist[u] + weight;
setds.insert(make_pair(dist[v], v));
}
}
}
return dist[v] ;
}
// finding minimum path function
int Graph :: FindMinCycle ( )
{
int min_cycle = INT_MAX;
int E = edges.size();
for ( int i = 0 ; i < E ; i++ )
{
Edge e = edges[i];
/* Obtain the edge vertices that we currently delete from the graph, and then use Dijkstra's shortest path technique to discover the shortest path between these two vertices. */
deleteEdge( e.u, e.v, e.wt ) ;
int dist = minimumPath( e.u, e.v );
/* If this is the shortest cycle, update min cycle; otherwise, add weight to currently deleted edges to create a cycle. */
min_cycle = min(min_cycle, dist + e.wt);
// add current edge back to the graph
insertEdge( e.u, e.v, e.wt );
}
return min_cycle ;
}
int main()
{
int V = 6;
Graph g(V);
g.insertEdge(0,1,1);
g.insertEdge(2,3,1);
g.insertEdge(1,2,1);
g.insertEdge(3,0,1);
g.insertEdge(4,0,1);
g.insertEdge(4,5,1);
g.insertEdge(3,4,1);
g.insertEdge(5,3,1);
cout << "Minimum weight cycle in the graph is: "<<g.FindMinCycle() << endl;
return 0;
}