usingnamespace std;
#include <iostream>
#include <fstream>
#include <array>
#include <vector>
class arc
{
// const int M=1000000;
public:
arc(int Id=0, int co=1000000, int tr=1000000) : ID(Id), cost(co), transit_time(tr) {}
int ID; // The tail(nodes) or head(B) of arc.
int cost;
//int cost_scaled;
int transit_time;
};
class Network{
public:
std::array<std:: vector<arc>,4> nodes;
int n; //number of nodes.
int s; //source
int t; // sink
int T; //time horizon
void nodes_initials();
void create_Network();
void add_arc_nodes(int , arc );
void print_cost();
};
void Network::nodes_initials(){
std::cout<< "Please enter the number of nodes:"<<endl;
std::cin>> n;
std::cout<< "Please enter the ID of source:"<<endl;
std::cin>> s;
std::cout<< "Please enter the ID of sink:"<<endl;
std::cin>> t;
std::cout<< "Please enter the time horizon:"<<endl;
std::cin>> T;
//nodes=new arc *[n+1];
//B=new arc *[n+1];
//for (int i=0; i<=n; i++)
//{
// nodes[i]=new arc;
// B[i]=new arc;
//}
}
void Network::add_arc_nodes(int i, arc a) // i is the head of arc a.
{
nodes[i].push_back(a);
}
void Network::create_Network() // read the network's parameter from data file.
{
int i;//head of arc.
int j;//tail of arc.
int co;// cost of arc.
int tr; //transit time of arc.
ifstream file;
file.open("Data.txt", ios::in);
while(file >> i >> j >> co >> tr)
{
arc a(j, co, tr);
add_arc_nodes(i,a);
}
file.close();
}
void Network::print_cost()
{
for(int i=0; i<4; i++)
{for (std::vector<arc>::iterator it = nodes[i].begin() ; it != nodes[i].end(); ++it)
std::cout << ' ' << *it->cost;
std::cout << '\n';
}
std::cout << '\n';
}
int main()
{ Network x;
int i;
x.nodes_initials();
x.create_Network();
x.print_cost();
std::cin>>i;
return 0;
}
The Data.tex file is as follows:
1 2 5 6
1 3 4 5
2 3 1 4
2 4 3 2
3 4 4 3
where each row represents an arc, the second column is "tail", the third is "cost" and the forth is "transit_time"