Search code examples
c++algorithmfilebellman-ford

How to give input data from a file in c++?


I have a source code in C++ for Bellman's Ford algorithm. It gets an input file with nodes,edges,source node and destination node and gives back the shortest path from on node to another. File's type is:

30 150 29 30  //Vertices,Nodes,SourceNode,DestinationNode  
30 25 20     // Node30 , to Node 25 , Weight 20
1 2 29
1 3 68
24 22 8
1 5 61
24 23 76
5 4 62

So, i want to make it to read input from a file like:

30 150   //Vertices,Nodes  
30 25 20   // Node30 , to Node 25 , Weight 20
1 2 29
1 3 68
24 22 8
1 5 61
24 23 76
5 4 62
29 30  //SourceNode,DestinationNode

Here is my source Code:

   #include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <algorithm>

#define MaxN 111111
#define INF 1000000000

using namespace std;

int n, m, s, f;
vector< pair<int, int> > adj[MaxN];
int d[MaxN];
int rear, front;
int queue[MaxN];
bool inqueue[MaxN];

void input(){
    int i, u, v, w;
    cin >> n >> m >> s >> f;
    for (i = 1; i <= n; i++) 
        adj[i].clear();
    for (i = 1; i <= m; i++){
        cin >> u >> v >> w;
        adj[u].push_back(make_pair(v, w));
        adj[v].push_back(make_pair(u, w));
    }
}

void push(int v){
    if (inqueue[v]) return;
    front = (front + 1) % n;
    queue[front] = v;
    inqueue[v] = true;
}

int pop(){
    rear = (rear + 1) % n;
    int v = queue[rear];
    inqueue[v] = false;
    return v;
}

void Bellman_Ford(){
    int i, u, v, w;
    memset(inqueue, false, sizeof(inqueue));
    for (i = 1; i <= n; i++) d[i] = INF;
    d[s] = 0;
    rear = 0;
    front = 0;
    push(s);
    while  (rear != front){
        u = pop();
        for (i = 0; i < adj[u].size(); i++){
            v = adj[u][i].first;
            w = adj[u][i].second;
            if (d[v] > d[u] + w){
                d[v] = d[u] + w;
                push(v);
            }
        }
    }


    if (d[f] == INF) cout << "-1" << endl; cout << d[f] << endl;
}

int main(){
    freopen("net30.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    input();
    Bellman_Ford();
}

Thanks in advance


Solution

  • If you want to modify your input to read from the input format 2 then just make the following changes to your input function:

    void input(){
        int i, u, v, w;
        ifstream fin("net30.txt");
        fin >> n >> m ;
        for (i = 1; i <= n; i++) 
            adj[i].clear();
        for (i = 1; i <= m; i++){
            fin >> u >> v >> w;
            adj[u].push_back(make_pair(v, w));
            adj[v].push_back(make_pair(u, w));
        }
        fin >> s >> f;
        fin.close();
    }
    

    However, to convert the content of the file, you will need to write a separate function to read the file contents and then re-write it to the same file. This function will do that:

    void changeInputFileFormat(){
        int i, u, v, w;
        ifstream fin("net30.txt");
    
        fin >> n >> m >> s >> f;
        vector<pair<int,pair<int,int> > > edges;
        for (i = 1; i <= m; i++){
            fin >> u >> v >> w;
            edges.push_back(make_pair(u, make_pair(v, w)));
        }
        fin.close();
    
    
        ofstream fout("net30.txt");
        //for converting to format 2
        fout<<n<<" "<<m<<endl;
        for( int i=0; i<edges.size(); i++ ){
            fout<<edges[i].first<<" "<<edges[i].second.first<<" "<<edges[i].second.second<<endl;
        }
        fout<<s<<" "<<f<<endl;
        fout.close();
    }