Tags » Node

UVa Problem Solution: 336 - A Node Too Far

Link: http://uva.onlinejudge.org/external/3/336.pdf
#include <stdio.h>
#include <stdlib.h>

#include <stdbool.h>
#define TOTAL_NODE 100
#define INVALID 2147483647


typedef struct mNode{
    int iNodeName;
    int iEdgeWeight;
    bool rVisited;
    int rCost;
    struct mNode *iNextNode;
    struct mNode *rPreviousNode;
    struct mNode *bBaseAddress;

}Node;

Node *AllNodeList;
void PutInNodeList(int FromNode, int ToNode, int EdgeWeight);
Node * GetNodeBaseAddress(int NodeName);
int GetNodeDegree(int NodeName);
int NodeIndexHead = 0;
void SetupNode(Node *mNodeAddress, int mNodeName, int mEdgeWeight, int mCost, Node *mNodeBaseAddress);
int TotalNodeInGraph(void);
int TotalEdgeInGraph(void);
void UpdateNodeValues(Node *mNodeAddress, int mEdgeWeight, Node *mNextNode, int mCost, Node *mPreviousNode, int mVisited);
void ClearBFSResult();
void ClearGRAPH();

void Enqueue(Node *,Node *);
bool Dequeue(Node **,Node **);

void BFS(int startNode,int startCost);

int main()
{

    freopen("input.txt","r",stdin);
    int caseCount,i,f,s,tmp,caseNumber=0;
    scanf("%d",&caseCount);
    while(caseCount!=0){
        for(i=0;i<caseCount;i++){
            scanf("%d%d",&f,&s);
            PutInNodeList(f,s,0);
            PutInNodeList(s,f,0);
        }
        scanf("%d%d",&f,&s);
        while(s|f){
            caseNumber++;
            tmp=0;
            BFS(f,s);
            for(i=0;i<NodeIndexHead;i++){
                if(!(AllNodeList[i]->rVisited)) tmp++;
            }
            printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n",caseNumber,tmp,f,s);
            scanf("%d%d",&f,&s);
            ClearBFSResult();
        }
        scanf("%d",&caseCount);
        ClearGRAPH();
    }


    return 0;
}


void PutInNodeList(int FromNode, int ToNode, int EdgeWeight){
    Node *tempNode = GetNodeBaseAddress(FromNode);

    if(tempNode != NULL){
        while(tempNode->iNextNode != NULL){
            tempNode = tempNode->iNextNode;
        }
        Node *tmp = (Node*) malloc(1 * sizeof(Node));

        Node *baseAddress = GetNodeBaseAddress(ToNode);
        if(baseAddress == NULL){
            baseAddress = (Node*) malloc(1 * sizeof(Node));
            SetupNode(baseAddress,ToNode,INVALID,INVALID,baseAddress);
            AllNodeList = baseAddress;
        }

        SetupNode(tmp,ToNode,EdgeWeight,INVALID,baseAddress);
        tempNode->iNextNode = tmp;

    }else{
        Node *tmp1 = (Node*) malloc(1 * sizeof(Node));
        SetupNode(tmp1,FromNode,INVALID,INVALID,tmp1);
        AllNodeList = tmp1;
        int tmpNodeHead = NodeIndexHead-1;

        Node *baseAddress = GetNodeBaseAddress(ToNode);
        if(baseAddress == NULL){
            baseAddress = (Node*) malloc(1 * sizeof(Node));
            SetupNode(baseAddress,ToNode,INVALID,INVALID,baseAddress);
            AllNodeList = baseAddress;
        }

        Node *tmp2 = (Node*) malloc(1 * sizeof(Node));
        SetupNode(tmp2,ToNode,EdgeWeight,INVALID,baseAddress);
        AllNodeList -> iNextNode = tmp2;
    }
}

void SetupNode(Node *mNodeAddress, int mNodeName, int mEdgeWeight, int mCost, Node *mNodeBaseAddress){
    mNodeAddress->iEdgeWeight = mEdgeWeight;
    mNodeAddress->iNextNode = NULL;
    mNodeAddress->iNodeName = mNodeName;
    mNodeAddress->rCost = mCost;
    mNodeAddress->rPreviousNode = NULL;
    mNodeAddress->rVisited = false;
    mNodeAddress->bBaseAddress = mNodeBaseAddress;
}

void UpdateNodeValues(Node *mNodeAddress,
                      int mEdgeWeight,
                      Node *mNextNode,
                      int mCost,
                      Node *mPreviousNode,
                      int mVisited){
    if(mEdgeWeight!=INVALID){
        mNodeAddress->iEdgeWeight=mEdgeWeight;
    }

    if(mNextNode != NULL){
        mNodeAddress->iNextNode = mNextNode;
    }

    if(mCost != INVALID){
        mNodeAddress->rCost = mCost;
    }

    if(mPreviousNode!=NULL){
        mNodeAddress->rPreviousNode=mPreviousNode;
    }
    if(mVisited != 0){
        if(mVisited>0){
            mNodeAddress->rVisited = true;
        }else{
            mNodeAddress->rVisited = false;
        }
    }

}


