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

时间: 2024-11-03 22:10:14

1111. Online Map (30)的相关文章

PAT (Advanced Level) 1111. Online Map (30)

预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n,m; int st,en

PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)

题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径.   对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的.   如果最短路程和最短时间路径一样,合并输出一次即可. 纯粹就是练习dijkstra,没什么难的. 第一次dijkstra求最短路程,记录下每个节点的路程和时间. 第二次dijkstra求最短时间,记录下每个节点的时间和经过的节点数. pre数组用来存储前驱节点,保存路径 #include <iostream>

1131 Subway Map (30 分)

1131 Subway Map (30 分) In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you are supposed to help people with your computer skills! Given th

1111 Online Map(迪杰斯特拉+DFS)

题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805358663417856 输出要求: 翻译结果: 在最短路径不唯一的情况下,输出最短路径中最快的一条,保证唯一. 如果最快的路径不是唯一的,则输出通过最少交叉口的路径,该交叉口保证是唯一的.如果最短路径和最快路径相同,请按以下格式将它们打印在一行中: 题目思路很简答,使用两个不同度量(距离.时间)的 迪杰斯特拉+DFS 求解即可. PS:我英语好菜,我翻译错了,

PAT-1111 Online Map (30分) 最短路+dfs

明天就要考PAT,为了应付期末已经好久没有刷题了啊啊啊啊,今天开了一道最短路,状态不是很好 1.没有读清题目要求,或者说没有读完题目,明天一定要注意 2.vis初始化的时候从1初始化到n,应该从0开始,以后初始化就从0到n即可 题目大意:给一张地图,两个结点中既有距离也有时间,有的单行有的双向,要求根据地图推荐两条路线:一条是最快到达路线,一条是最短距离的路线. 第一行给出两个整数N和M,表示地图中地点的个数和路径的条数.接下来的M行每一行给出:道路结点编号V1 道路结点编号V2 是否单行线 道

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

【spring set注入 注入集合】 使用set注入的方式注入List集合和Map集合/将一个bean注入另一个Bean

Dao层代码: 1 package com.it.dao; 2 3 public interface SayHell { 4 public void sayHello(); 5 } Dao的Impl实现层: 1 package com.it.dao.impl; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import com.it.dao.SayHell; 7 8 /** 9 * Spring如何知道setter方法?如何将值注入

Map按照条件排序输出

需求: /** * Map进行多条件排序输出* 水果具有好吃不好吃难吃属性.* 入口Map * 首先按照好吃不好吃难吃排序* 然后按照水果的标志Id排序* 出口Map * * 2016年8月14日*/ 1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Comparator; 4 import java.util.HashMap; 5 import java.util.Iterator;

Java Map hashCode深究

[Java心得总结七]Java容器下——Map 在自己总结的这篇文章中有提到hashCode,但是没有细究,今天细究整理一下hashCode相关问题 1.hashCode与equals 首先我们都知道hashCode()和equals()函数是java基类Object的一部分,我查阅了java7文档,其中对于两者的描述如下: 解读这里对hashCode的描述,不难发现: 首先hashCode必须是一个整数,即Integer类型的 其次满足一致性,即在程序的同一次执行无论调用该函数多少次都返回相同