1111. Online Map (30)

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;
///错误,没有更新min_cnt; } }
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 贴上的代码有错误,在有向图中,计算路径长度要使用正确的,注意两个坐标的前后关系。

5 再次发现错误,在时间最短的算法中,发现更快的后,要清空原有的vector,再push_back()

6 在dfs_T中,没有更新min_cnt;

7 深度优先算法时,记录路径有问题。

有两种方法,一种是在拓展子路径时加入path,一种是在函数开头进入path,函数结尾pop。这样不会遗漏任何节点。

采用灵活的方法时谨记,路径是如何变化的。

在这道题中,两种思路发生了混淆。考虑本节点的计入,还是在搜索下一个节点时的计入。先根还是后根,却计入了

 

记录路径时,可以采用进入前加入路径,可以采用函数开头加入路径。

两种版本的都可以。要诀是深度搜索加入哪个节点就在路径中加入哪个节点。

在函数开头加入路径,需要在深度搜索终止时多写一条退出路径的语句。在寻找下一个节点时加入下一条路径,需要在进行dfs前加入路径,防止遗漏开始节点。

 

递归没有太多诀窍,只是谨慎写就好。

贪心算法注意维护必要的数据结构。找到合适的路径。需要维护一个记录最优的数据结构。

 

posted @ 2017-08-30 22:31  forjiuzhou  阅读(113)  评论(0编辑  收藏  举报