POJ 1502 MPI Maelstrom
2009-10-30 14:17
357 查看
/*
纯手写的Dijkstra, 优先级队列也是手工写的
*/
纯手写的Dijkstra, 优先级队列也是手工写的
*/
#include <iostream> #include <cstring> #define MAX_N 1000 //最大结点数 using namespace std; //邻接表 struct head { int from, to, weight; head * next; head() { from = to = 0; next = NULL; } }*heads[MAX_N + 1]; int dist[MAX_N + 1]; //记录最短路径 int n; int heap[MAX_N + 1]; //邻接表 char temp[20]; bool inQueue[MAX_N + 1]; //标识当前节点是否在队列中 //插入边[from, to, weight] void insert(int from, int to, int weight) { head *newNode = new head(); newNode->from = from; newNode->to = to; newNode->weight = weight; newNode->next = heads[from]; heads[from] = newNode; } //结点p的左孩子 int left(int p) { return p * 2; } //结点p的右孩子 int right(int p) { return p * 2 + 1; } //结点p的父亲 int parent(int p) { return p / 2; } //取堆的大小 int heapSize() { return heap[0]; } //增加堆的大小change void changeHeapSize(int change) { heap[0] += change; } //比较两个堆元素对应结点的距离大小 int compare(int p1, int p2) { int dist1 = dist[heap[p1]]; int dist2 = dist[heap[p2]]; if(dist1 > dist2) return -1; else if(dist1 == dist2) return 0; else return 1; } //从结点n开始调整堆 void maxHeapify(int p) { int l = left(p), r = right(p), largest; if(l <= heapSize() && compare(l, p) > 0) largest = l; else largest = p; if(r <= heapSize() && compare(r, largest) > 0) largest = r; if(largest != p) { int temp = heap[p]; heap[p] = heap[largest]; heap[largest] = temp; maxHeapify(largest); } } //弹出堆中最大值 int popHeapMax() { if(heapSize() < 1) return -1; int maxVal = heap[1]; inQueue[maxVal] = false; heap[1] = heap[heapSize()]; changeHeapSize(-1); maxHeapify(1); return maxVal; } //得到val在堆中的下标 int getHeapPos(int val) { /*int curPos = 1; while(heap[curPos] != 0 && heap[curPos] != val) { if(val < heap[curPos]) curPos = left(curPos); else curPos = right(curPos); } return curPos;*/ for(int i = 1; i <= heapSize(); i++) if(heap[i] == val) return i; return -1; } //更新堆,从pos往上更新 void updateHeapPos(int pos) { while(pos > 1 && compare(pos, parent(pos)) > 0) { int temp = heap[pos]; heap[pos] = heap[parent(pos)]; heap[parent(pos)] = temp; pos = parent(pos); } } 插入堆元素 void heapInsert(int posVal) { changeHeapSize(1); heap[heapSize()] = posVal; updateHeapPos(heapSize()); } void init() { heap[0] = 0; //heap[1] = 1; inQueue[0] = true; for(int i = 1; i <= n; i++) { inQueue[i] = true; heads[i] = NULL; dist[i] = INT_MAX; heapInsert(i); } dist[1] = 0; } void dijkstra() { while(heapSize() != 0) { int maxNode = popHeapMax(); int curDist = dist[maxNode]; head * curHead = heads[maxNode]; while(curHead != NULL) { int nextNode = curHead->to; if(inQueue[nextNode]) { if(curDist + curHead->weight < dist[nextNode]) { dist[nextNode] = curDist + curHead->weight; updateHeapPos(getHeapPos(nextNode)); } } curHead = curHead->next; } } } int getNum() { int i, len = strlen(temp), total = 0; for(i = 0; i < len; i++) total = total * 10 + int(temp[i] - '0'); return total; } int main() { int i, j; scanf("%d", &n); init(); for(i = 2; i <= n; i++) { for(j = 1; j < i; j++) { scanf("%s", temp); if(temp[0] == 'x') continue; int weight = getNum(); insert(i, j, weight); insert(j, i, weight); } } dijkstra(); int maxVal = -1; for(i = 1; i <= n; i++) if(dist[i] > maxVal) maxVal = dist[i]; printf("%d/n", maxVal); return 0; }
相关文章推荐
- POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)
- POJ 1502 MPI Maelstrom Dijkstra
- POJ-1502 MPI Maelstrom (裸dijkstra算法)
- 最短路径___MPI Maelstrom(Poj 1502)
- poj_1502_MPI Maelstrom_spfa
- poj 1502 MPI Maelstrom(最短路)
- 【poj 1502】 MPI Maelstrom 【East Central North America 1996】
- POJ - 1502 MPI Maelstrom
- poj 1502 MPI Maelstrom dijkstra基础 入门 点型模板题
- poj 1502 MPI Maelstrom
- POJ 1502 MPI Maelstrom
- POJ 1502 - MPI Maelstrom(单源最短路)
- poj 1502 MPI Maelstrom
- (简单) POJ 1502 MPI Maelstrom,Dijkstra。
- POJ_1502_MPI Maelstrom_最短路径
- poj 1502 MPI Maelstrom
- POJ 1502 MPI Maelstrom 单源最短路 Floyd Dijkstra
- POJ 1502 MPI Maelstrom最短路问题
- POJ 1502 MPI Maelstrom (简单最短路,多种算法均可)
- POJ 1502 MPI Maelstrom