Tags » 336

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

...

În centimetri sorb uitarea, privire cu privire. Bine întocmit stă destinul gata să-l răstorn.
Pasul decisiv așteaptă pământ să se aștearnă.
Nu vreau. Partea asta o rup. 57 more words

Din Puțul Gândirii

gând fugar...

Într-o autogară pustie, alb tricou, geacă neagră de piele nu țin în frâu răcoarea ce îndepărtează omul de uman. Vid. Strigăt de copil din afara cercului meu. 312 more words

Din Puțul Gândirii

Marlin 336 for 10 years

Ten years ago today, I drove from Socorro, NM, to Los Lunas, NM, and spent some of my Christmas money. I purchased a Marlin 336, .30-30 Win., lever-action rifle for $294.97. 263 more words

Firearms

搜索隊回報 - 郵筒336

2014覆檢 (link) (1st record)

記 錄 員: 0053 泰利隊員

記 錄 日 期: 24/12/2014

地 點: 九龍 清水灣道/銀影路油站對面 (on Google Map)

QEII (ERII) 伊利沙伯二世 (1952-) 36 more words

Plans in motion for new elementary school in Holton

This embed is invalid

HOLTON (KSNT) – Plans for a new elementary school are in motion for USD 336 in Holton.

According to Superintendent Dennis Stones, a $21.5 million bond issue was passed by the community last year. 232 more words

News