当前位置:网站首页>4275. Dijkstra序列
4275. Dijkstra序列
2022-06-24 07:07:00 【Ray.C.L】

思路:直接跑一遍dijkstra看是否与序列中加入的顶点顺序相同
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010, INF = 0x3f3f3f3f;
int dis[N],g[N][N];
bool st[N];
int q[N];
int n,m;
bool dijkstra(){
memset(st, false, sizeof st);
memset(dis, INF, sizeof dis);
dis[q[0]] = 0;
for(int i = 0; i < n; i++){
int t=q[i];
for(int j = 1; j <= n; j++)
if(!st[j] && dis[j] < dis[t])
return false;
st[t] = true;
for(int j = 1; j <=n; j++)
dis[j] = min(dis[j],dis[t] + g[t][j]);
}
return true;
}
int main()
{
scanf("%d%d", &n, &m);
memset(g, INF, sizeof g);
while (m -- ){
int a,b,c;
scanf("%d%d%d", &a, &b, &c);
g[a][b]=g[b][a]=c;
}
int k;
scanf("%d", &k);
while( k-- ){
for(int i=0;i<n;i++)
scanf("%d", &q[i]);
if(dijkstra()) puts("Yes");
else puts("No");
}
return 0;
}
边栏推荐
猜你喜欢

Background management of uniapp hot update

2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。

關於ETL看這篇文章就够了,三分鐘讓你明白什麼是ETL

One article explains in detail | those things about growth

为什么ping不通,而traceroute却可以通

用VNC Viewer的方式远程连接无需显示屏的树莓派

【LeetCode】387. 字符串中的第一个唯一字符

liunx服务器 telnet 带用户名 端口登陆方法

Database migration from PostgreSQL to MySQL

2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)
随机推荐
RuntimeError: Missing dependencies:XXX
Jenkins自动化部署,连接不到所依赖的服务【已解决】
K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream
[team management] 25 tips for testing team performance management
Ordering of MySQL composite index
Earthly container image construction tool -- the road to dream
什么是SRE?一文详解SRE运维体系
Summary of methods in numpy
Numpy 中的方法汇总
深度学习与神经网络:最值得关注的6大趋势
快慢指针系列
Become an IEEE student member
Xtrabackup for data backup
A tip to read on Medium for free
RuntimeError: Missing dependencies:XXX
剑指 Offer 55 - I. 二叉树的深度-dfs法
Several schemes of PHP code encryption
There was an error checking the latest version of pip
Lombok use
QT source code analysis -- QObject (2)