当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5

数云发布2022美妆行业全域消费者数字化经营白皮书:全域增长破解营销难题

What is graph neural network? Figure what is the use of neural networks?

It is enough to read this article about ETL. Three minutes will let you understand what ETL is

开源之夏中选名单已公示,基础软件领域成为今年的热门申请

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

A tip to read on Medium for free

Telnet port login method with user name for liunx server
![打印出来的对象是[object object],解决方法](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
打印出来的对象是[object object],解决方法

Pymysql inserts data into MySQL and reports an error for no reason
随机推荐
2022春招面试总结
QT source code analysis -- QObject (2)
PHP代码加密的几种方案
【牛客】把字符串转换成整数
China chip Unicorn Corporation
2020中国全国各省市,三级联动数据,数据机构(数据来自国家统计局官网)
520. 检测大写字母
Floating error waiting for changelog lock
IDEA另起一行快捷键
数据中台:数据中台全栈技术架构解析,附带行业解决方案
À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL
Smart power plant: how to make use of easycvr to build a safe, stable, green and environment-friendly intelligent inspection platform
One article explains in detail | those things about growth
Ordering of MySQL composite index
rsync做文件备份
Why can ping fail while traceroute can
DataX User Guide
更改SSH端口号
什么是SRE?一文详解SRE运维体系
Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)