예제 #1
0
파일: Graph.cpp 프로젝트: jk983294/Store
void Graph::dijkstra_shortest_path(int root, bool isTerminateAsFound){
	//<length nodeid > tricky nodes[i]为节点i在heap中的指针 
	MinBinaryHeap<int, int > q;  //优先队列	
	vector <BinaryHeapNode<int, int >*> nodes(n);   	
	int min,w;		//min 最小权值对应的节点id, w为与min相连的节点id
	for (int i = 0; i < n; ++i){
		distance[i] = POSTIVE_MAXINT;     parent[i] = -1;
		if ( i == root ) { distance[i] = 0; parent[i] = i; }
		nodes[i] = q.insert(distance[i],i);
	}
	while(!q.empty()){
		min = q.minimum()->data();
		if (isTerminateAsFound  && min==dest){	//必须等到dest出优先队列才能结束,因为以后都不可能对其松弛
			break;
		}	
		if (q.minimum()->key() == POSTIVE_MAXINT){	break;	}	//后面的都是其他森林,不需要遍历
		q.removeMinimum();  nodes[min] = NULL;
		//开始更新和u相连的边 relax
		list<Node>::iterator itr = linktable[min].begin();
		while(itr !=  linktable[min].end()){
			w = itr->id;
			if (nodes[w] != NULL)   //未被选中加入S
			{
				if (distance[min]+itr->weight < distance[w])    //relax
				{
					q.decreaseKey(nodes[w], distance[min]+itr->weight);
					distance[w] = distance[min]+itr->weight;
					parent[w] = min;
				}
			}
			++itr;
		}
	}
}
예제 #2
0
파일: Graph.cpp 프로젝트: jk983294/Store
void Graph::dijkstra_without_pass_other_mustpass_nodes(int root){
	//<length nodeid > tricky nodes[i]为节点i在heap中的指针 
	MinBinaryHeap<int, int > q;  //优先队列	
	vector <BinaryHeapNode<int, int >*> nodes(n);   	
	int min,w;		//min 最小权值对应的节点id, w为与min相连的节点id
	for (int i = 0; i < n; ++i){
		distance[i] = POSTIVE_MAXINT;     parent[i] = -1;
		if ( i == root ) { distance[i] = 0; parent[i] = i; }
		nodes[i] = q.insert(distance[i],i);
	}
	while(!q.empty()){
		min = q.minimum()->data();
//		if (min==end)	break;		//必须等到end出优先队列才能结束,因为以后都不可能对其松弛
		if (q.minimum()->key() == POSTIVE_MAXINT){	break;	}	//后面的都是其他森林,不需要遍历
		q.removeMinimum();  nodes[min] = NULL;
		//除root以外其他必过节点成为黑洞,没有出边,不帮助松弛
		if ((min != root )&&(allmustpass.find(min) != allmustpass.end()))	continue;	
		//开始更新和u相连的边 relax
		list<Node>::iterator itr = linktable[min].begin();
		while(itr !=  linktable[min].end()){
			w = itr->id;
			if (nodes[w] != NULL){   //未被选中加入S
				if (distance[min]+itr->weight < distance[w]){    //relax
					q.decreaseKey(nodes[w], distance[min]+itr->weight);
					distance[w] = distance[min]+itr->weight;
					parent[w] = min;
				}
			}
			++itr;
		}
	}
}