HDU 1688 Sightseeing
2015-11-02 15:33
357 查看
#include <stdio.h> #define MAX_CITIES 1000 #define MAX_ROADS 10000 #define INF 1000000 int numOfCities, numOfRoads; typedef struct Road{ int to; int len; int next; }Road; Road RoadArray[MAX_ROADS + 1]; int RoadNum; int head[MAX_CITIES + 1]; int start, final; int dist[MAX_CITIES + 1][2]; int count[MAX_CITIES + 1][2]; int visited[MAX_CITIES + 1][2]; int main(){ int testCases; scanf("%d", &testCases); while (testCases--){ scanf("%d%d", &numOfCities, &numOfRoads); int i; for (i = 1; i <= numOfCities; i++){ visited[i][0] = visited[i][1] = 0; dist[i][0] = dist[i][1] = INF; head[i] = 0; } RoadNum = 0; int from, to, len; for (i = 1; i <= numOfRoads; i++){ scanf("%d%d%d", &from, &to, &len); RoadNum++; RoadArray[RoadNum].to = to; RoadArray[RoadNum].len = len; RoadArray[RoadNum].next = head[from]; head[from] = RoadNum; } scanf("%d%d", &start, &final); dist[start][0] = 0; count[start][0] = 1; int doubleCities = numOfCities << 1; for (i = 1; i <= doubleCities; i++){ int distance = INF; int city = 0; int zeroOrOne; int j; for (j = 1; j <= numOfCities; j++){ if (visited[j][0] == 0 && dist[j][0] < distance){ distance = dist[j][0]; city = j; zeroOrOne = 0; } else if (visited[j][1] == 0 && dist[j][1] < distance){ distance = dist[j][1]; city = j; zeroOrOne = 1; } } if (city == 0){ break; } visited[city][zeroOrOne] = 1; for (j = head[city]; j != 0; j = RoadArray[j].next){ to = RoadArray[j].to; int newDist = distance + RoadArray[j].len; if (newDist < dist[to][0]){ dist[to][1] = dist[to][0]; count[to][1] = count[to][0]; dist[to][0] = newDist; count[to][0] = count[city][zeroOrOne]; } else if (newDist == dist[to][0]){ count[to][0] += count[city][zeroOrOne]; } else if (newDist < dist[to][1]){ dist[to][1] = newDist; count[to][1] = count[city][zeroOrOne]; } else if (newDist == dist[to][1]){ count[to][1] += count[city][zeroOrOne]; } } } int num = count[final][0]; if (dist[final][1] - 1 == dist[final][0]){ num += count[final][1]; } printf("%d\n", num); } return 0; }
相关文章推荐
- 详解图的应用(最小生成树、拓扑排序、关键路径、最短路径)
- python编写的最短路径算法
- 【算法】最短路径之A*搜索
- Dijkstra和floyd——求单源点最短路径
- 初学图论-Dijkstra单源最短路径算法基于优先级队列(Priority Queue)的实现
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- poj 1511 Invitation Cards