Node * GetNodeBaseAddress(int NodeName){
    int i;
    for(i=0;i<NodeIndexHead;i++){
        if(AllNodeList[i]->iNodeName == NodeName){
            return AllNodeList[i];
        }
    }
    return NULL;
}

int GetNodeDegree(int NodeName){
    Node* tmp = GetNodeBaseAddress(NodeName);
    int i=0;
    if(tmp!=NULL){
        tmp = tmp->iNextNode;
    }
    while(tmp!=NULL){
        i++;
        tmp=tmp->iNextNode;
    }
    return i;
}

int TotalNodeInGraph(){
    return NodeIndexHead;
}

int TotalEdgeInGraph(){
    return INVALID;
}


typedef struct smQueue{
    Node *smX;
    Node *smY;
    struct smQueue *next;
}smNode;


smNode *smFirstNode, *smLastNode, *tempNode;
bool firstTime = true;

void Enqueue(Node *valX, Node *valY){
    smNode *nde = malloc(1 * sizeof(smNode));
    if(firstTime){
        firstTime = false;
        smFirstNode = nde;
        smLastNode = nde;
        smLastNode->smX = valX;
        smLastNode->smY = valY;
        smLastNode->next = NULL;
        return;
    }
    smLastNode->next = nde;
    smLastNode = nde;
    smLastNode->smX = valX;
    smLastNode->smY = valY;
    smLastNode->next = NULL;

}


bool Dequeue(Node **smValueX, Node **smValueY){

    if(smFirstNode!=NULL){
        *smValueX = smFirstNode->smX;
        *smValueY = smFirstNode->smY;
        tempNode = smFirstNode->next;
        free(smFirstNode);
        smFirstNode = tempNode;
        if(smFirstNode==NULL){
            firstTime = true;
        }
        return true;
    }
    return false;
}


void BFS(int startNode,int startCost){

    Node *bfsTmpNode, *bfsTmpParentNode, *bfsGarbageNode,*bfsListTraversal;

    bfsTmpNode = GetNodeBaseAddress(startNode);
    if(bfsTmpNode==NULL) return;
    bfsTmpNode->rVisited = true;
    bfsTmpNode->rCost = startCost;
    bfsTmpNode->rPreviousNode = NULL;
    Enqueue(bfsTmpNode,NULL);

    while(Dequeue(&bfsTmpParentNode,&bfsGarbageNode)){
        bfsListTraversal = bfsTmpParentNode;

        while(1){
            bfsListTraversal = bfsListTraversal->iNextNode;
            if(bfsListTraversal==NULL) break;

            if(bfsTmpParentNode->rCost == 0)continue;

            bfsTmpNode = bfsListTraversal->bBaseAddress;
            if(bfsTmpNode->rVisited) continue;
            bfsTmpNode->rVisited = true;
            bfsTmpNode->rCost = bfsTmpParentNode->rCost - 1;
            bfsTmpNode->rPreviousNode = bfsTmpParentNode;
            Enqueue(bfsTmpNode,NULL);
        }
    }
}


void ClearBFSResult(){
    int i;
    for(i=0;i<NodeIndexHead;i++){
        AllNodeList[i]->rCost = INVALID;
        AllNodeList[i]->rPreviousNode = NULL;
        AllNodeList[i]->rVisited = false;
    }

}

void ClearGRAPH(){
    int i;
    for(i=0;i<NodeIndexHead;i++){
        AllNodeList[i]=NULL;
    }
    NodeIndexHead = 0;
} 6 more words
UVa

Azure App Services: Restart your node-WebJobs during GitDeploy

With Azure App Services (aka. Azure WebSites), the Microsoft Azure cloud offers a great, highly scalable and simple way to host cloud and SaaS services. Besides ASP.NET, several other platforms and languages are supported, e.g. 333 more words

Azure

Setting up your environment A to Z

Introduction

In the previous series of articles, I explained about the different pieces required to get started with web development. This article will walk you through all the steps to go  from a blank machine to a full-fledged web development box. 951 more words

Web Development

Developer Catchup: Rust 1.0 and Node reunification

Rust wakes up

First up, Rust has reached version 1.0, though this is an announcement that was hardly unexpected. It has a lot to live up to given the… 378 more words

Catchup

Recording a test ride

To test the GPS receiver and to get a first feel for handling the messages sent by it, we did a short test ride on bus line 63 in Dresden, Germany. 545 more words

Measurement Device

Northlane Announce Summer 2015 Tour With Like Moths To Flames, In Hearts Wake & Oceans Ate Alaska

Northlane have announced a summer tour to promote their new album, Node, which will be released in July. The band will also be joined by Like Moths To Flames, In Hearts Wake and Oceans Ate Alaska for this run. 91 more words

News