九度OJ108 HDOJ3790:最短路径问题 迪杰斯特拉算法

来源:互联网 发布:阿里云邮箱客服400 编辑:程序博客网 时间:2024/05/01 17:34

    浙大的这道考研上机题明摆着要使用最短路径算法,考虑时间复杂度,我用了迪杰斯特拉算法。

    这道题比书上的样例算法稍微有些复杂,不仅要求距离最短,而且要在同时有多个距离最短的情况下,要求费用也最短。这个处理的方法就是更新最短路径时考虑两种情况。HDOJ的测试数据里有重边,因此输入时要将保存最短的重边。

    我的AC代码:

#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>using namespace std;const int Max = 1000 + 10;int dist[Max][Max];int cost[Max][Max];int minDist[Max];int minCost[Max];const int Inf = 0x0fffffff;int n, m;void Dijkstra(int source){bool final[Max] = {false};for(int i=1; i<=n; ++i) minDist[i] = dist[source][i], minCost[i] = cost[source][i], final[i] = false;final[source] = true;for(int i=2; i<=n; ++i){int pos = 1, md = Inf, mc = Inf;for(int j=1; j<=n; ++j)if(!final[j] && ((minDist[j] < md) || (minDist[j] == md && minCost[j] < mc)))md = minDist[j], pos = j, mc = minCost[j];if(md == Inf) break;final[pos] = true;for(int j=1; j<=n; ++j)if(!final[j] && ((minDist[pos] + dist[pos][j] < minDist[j]) || (minDist[pos] + dist[pos][j] == minDist[j] && minCost[pos] + cost[pos][j] < minCost[j])))minDist[j] = minDist[pos] + dist[pos][j], minCost[j] = minCost[pos] + cost[pos][j];}}int main(){int a, b, c, d;int head, tail;while(scanf("%d %d", &n, &m) && !(!n && !m)){for(int i(0); i<Max; ++i)for(int j(0); j<Max; ++j) dist[i][j] = cost[i][j] = Inf;for(int i(0); i<m; ++i){scanf("%d %d %d %d", &a, &b, &c, &d);//判断重边if(dist[a][b] > c || (dist[a][b] == c && cost[a][b] > d)){dist[a][b] = dist[b][a] = c;cost[a][b] = cost[b][a] = d;}}scanf("%d %d", &head, &tail);Dijkstra(head);printf("%d %d\n", minDist[tail], minCost[tail]);}system("pause");return 0;}




原创粉丝点击