【PAT 1030】Travel Plan 最短路径Dijkstra
来源:互联网 发布:数据库系统概论第5版11 编辑:程序博客网 时间:2024/06/12 01:40
1030. Travel Plan (30)
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input4 5 0 30 1 1 201 3 2 300 3 4 100 2 2 202 3 1 20Sample Output
0 2 3 3 40
分析:典型的最短路径算法,无向图,采用Dijkstra。每次修改minDis[]时,更新其prev(上一节点),为后面输出路线使用。
代码:
#include <iostream>#include <fstream>#include <algorithm>#include <vector>#include <cstring>using namespace std;//此代码使用前,需删除下面两行+后面的system("PAUSE")ifstream fin("in.txt");#define cin fin#define MAXNUM 500const int INF = 0x7fffffff;//struct HighWay//{//short distance;//short cost;//HighWay(){distance=0;cost=0;}//}hw[MAXNUM][MAXNUM];int dist[MAXNUM][MAXNUM],costA[MAXNUM][MAXNUM];struct LinkHW{int distance;int cost;int prev;//LinkHW(){distance=0;cost=0;prev=0;}}minDis[MAXNUM],mindis;bool visited[MAXNUM]={false};int n = 0;void Dijkastra(int begin,int end){int count = 1;int i;visited[begin]=true;int centre = begin;int tcen = begin;int tdis,tcost;while(count<n){mindis.distance = INF;mindis.cost = INF;for(i=0;i<n;i++){if(visited[i])continue;if(dist[centre][i] > 0)//存在通路{tdis = dist[centre][i] + minDis[centre].distance;tcost = costA[centre][i] + minDis[centre].cost;if(minDis[i].distance==0 || tdis<minDis[i].distance || (tdis==minDis[i].distance && tcost<minDis[i].cost))//此通路是否比目前路径更优{minDis[i].distance = tdis;minDis[i].cost = tcost;minDis[i].prev = centre;//记录此最短路径下的前一节点}}if(minDis[i].distance<mindis.distance || (minDis[i].distance==mindis.distance && minDis[i].cost<mindis.cost))//以centre中心的通路最优化处理,寻找下一个节点{mindis.distance = minDis[i].distance;mindis.cost = minDis[i].cost;tcen = i;}}if(tcen == end)break;//若走到目标点,则提前退出centre = tcen;visited[centre] = true;count++;}}int main(){ int m,s,d; cin>>n>>m>>s>>d; memset(dist,0,sizeof(int)*MAXNUM*MAXNUM); memset(costA,0,sizeof(int)*MAXNUM*MAXNUM); memset(minDis,0,sizeof(LinkHW)*MAXNUM); memset(visited,0,sizeof(bool)*MAXNUM); int c1,c2,dis,cost; int i; for(i=0;i<m;i++) {cin>>c1>>c2>>dis>>cost;dist[c1][c2] = dist[c2][c1] = dis;costA[c1][c2] = costA[c2][c1] = cost; } Dijkastra(s,d); int pre = d; vector<int> v; while(pre!=s) { v.push_back(pre); pre = minDis[pre].prev; } cout<<s; for(i=v.size()-1;i>=0;i--){cout<<' '<<v[i];} cout<<' '<<minDis[d].distance<<' '<<minDis[d].cost<<endl; system("PAUSE"); return 0;}
问题:
代码提交后,最后一个测试用例报“内存超限”错误。
不知为何?
发现问题的亲,请回复告知我一声,谢了。
- 【PAT 1030】Travel Plan 最短路径Dijkstra
- PAT (Advanced Level) 1030. Travel Plan (30) Dijkstra最短路径
- PAT 1030. Travel Plan (30)(Dijkstra,最短路径的同时计算最小奥cost)
- PAT 1030 Travel Plan(单源最短路径+优化Dijkstra)
- PAT 1030 Travel Plan dijkstra算法+双最短条件+保存最短路
- 1030. Travel Plan (30)寻找最短路径
- pat 1030 Travel Plan
- hdu2433 Travel(最短路径dijkstra,好题)
- 1030. Travel Plan (30)-PAT甲级真题(Dijkstra + DFS,输出路径,边权)
- PAT 1030 Travel Plan (30)
- PAT 1030 Travel Plan SPFA
- PAT--1030. Travel Plan(最短路)
- PAT 1030 最短路径
- 最短路径 Dijkstra
- 最短路径-Dijkstra
- Dijkstra 最短路径
- Dijkstra 最短路径。
- 最短路径dijkstra
- 安全联盟:网站公众安全与自身安全同等重要
- 开源收集
- cocos2dx之闪烁效果-CCBlink
- C++ 命名空间namespace
- 多科成绩单
- 【PAT 1030】Travel Plan 最短路径Dijkstra
- 算法
- 侧边栏的实现----网易新闻的侧边栏效果
- OpenGL 学习笔记一
- 【算法与数据结构】最大子序列和问题
- 关于sqlserver、access、mysql数据库性能的一个简单测试
- Socket网络通信方式/模型
- Base64加解密
- 【C语言学习笔记】字符串拼接的3种方法