[英]How to give input data from a file in c++?
I have a source code in C++ for Bellman's Ford algorithm. 我有C ++的Bellman福特算法源代码。 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 提前致谢
If you want to modify your input to read from the input format 2 then just make the following changes to your input function: 如果要修改输入以读取输入格式2,则只需对输入函数进行以下更改:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.