Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

NetworkFlow

//
//  main.cpp
//  NetworkFlow
//
//  Created by Himanshu Singh on 4/5/19.
//  Copyright © 2019 Himanshu Singh. All rights reserved.
//

#include <iostream>
#include <cstdlib>
#include <stack>
#include <queue>



using namespace std;
class Edge{
public: int tail,head,flow;
    public : Edge(int tail,int head,int flow){
        this->tail=tail;
        this->head=head;
        this->flow=flow;
    }
    
};




class Network{
    private :
    class Node{
        public :
        Edge *e;
        Node *next;
    public:
        Node(Edge *e,Node *next){
            this->e=e;
            this->next=next;
        }
    };
public:
    int v,e;
    Node** adjList;
public:
    Network(int v,int e){
        this->v=v;
        this->e=e;
        adjList=(Node**) malloc(( sizeof(Node*) * (v) ));
        //adjList=new Node[v-1];
        
        
    }
    ~Network(){
        adjList = (Node**) realloc(adjList, (v)*sizeof(Node));
        cout<<"\ndestruct";
    }
    
    void insert(Edge *edge){
        //  cout<<"size"<<sizeof(adjList);
        int tail=edge->tail;
        int head=edge->head;
        //        Node* nodeTail = new Node(edge,adjList[tail]);
        //        nodeTail->e=edge;
        //        nodeTail->next=adjList[tail];
        // adjList[head]=new Node(edge,adjList[head]);
        
        adjList[tail]=new Node(edge,adjList[tail]);
        
        //        Node* nodeHead = new Node();
        //        nodeHead->e=edge;
        //        nodeHead->next=adjList[head];
        //        adjList[head]=nodeHead;
        //cout<<" data "<<adjList[0].e.flow;
    }
    
    void print(){
        Node* n;int i=0;
        while(adjList[i]){
            cout<<"\n"<<adjList[i]->e->tail;
            cout<<" "<<adjList[i]->e->head;
            n=adjList[i]->next;
            while(n){
                cout<<" "<< n->e->head;
                n=n->next;
                
            }
            i++;
        }
    }
    
    void path(){
       
        int source=0,sink=v-1;
        stack<Node*> stack;
        Node *point=adjList[0];
        stack.push(point);
        while(point->e->head!=sink){
            point=adjList[point->e->head];
            stack.push(point);
        }
        cout<<"\n";
        while(!stack.empty()){
            point=stack.top();
            cout<<point->e->head<<" s ";
            stack.pop();
        }
        
//        while(stack.size()>0){
//            Node* n=stack.top();
//            int i=1; int min=n->e->flow;
//            while(sink==n->e->head && i<e){
//                stack.push(adjList[n->e->head]);
//                n=stack.top();
//                if(min>n->e->flow)
//                    min=n->e->flow;
//                i++;
//            }
//            queue<Node*> q;
//            while (stack.empty()) {
//                Node* p=stack.top();
//                cout<<p->e->head<<" ";
//                p->e->flow-=min;
//                q.push(p);
//                stack.pop();
//            }
//            while(q.empty()){
//                stack.push(q.front());
//                q.pop();
//            }
//            Node* k=stack.top();
//            k=k->next;
//            while(!k && !stack.empty()){
//                stack.pop();
//                k=stack.top();
//                k=k->next;
//            }
//            if(!stack.empty()){
//                stack.push(k);
//            }
//
//        }
    }
    
};

int main(int argc, const char * argv[]) {
    // insert code here...
    cout << "Hello, World!\n";
    int V,E;
    cin>>V>>E;
    Network* network=new Network(V,E);
    int head,tail,cap;
    for (int i=0; i<E ;i++){
        cin>>tail>>head>>cap;
        // Edge* edge= new Edge(tail,head,cap);
        network->insert(new Edge(tail,head,cap));
    }
    network->print();
    network->path();
    network->~Network();
    return 0;
}


Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.