繁体   English   中英

如何从 c++ 中的文件读取输入

[英]How do I read inputs from file in c++

有没有办法从txt文件中读取输入

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

对于部分

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;
}

只需使用std::ifstream打开文件,然后使用operator>>从中读取值(它将处理跳过值之间的所有空白),例如:

#include <iostream>
#include <fstream>
using namespace std;

...

int main()
{
    int V, u, v, w;

    ifstream ifs("filename.txt");
    if (!ifs.is_open())
    {
        cerr << "Cannot open the file" << endl;
        return 0;
    }

    ifs >> V;
    Graph g(V);

    while (ifs >> u >> v >> w) {
        g.insertEdge(u, v, w);
    }

    cout << "Minimum weight cycle in the graph is: " << g.FindMinCycle() << endl;
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM