Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- class NWCM{
- int cost[10][10], capacity[10], requirement[10], allotment[10][10];
- int cap_size, req_size;
- public:
- NWCM(int cap_size, int req_size);
- void input();
- void displayAllotment(bool show_cap_req = true);
- void displayCost();
- void computeTransportationCost();
- bool requirementFulfilled();
- int min(int num1, int num2);
- };
- int NWCM::min(int num1, int num2){
- return num1 < num2 ? num1 : num2;
- }
- NWCM::NWCM(int cap_size, int req_size){
- NWCM::cap_size = cap_size;
- NWCM::req_size = req_size;
- // Initializing cost, allotment, requirement, capacity matrix
- for(int i = 1; i <= cap_size; i++){
- capacity[i] = 0;
- requirement[i] = 0;
- for(int j = 1; j <= req_size; j++){
- cost[i][j] = 0;
- allotment[i][j] = 0;
- }
- }
- }
- void NWCM::input(){
- cout << "Enter the elements of cost matrix, capacity, requirement-->\n" << endl;
- // Taking input in cost matrix
- for(int i = 1; i <= cap_size; i++){
- for(int j = 1; j <= req_size; j++){
- cout << "cost[" << i << "][" << j << "] = ";
- cin >> cost[i][j];
- }
- }
- // Taking input in capacity matrix
- for(int i = 1; i <= cap_size; i++){
- cout << "capacity[" << i << "] = ";
- cin >> capacity[i];
- }
- // Taking input in requirement matrix
- for(int i = 1; i <= req_size; i++){
- cout << "requirement[" << i << "] = ";
- cin >> requirement[i];
- }
- }
- void NWCM::displayAllotment(bool show_cap_req){
- for(int i = 1; i <= cap_size; i++){
- for(int j = 1; j <= req_size; j++){
- cout << allotment[i][j] << "\t";
- }
- if(show_cap_req){
- cout << capacity[i];
- }
- cout << endl;
- }
- if(show_cap_req)
- {
- // Requirement
- for(int i = 1; i <= req_size; i++){
- cout << requirement[i] << "\t";
- }
- }
- cout << endl;
- }
- void NWCM::displayCost(){
- for(int i = 1; i <= cap_size; i++){
- for(int j = 1; j <= req_size; j++){
- cout << cost[i][j] << "\t";
- }
- cout << endl;
- }
- }
- void NWCM::computeTransportationCost(){
- int row = 1, col = 1;
- int iteration = 0;
- // We continue perfoming allotments till all the capacity
- // and requirement
- while(!requirementFulfilled()){
- cout << "requirementFulfilled() = " << requirementFulfilled() << endl;
- cout << "Allotment Matrix after iteration-" << iteration << "--->" << endl;
- displayAllotment();
- cin.get();
- cout << "\n\n\n";
- int min_allotment = min(capacity[row], requirement[col]);
- allotment[row][col] = min_allotment;
- capacity[row] = capacity[row] - min_allotment;
- requirement[col] = requirement[col] - min_allotment;
- if(capacity[row] == 0){
- row = row + 1;
- }
- if(requirement[col] == 0){
- col = col + 1;
- }
- iteration = iteration + 1;
- }
- int total_cost = 0;
- for(int i = 1; i <= cap_size; i++){
- for(int j = 1; j <= req_size; j++){
- total_cost += allotment[i][j] * cost[i][j];
- }
- }
- cout << "Elements in allotment matrix-->" << endl;
- displayAllotment(false);
- cout << endl << endl;
- cout << "Elements in cost matrix-->" << endl;
- displayCost();
- cout << endl << endl;
- cout << "Total transportation cost using NWCM = " << total_cost << endl;
- }
- bool NWCM::requirementFulfilled(){
- bool req = true, cap = true;
- // Checking if all the elements of the requirement matrix is 0
- for(int i = 1; i <= req_size; i++){
- if(requirement[i] != 0){
- req = false;
- break;
- }
- }
- // Checking if all the elements of the capacity matrix is 0
- for(int i = 1; i <= cap_size; i++){
- if(capacity[i] != 0){
- cap = false;
- break;
- }
- }
- return req && cap;
- }
- int main() {
- system("cls");
- cout << "Enter the number of capacities(1-9) = ";
- int cap;
- cin >> cap;
- cout << "Enter the number of requirements(1-9) = ";
- int req;
- cin >> req;
- NWCM obj(cap, req);
- obj.input();
- obj.computeTransportationCost();
- return 0;
- }
Add Comment
Please, Sign In to add comment