⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrayset.cpp

📁 I implement Dijkstra s Single Source Shortest Path, say SSP, algorithm for directed graphs using a s
💻 CPP
字号:
#include "ArraySet.h"

/*
 *Constructor, Initialize the array to store the medium state
 *during the Dijkstra.
 *int size:the max size of the array.
 */
ArraySet::ArraySet(int size)
{
	maxSize = size;
	initial();
}

void ArraySet::initial()
{
	currentSize = 0;
	dists = new int[maxSize];
	nodes = new int[maxSize];
}

/*
 *Destructor
 */
ArraySet::~ArraySet(void)
{
	delete [] dists;
	delete [] nodes;
}

/*
 *find the node with min dist and delete it from array.
 *return the ID of the node.
 */
int ArraySet::deleteMin()
{
	int i, min;
	if(currentSize<=0){
		cout<<"No Elements!"
		<<endl;
		return -1;
	}

	min = INT_MAX;
	for(i=0; i<currentSize; i++){
		if(dists[i]<min){
			min = dists[i];
		}
	}

	i--;

	if(currentSize>1){
		nodes[i] = nodes[currentSize-1];
		dists[i] = dists[currentSize-1];
	}
	currentSize = currentSize-1;
	return nodes[i];
}

/*
 *Decrease the dist of a node to a new value
 *int node: the node to decrease value.
 *int newDist: new value of the dist of the node
 */
int ArraySet::decreasDist(int node, int newDist)
{
	for(int i=0; i<currentSize; i++){
		if(nodes[i] == node && newDist<dists[i]){
				dists[i] = newDist;
				return 1;
			}
	}
	return 0;
}

/*
 *insert a new (node, dist) pair into the array
 *return the current size of the array.
 */
int ArraySet::insert(int node, int dist){
	for(int i=0; i<maxSize; i++){
		if(nodes[i]!=-1){
			nodes[currentSize] = node;
			dists[currentSize] = dist;
		}
	}
	currentSize++;
	return currentSize;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -