• 1163 Dijkstra Sequence


    Dijkstra's algorithm is one of the very famous greedy algorithms.
    It is used for solving the single source shortest path problem which gives the shortest paths from one particular source vertex to all the other vertices of the given graph. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.

    In this algorithm, a set contains vertices included in shortest path tree is maintained. During each step, we find one vertex which is not yet included and has a minimum distance from the source, and collect it into the set. Hence step by step an ordered sequence of vertices, let's call it Dijkstra sequence, is generated by Dijkstra's algorithm.

    On the other hand, for a given graph, there could be more than one Dijkstra sequence. For example, both { 5, 1, 3, 4, 2 } and { 5, 3, 1, 2, 4 } are Dijkstra sequences for the graph, where 5 is the source. Your job is to check whether a given sequence is Dijkstra sequence or not.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains two positive integers Nv​ (≤103) and Ne​ (≤105), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to Nv​.

    Then Ne​ lines follow, each describes an edge by giving the indices of the vertices at the two ends, followed by a positive integer weight (≤100) of the edge. It is guaranteed that the given graph is connected.

    Finally the number of queries, K, is given as a positive integer no larger than 100, followed by K lines of sequences, each contains a permutationof the Nv​ vertices. It is assumed that the first vertex is the source for each sequence.

    All the inputs in a line are separated by a space.

    Output Specification:

    For each of the K sequences, print in a line Yes if it is a Dijkstra sequence, or No if not.

    Sample Input:

    1. 5 7
    2. 1 2 2
    3. 1 5 1
    4. 2 3 1
    5. 2 4 1
    6. 2 5 2
    7. 3 5 1
    8. 3 4 1
    9. 4
    10. 5 1 3 4 2
    11. 5 3 1 2 4
    12. 2 3 4 5 1
    13. 3 2 1 5 4

    Sample Output:

    1. Yes
    2. Yes
    3. Yes
    4. No

    题目大意:

    给定一个图 要你求这个图是否是Dijkstra序列

    注意: 

    在每一步中,我们找到一个尚未在集合内且与源顶点距离最小的顶点,并将其收于集合中

    AC代码:

    1. /*
    2. * @Author: Spare Lin
    3. * @Project: AcWing2022
    4. * @Date: 2022/6/28 18:44
    5. * @Description: 4275. Dijkstra序列 来源:PAT甲级真题1163
    6. */
    7. #include <iostream>
    8. #include <cstring>
    9. #include <algorithm>
    10. using namespace std;
    11. const int MAXN = 1e3 + 7;
    12. int n, m; //点和边
    13. int dis[MAXN], g[MAXN][MAXN];
    14. bool vis[MAXN];
    15. int q[MAXN];//输入的序列
    16. bool Dijkstra() {
    17. memset(dis, 0x3f, sizeof(dis));
    18. memset(vis, 0, sizeof(vis));
    19. dis[q[0]] = 0;
    20. for (int i = 0; i < n; ++i) {
    21. int t = q[i];
    22. for (int j = 1; j <= n; ++j) {
    23. //若当前点没被访问过且存在一个点比当前点更小
    24. if (!vis[j] && dis[j] < dis[t]) {
    25. return false;
    26. }
    27. }
    28. vis[t] = true;
    29. //更新当前近的点
    30. for (int j = 1; j <= n; ++j) {
    31. dis[j] = min(dis[j], dis[t] + g[t][j]);
    32. }
    33. }
    34. return true;
    35. }
    36. int main() {
    37. scanf("%d%d", &n, &m);
    38. memset(g, 0x3f, sizeof(g));
    39. while (m--) {
    40. int x, y, val;
    41. scanf_s("%d%d%d", &x, &y, &val);
    42. g[x][y] = g[y][x] = val;
    43. }
    44. int k;
    45. scanf("%d", &k);
    46. while (k--) {
    47. for (int i = 0; i < n; ++i) {
    48. scanf("%d", &q[i]);
    49. }
    50. printf(Dijkstra() ? "Yes\n" : "No\n");
    51. }
    52. return 0;
    53. }

     

  • 相关阅读:
    移远通信新一代LTE智能模组SC200E系列,以强大性能赋能多场景转型
    垃圾回收 - 分代垃圾回收
    版本管理的使用
    序言 工欲善其事必先利其器
    RabbitMQ初步到精通-第二章-RabbitMQ介绍
    72、Spring Data JPA 的 Specification 动态查询
    JavaScript垃圾回收机制
    Week 7 - Distributional Representations(分布表示)
    以go rabbitmq为例子--用最少的时间最好的掌握消息队列
    cmake如何将源文件按照指定的组织方式进行分组,在IDE例如vistual stdio或者xcode项目工程文件中展示和管理这些源文件
  • 原文地址:https://blog.csdn.net/weixin_55664293/article/details/125525249