当前位置:网站首页>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;
}
边栏推荐
- Get screen width and height tool class
- Numpy 中的方法汇总
- MyCAT读写分离与MySQL主从同步
- 微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错
- 数据库迁移从PostgreSQL迁移到 MYSQL
- Why do you want to file? What are the advantages and disadvantages of website filing?
- Smart power plant: how to make use of easycvr to build a safe, stable, green and environment-friendly intelligent inspection platform
- 1528. 重新排列字符串
- 基于单片机开发的酒精浓度测试仪方案
- PHP代码加密的几种方案
猜你喜欢
随机推荐
Database migration from PostgreSQL to MySQL
2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
数据库迁移从PostgreSQL迁移到 MYSQL
数据中台:民生银行的数据中台实践方案
解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思
更改SSH端口号
1528. 重新排列字符串
Xiaohei ai4code code baseline nibble 1
[force deduction 10 days SQL introduction] Day3
Database to query the quantity of books lent in this month. If it is higher than 10, it will display "more than 10 books lent in this month". Otherwise, it will display "less than 10 books lent in thi
1704. 判断字符串的两半是否相似
Summary of methods in numpy
Video Fusion communication has become an inevitable trend of emergency command communication. How to realize it based on easyrtc?
常用表情符号
Prompt code when MySQL inserts Chinese data due to character set problems: 1366
Get screen width and height tool class
利用sonar做代码检查
什么是SRE?一文详解SRE运维体系
Liunx Mysql安装
为什么ping不通,而traceroute却可以通









