首页 > 其他分享 >1163 Dijkstra Sequence + 层序遍历 + 链式前向星

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

时间:2023-05-07 14:34:52浏览次数:68  
标签:arr Sequence int 层序 ++ Dijkstra include dis

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] = edge{b, 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;
}

标签:arr,Sequence,int,层序,++,Dijkstra,include,dis
From: https://www.cnblogs.com/jby3303/p/17379277.html

相关文章

  • CF750E - New Year and Old Subsequence
    题意:给一个字符串,每次询问它的一个区间,问最少删除多少个字符,使得区间没有子序列2016,但是有子序列2017。Mysolution首先考虑贪心,通过预处理的方式找到区间最后一个7,依次往前贪心的找到最靠后的一组2017。接下来,我们需要7的后面没有6,7前面的部分不能组合出2016。我们先......
  • PAT Advanced 1007. Maximum Subsequence Sum
    PATAdvanced1007.MaximumSubsequenceSum1.ProblemDescription:Givenasequenceof\(K\)integers{\(N_1,N_2,...,N_K\)}.Acontinuoussubsequenceisdefinedtobe{\(N_i,N_{i+1},...,N_j\)}where\(1≤i≤j≤K\).TheMaximumSubsequencei......
  • rgi main --input_sequence temp/out_pro.fa --output_file result/protein --inpu
    这是一个命令行命令,用于对temp/out_pro.fa文件进行抗菌基因分析。参数的含义如下:rgi:表示运行resistantgeneidentifier(rgi)程序。main:指定使用rgi的主要模式。--input_sequencetemp/out_pro.fa:指定输入序列文件名和路径。--output_fileresult/protein:指......
  • AtCoder Regular Contest 134 D Concatenate Subsequences
    洛谷传送门AtCoder传送门我一年前甚至不会做/qd发现\(a_{x_1}\)为\(k=\min\limits_{i=1}^na_i\)时最优。然后开始分类讨论:如果\(\min\limits_{a_i=k}a_{i+n}\lek\),答案为\((k,\min\limits_{a_i=k}a_{i+n})\)。这是因为如果再选一个\(k\)肯定不优。否则......
  • 1159 Structure of a Binary Tree + 根据前序和中序构建二叉树+ 层序遍历模板复习
    题目链接:https://pintia.cn/problem-sets/994805342720868352/exam/problems/1478635126488367104唉,今天的bug出在了下面这条语句。if(tree[root_key].left*tree[root_key].right<0)full_tree=false;我写成了full_tree=!(tree[root_key].left*tree[root_key].rig......
  • Java层序遍历打印二叉树(有Null值)
    publicclassSolution{publicstaticvoidmain(String[]args){Integer[]arr={3,9,20,null,null,15};//根据数组构造出二叉树TreeNodetreeNode=creatTreeNode(arr,0);//层序有Null值的打印二叉树printBin......
  • [ABC299F] Square Subsequence
    ProblemStatementYouaregivenastring$S$consistingoflowercaseEnglishletters.Printthenumberofnon-emptystrings$T$thatsatisfythefollowingcondition,modulo$998244353$.Theconcatenation$TT$oftwocopiesof$T$isasubsequenceof$S$(......
  • 8095: 小L的假期旅行 dijkstra
    描述 在即将到来的五一假期,小L向爸爸妈妈申请了T元的经费,开始计划起了自己五一的假期旅行。小L家在1号城市,尽管假期并不算长,小L还是希望在T元经费内选择去其他城市旅行。算上小L自己所在的1号城市,小L列举了N个城市,而这N个城市里有一些城市之间有双向连通的路径,并且每条路径也......
  • Tablespace 'innodb_system' Page [page id: space=0, page number=5] log sequence n
    场景:这几天在外面实习,老师的项目数据库崩了让我看,连着两条看到十一二点,哎。主要场景是mysql突然崩溃,发现重启mysqld服务无效,重启系统无效。查看/var/log/mysql.log日志,看到以下内容:Themanualpageathttp://dev.mysql.com/doc/mysql/en/crashing.htmlcontainsinfo......
  • CF1814E Chain Chips & CF750E New Year and Old Subsequence - 动态 dp -
    一句话概括动态dp:用来解决带修改/多次区间询问的dp问题。将转移写成矩阵的形式,然后利用线段树求解区间问题/单点修改1814E注意一条边要么选2要么选0次,而且第一条边一定是选了2次。如果有一条边没选,那么这条边两侧的边一定都选了。设\(f_i\)代表考虑到第\(i\)条边,......