1163 Dijkstra Sequence + 层序遍历 + 链式前向星

Posted 江柏英的技术博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1163 Dijkstra Sequence + 层序遍历 + 链式前向星相关的知识,希望对你有一定的参考价值。

PAT题目链接:https://pintia.cn/problem-sets/994805342720868352/exam/problems/1478635670373253120

这题踩了太多坑,本来没什么内容,硬是断断续续查了三天的bug:

第一天: 循环的时候内部判断逻辑不要写在for循环里,否则本该continue的逻辑,硬生生变成了break。我真是脑袋瓜秀逗了才会这么写代码。
第二天: 混淆了dijkstra和一般最短路算法的思想,导致迟迟得不到想要的结果。最后参考了层序遍历的思想,改掉了这个bug。
第三天: 题目说Ne不超过105,但是开边的时候数组要开205, 因为是双向的。最后那个段错误又卡了我好久。

#include<iostream>
#include<queue>
#include<vector>
#include<set>
#include<cstring>
using namespace std;
int n, m, k;
struct edge 
    int to, next, c;
 g[200005];
int head[1005], dis[1005], vis[1005], cnt;
inline void add(int a, int b, int c) 
    g[++cnt] = edgeb, head[a], c;
    head[a] = cnt;

bool spfa(vector<int> &arr) 
    memset(vis, 0, sizeof(vis));
    memset(dis, 0x3f3f3f3f, sizeof(dis));
    queue<int> q;
    int idx = 0; 
    q.push(arr[idx++]);
    dis[arr[0]] = 0;
    vis[arr[0]] = 1;
    while (q.size()) 
        // 灵感来源——层序遍历
        int size = q.size();
        // 上一波出队, 更新状态
        for (int j = 0; j < size; j++) 
            int ind = q.front(); q.pop();
            for (int i = head[ind]; i; i = g[i].next) 
                int to = g[i].to, c = g[i].c;
                if (dis[ind] + c < dis[to]) dis[to] = dis[ind] + c;
            
        
        // 下一波入队
        int min_dis = 0x3f3f3f3f;
        set<int> v;
        for (int to = 1; to <= n; to++)  
            if (to == arr[0]) continue;
            if (!vis[to]) 
                if (dis[to] < min_dis) 
                    min_dis = dis[to];
                    v.clear();
                    v.insert(to);
                 else if (dis[to] == min_dis) 
                    v.insert(to);
                
            
        
        while (v.size()) 
            if (v.find(arr[idx]) != v.end()) 
                q.push(arr[idx]); vis[arr[idx]] = 1;
                v.erase(arr[idx]);
                idx++;
             else 
                return false;
            
        
    
    return idx == n;

int main() 
    cin >> n >> m;
    for (int i = 0; i < m; i++) 
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
        add(b, a, c);
    
    cin >> k;
    while (k--) 
        vector<int> arr(n);
        for (int i = 0; i < n; i++) cin >> arr[i];
        if (spfa(arr)) cout << "Yes\\n";
        else cout << "No\\n";
    
    return 0;

7-4 Dijkstra Sequence (30分)

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?N?v???(≤10?^3??) and?N?e???(≤10^?5??), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to?N?v??.

Then?N?e???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?N?v???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:

5 7
1 2 2
1 5 1
2 3 1
2 4 1
2 5 2
3 5 1
3 4 1
4
5 1 3 4 2
5 3 1 2 4
2 3 4 5 1
3 2 1 5 4

Sample Output:

Yes
Yes
Yes
No


以上是关于1163 Dijkstra Sequence + 层序遍历 + 链式前向星的主要内容,如果未能解决你的问题,请参考以下文章

POJ1163

POJ1163(简单的DP)

poj 1163 The Triangle

poj-1163-The Triangle

ZOJ-1163-The Staircases

CF-1163