标签:更改 数组 pos 坐标 exists 维护 否则 next name
Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (2 <= N <= 500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:
V1 V2 one-way length time
where V1 and V2 are the indices (from 0 to N-1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.
Finally a pair of source and destination is given.
Output Specification:
For each case, first print the shortest path from the source to the destination with distance D in the format:
Distance = D: source -> v1 -> ... -> destination
Then in the next line print the fastest path with total time T:
Time = T: source -> w1 -> ... -> destination
In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.
In case the shortest and the fastest paths are identical, print them in one line in the format:
Distance = D; Time = T: source -> u1 -> ... -> destination
Sample Input 1:
10 15 0 1 0 1 1 8 0 0 1 1 4 8 1 1 1 3 4 0 3 2 3 9 1 4 1 0 6 0 1 1 7 5 1 2 1 8 5 1 2 1 2 3 0 2 2 2 1 1 1 1 1 3 0 3 1 1 4 0 1 1 9 7 1 3 1 5 1 0 5 2 6 5 1 1 2 3 5
Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5 Time = 3: 3 -> 1 -> 5
Sample Input 2:
7 9 0 4 1 1 1 1 6 1 1 3 2 6 1 1 1 2 5 1 2 2 3 0 0 1 1 3 1 1 1 3 3 2 1 1 2 4 5 0 2 2 6 5 1 1 2 3 5
Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5
#include <iostream> #include <vector> #include <limits> #include <algorithm> using namespace std; int n, m,source,destination; int length[501][501]; int time[501][501]; int T[501], L[501]; int visit_t[501]; int visit_l[501]; int min_s = INT_MAX; int pre_L[501],weight[501]; vector<int> pre_T[501]; vector<int> path_short, path_fast; void shortest(int sour, int dest) { //int min= INT_MAX; for (int i = 0; i < n; i++) { int min = INT_MAX; int u = -1; for (int j = 0; j < n; j++) { if (visit_l[j] == 0 && L[j] < min) { u=j, min = L[j]; } } if (u == -1) break; visit_l[u] = 1; for (int j = 0; j < n; j++) { if (visit_l[j] == 0 && length[u][j]!=-1 &&length[u][j] + L[u] < L[j]) { pre_L[j] = u; L[j] = length[u][j]+L[u]; weight[j] = weight[u] + time[u][j]; } if (visit_l[j] == 0 && length[u][j] != -1 && length[u][j] + L[u] == L[j]) { if (weight[u] + time[u][j] < weight[j]) { pre_L[j] = u; weight[j] = weight[u] + time[u][j]; } } } } } void fastest() { for (int i = 0; i < n; i++) { int u = -1; int min = INT_MAX; for (int j = 0; j < n; j++) { if (visit_t[j] == 0 && T[j] < min) { min = T[j], u = j; } } if (u == -1) break; visit_t[u] = 1; for (int j = 0; j < n; j++) { if (visit_t[j] == 0 && time[u][j] != -1 && time[u][j] + T[u] < T[j]) { T[j] = time[u][j] + T[u]; pre_T[j].push_back(u); } } } } int result_L; void dfs_L(int x) { if (x == source) { return; } else { result_L += length[x][pre_L[x]]; dfs_L(pre_L[x]); path_short.push_back(x); } } vector<int> temp_path; int min_cnt = INT_MAX; int result_T; void dfs_T(int x,int cnt) { if (x == source) { if (cnt < min_cnt) { path_fast = temp_path; return; } } else { for (int i = 0; i < pre_T[x].size(); i++) { result_T += time[x][pre_T[x][i]]; temp_path.push_back(x); dfs_T(pre_T[x][i], cnt + 1); temp_path.pop_back(); result_T -= time[x][pre_T[x][i]]; } } } int main() { cin >> n >> m; fill(T, T + 501, INT_MAX); fill(L, L + 501, INT_MAX); fill(weight, weight + 501, INT_MAX); fill(length[0], length[0] + 501 * 501, -1); fill(time[0], time[0] + 501 * 501, -1); int sour, dest, oneway, Length, Time; for (int i = 0; i < m; i++) { cin >> sour >> dest >> oneway >> Length >> Time; length[sour][dest] = Length,time[sour][dest] = Time; if (!oneway) { length[dest][sour] = Length,time[dest][sour] = Time; } } cin >> source >> destination; T[source] = 0,L[source] = 0; shortest(source, destination); fastest(); dfs_L(destination); dfs_T(destination,0); for (int i = 0; i < path_short.size(); i++) { cout << path_short[i]; } for (auto i = path_fast.end() - 1; i != path_fast.begin(); i--) { cout << *i; } }
1 迪杰斯特拉算法
迪杰斯特拉算法是一个贪心算法,维护一个所有点到起始点距离的数组。分为两步,第一步加入目前的最短路径,认为它是可信的。其余的可能不正确。第二步使用新加入的点,用它来更新没有被访问的点到起始点的距离。
2 错误点,判断相等时使用了=
3 错误点,循环变量应该在循环内定义。否则容易忘记更改变量。
4 贴上的代码有错误,在有向图中,计算路径长度要使用正确的,注意两个坐标的前后关系。
标签:更改 数组 pos 坐标 exists 维护 否则 next name
原文地址:http://www.cnblogs.com/patforjiuzhou/p/7455690.html