1030. Travel Plan (30)
时间限制400 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
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 3 0 1 1 20 1 3 2 30 0 3 4 10 0 2 2 20 2 3 1 20Sample Output0 2 3 3 40N(编号0-N-1的城市) M(高速路) S(起始点) D(目的地)
城市A 城市B 距离 过路费
……
从S到D距离最短,如果有多条,过路费最少;
这题和救援队的那题差不多,但是救援队的我是用DFS写的,然后久没有写dijkstra并且和prim混得有点乱,就写一下dijkstra的(仔细想想,感觉这个属于BFS一类的)(只对距离大于或等于零的有效)。
dijkstra首先起始点到自己距离0,过路费0,自己在距离自己最短的范围内;其他到自己的距离默认为无穷;
一:当前S看看【不在这个当前最短范围内(outset==true)城市index的到S的距离+S到起始点的距离是否更短||距离相等过路费更少】代替,并标记是通过S到index的;
二:在所有不在这个当前最短范围内(outset==true)城市index看看,哪个index到起始点的距离更短,获取这个为新S,并置入这个当前最短范围内outset=false;若已经到目的地,停止,否则一;
联动救援队网易:http://xujiayu317.blog.163.com/blog/static/25475209201561561155290/CSDN:http://blog.csdn.net/u014646950/article/details/46931667联动网易prim:http://xujiayu317.blog.163.com/blog/static/254752092014113118356/评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月01日 21:15 | 答案正确 | 30 | 1030 | C++ (g++ 4.7.2) | 3 | 3252 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 300 | 18/18 |
1 | 答案正确 | 1 | 312 | 6/6 |
2 | 答案正确 | 3 | 3252 | 6/6 |
#include<iostream> #define MAX 250000/*这一题题目中说每个数<=500*/ using namespace std; struct Highways { int distance; int cost; bool have; }; void StarC(Highways*Y, int Sat, int ENd, bool h) { while (Sat< ENd) { Y[Sat++].have = h; } } template<typename T> void StarClear(T*Y, int Sat, int ENd, T goal) { while (Sat< ENd) { Y[Sat++] = goal; } } void Dijkstra(Highways**road, int*TotalDistanceMin,int*TotalCostMin , int*_Set_elem_to_Now_roadMin, int S, int D, int N) { int index,min; bool * OutSet; OutSet = (bool*)malloc(N*sizeof(bool)); StarClear(OutSet, 0, N, true); OutSet[S] = false; TotalCostMin[S] = 0; TotalDistanceMin[S] = 0; while (S != D) { for (index = 0; index < N; index++) { if (OutSet[index] && road[S][index].have && (road[S][index].distance + TotalDistanceMin[S] < TotalDistanceMin[index]|| (road[S][index].distance + TotalDistanceMin[S] == TotalDistanceMin[index]&& TotalCostMin[index] >road[S][index].cost + TotalCostMin[S]))) { TotalDistanceMin[index] = road[S][index].distance + TotalDistanceMin[S]; TotalCostMin[index] = road[S][index].cost + TotalCostMin[S]; _Set_elem_to_Now_roadMin[index] = S; } } S = 0; min = MAX; for (index = 0; index < N; index++) { if (OutSet[index] && TotalDistanceMin[index] < min) { min = TotalDistanceMin[index]; S = index; } } OutSet[S] = false; } free(OutSet); } int main() { Highways**road; int*TotalDistanceMin; int*TotalCostMin; int*_Set_elem_to_Now_roadMin; int N, M, S, D,index,from,to,dd,cc; cin >> N >> M >> S >> D; TotalCostMin = (int*)malloc(N*sizeof(int)); StarClear(TotalCostMin, 0, N, MAX); TotalDistanceMin = (int*)malloc(N*sizeof(int)); StarClear(TotalDistanceMin, 0, N, MAX); _Set_elem_to_Now_roadMin = (int*)malloc(N*sizeof(int)); StarClear(_Set_elem_to_Now_roadMin, 0, N, -1); road = (Highways**)malloc(sizeof(Highways*)*N); for (index = 0; index < N; index++) { road[index] = (Highways*)malloc(N*sizeof(Highways)); StarC(road[index], 0, N, false); } for (index = 0; index < M; index++) { cin >> from >> to >> dd >> cc; road[to][from].cost=road[from][to].cost = cc; road[to][from].distance = road[from][to].distance = dd; road[to][from].have = road[from][to].have = true; } Dijkstra(road,TotalDistanceMin, TotalCostMin,_Set_elem_to_Now_roadMin, S, D, N); dd = TotalDistanceMin[D]; cc = TotalCostMin[D]; for (from = D,to=0; from != -1; from = _Set_elem_to_Now_roadMin[from]) { TotalDistanceMin[to++] = from; } while (to--) cout << TotalDistanceMin[to] << " "; cout << dd << " " << cc << endl; free(TotalCostMin); free(TotalDistanceMin ); free(_Set_elem_to_Now_roadMin); for (index = 0; index < N; index++) { free(road[index]); } free(road); system("pause"); return 0; }