URAL 1205 By the Underground or by Foot (建图 + Dijkstra + 堆优化)
来源:互联网 发布:白夜水彩淘宝 编辑:程序博客网 时间:2024/06/05 17:50
#include <stdio.h>#include <math.h>#define INF (1 << 30)double footSpeed, undergroundSpeed;int numOfStations;int start, end;typedef struct Coordinates{double x;double y;}Cooridnates;Coordinates CoordinatesArray[300];double timeArray[300][300];double minTimeArray[300];int stack[300];int top;int pre[300];int heapSize;typedef struct Node{double minTime;int location;}Node;Node heap[300];double getDistance(Coordinates one, Coordinates another){double deltaX = one.x - another.x;double deltaY = one.y - another.y;return sqrt(deltaX * deltaX + deltaY * deltaY);}void swap(Node *a, Node *b){Node c = *a;*a = *b;*b = c;}void heapDecreaseKey(int NodeIndex,double minTime){heap[NodeIndex].minTime = minTime;while (NodeIndex > 1 && heap[NodeIndex].minTime < heap[ NodeIndex >> 1 ].minTime){swap(&heap[NodeIndex], &heap[ NodeIndex >> 1 ]);NodeIndex = NodeIndex >> 1;}}void minHeapify(int parent){int least = parent;int left = parent << 1;if (left <= heapSize && heap[left].minTime < heap[least].minTime)least = left;int right = (parent << 1) + 1;if (right <= heapSize && heap[right].minTime < heap[least].minTime)least = right;if (least != parent){swap(&heap[parent], &heap[least]);minHeapify(least);}}void buildMinHeap(){int to;for (to = heapSize >> 1; to >= 1; to--)minHeapify(to);}double getMinTimeByDijkstra(){heapSize = 0;int to;for (to = 1; to <= end; to++){if (to != start){heapSize++;heap[heapSize].minTime = timeArray[start][to];heap[heapSize].location = to;}}buildMinHeap();int numOfMinTimesFound;for (numOfMinTimesFound = 2; numOfMinTimesFound <= end; numOfMinTimesFound){double minTime = heap[1].minTime;int location = heap[1].location;if (location == end)return minTime;swap(&heap[1], &heap[heapSize]);heapSize--;int NodeIndex;for (NodeIndex = 1; NodeIndex <= heapSize; NodeIndex++){Node tempNode = heap[NodeIndex];if (minTime + timeArray[location][tempNode.location] < tempNode.minTime){heapDecreaseKey(NodeIndex, minTime + timeArray[location][tempNode.location]);pre[tempNode.location] = location;}}minHeapify(1);}}int main(){scanf("%lf%lf", &footSpeed, &undergroundSpeed);scanf("%d", &numOfStations);int station;for (station = 1; station <= numOfStations; station++){Coordinates tempCoordinates;scanf("%lf%lf", &tempCoordinates.x, &tempCoordinates.y);CoordinatesArray[station] = tempCoordinates;}int from, to;for (from = 1; from <= numOfStations; from++)for (to = from + 1; to <= numOfStations; to++)//注意:默认可以从一个车站步行到另一个车站timeArray[from][to] = timeArray[to][from] = getDistance(CoordinatesArray[from], CoordinatesArray[to]) / footSpeed;while (scanf("%d%d", &from, &to) != EOF && (from != 0 && to != 0) )timeArray[from][to] = timeArray[to][from] = getDistance(CoordinatesArray[from], CoordinatesArray[to]) / undergroundSpeed;start = numOfStations + 1;end = numOfStations + 2;scanf("%lf%lf", &CoordinatesArray[start].x, &CoordinatesArray[start].y);scanf("%lf%lf", &CoordinatesArray[end].x, &CoordinatesArray[end].y);int location;for (location = 1; location <= end; location++){timeArray[start][location] = timeArray[location][start] = getDistance(CoordinatesArray[start], CoordinatesArray[location]) / footSpeed;timeArray[end][location] = timeArray[location][end] = getDistance(CoordinatesArray[end], CoordinatesArray[location]) / footSpeed;}//注意输出精度printf("%.10lf\n", getMinTimeByDijkstra());int preLocation = pre[end];while (preLocation != 0){stack[++top] = preLocation;preLocation = pre[preLocation];}printf("%d", top);while (top > 0)printf(" %d", stack[top--]);printf("\n");return 0;}
0 0
- URAL 1205 By the Underground or by Foot (建图 + Dijkstra + 堆优化)
- URAL 1205 By the Underground or by Foot (建图 + Dijkstra迪杰斯特拉算法)
- ural 1205. By the Underground or by Foot? Dijkstra
- URAL 1205 By the Underground or by Foot?
- By the Underground or by Foot?
- The Humble Programmer by Edsger W. Dijkstra
- Dijkstra算法之优先队列优化版本 By ACReaper Dijkstra
- Dijkstra算法之优先队列优化版本 By ACReaper
- Radix Heap ---Dijkstra算法的优化 BY Gremount
- 对dijkstra算法的常数优化-by azui
- By value? Or by reference?
- The behavior of App killed or restored by Android System or by users
- jaxb:extensionBindingPrefixes by disabling the strict mode or by using the extension
- [BZOJ4152][AMPPZ2014]The Captain(堆优化dijkstra)
- BZOJ 4152 The Captain (Dijkstra 堆优化)
- 堆优化的Dijkstra
- DIJKSTRA堆优化
- Dijkstra + 堆优化
- #ifdef __cplusplus 有什么作用
- SparkSQL: no typetag available for xxxx问题的解决办法
- UIDatePicker的用法
- JavaScript权威指南_163_第17章_事件处理_17.2-注册事件处理程序-设置HTML标签属性为事件处理程序
- ios取消屏幕关闭
- URAL 1205 By the Underground or by Foot (建图 + Dijkstra + 堆优化)
- Golang的安装
- LeetCode Number of 1 Bits
- 使用CryptoAPI获取证书扩展属性之四:“CRL分发点”和“颁发机构信息访问”
- CentOS7升级内核到3.12.48
- VS2008基于对话框的MFC上位机串口通信(C++实现)简单例程
- 用xmanager登陆Linux图形界面
- Advanced English Writing
- 状态机思路在程序设计中的应用