当前位置: 首页 > news >正文

【Luogu】每日一题——Day21. P3556 [POI 2013] MOR-Tales of seafaring (图论)

P3556 [POI 2013] MOR-Tales of seafaring - 洛谷

题目:

思路:

有点分层图的感觉

由于题目不是要求简单路径,因此我们可以反复走来刷路径,那么一个思路就是求最短路

如果对于询问 s->t 是否存在 d,那么就有两种情况,一个是 d < dis,即小于最短路,那么此时肯定无解,否则一定有解,具体的,如果 d 是奇数,那么我们可以考虑一个走了奇数步的最短路,然后反复刷步数得到,而偶数也是一样的考虑偶数

所以维护奇数偶数的最短路即可,spfa秒了

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include <iomanip>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <utility>
#include <array>
#include <tuple>
using namespace std;#define yes cout << "YES" << endl
#define no cout << "NO" << endl
vector<vector<short>> g(5005);
int dis[5005][2];
bool inq[5005];
int n, m, k;
bool ans[1000005];
bool hasfa[5005];struct MyStruct
{int b, d, id;
};
vector<vector<MyStruct>> ask(5005);void spfa(int fa)
{memset(dis, 0x3f, sizeof dis);memset(inq, 0, sizeof inq);queue<int> q;dis[fa][0] = 0;inq[fa] = 1;q.push(fa);while (!q.empty()){auto t = q.front();q.pop();inq[t] = 0;for (auto & son : g[t]){if (dis[son][1] > dis[t][0] + 1){dis[son][1] = dis[t][0] + 1;if (!inq[son]){inq[son] = 1;q.push(son);}}if (dis[son][0] > dis[t][1] + 1){dis[son][0] = dis[t][1] + 1;if (!inq[son]){inq[son] = 1;q.push(son);}}}}
}void solve()
{cin >> n >> m >> k;for (int i = 0; i < m; i++){int u, v;cin >> u >> v;g[u].emplace_back(v);g[v].emplace_back(u);hasfa[u] = hasfa[v] = 1;}for (int i = 0; i < k; i++){int a, b, d;cin >> a >> b >> d;ask[a].push_back({ b, d, i });}for (int i = 1; i <= n; i++){if (!hasfa[i] || ask[i].empty()){continue;}spfa(i);for (auto & query : ask[i]){ans[query.id] = (query.d >= dis[query.b][query.d & 1]);}}for (int i = 0; i < k; i++){cout << (ans[i] ? "TAK" : "NIE") << endl;}
}
signed main()
{cin.tie(0)->sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);int t = 1;//cin >> t;while (t--){solve();}return 0;
}

http://www.lryc.cn/news/623668.html

相关文章:

  • 裸机框架:按键模组
  • 深度学习之优化器
  • 概率论基础教程第4章 随机变量(一)
  • 《Cocos游戏开发入门一本通》第四章
  • 李宏毅NLP-11-语音合成
  • 神经网络中的梯度概念
  • 显式编程(Explicit Programming)
  • c++--文件头注释/doxygen
  • 系统学习算法 专题十七 栈
  • C++ 特殊类设计与单例模式解析
  • 编译器生成的合成访问方法(Synthetic Accessor Method)
  • Python训练营打卡Day35-复习日
  • Spring Framework :IoC 容器的原理与实践
  • 库制作与原理(下)
  • HAL-EXTI配置
  • Python异常、模块与包(五分钟小白从入门)
  • STL 容器
  • 【Linux网络编程】NAT、代理服务、内网穿透
  • Windows 10共享打印机操作指南
  • 第七十八章:AI的“智能美食家”:输出图像风格偏移的定位方法——从“滤镜病”到“大师风范”!
  • Flutter 3.35 更新要点解析
  • 解码词嵌入向量的正负奥秘
  • 【R语言】R语言矩阵运算:矩阵乘除法与逐元素乘除法计算对比
  • Flutter vs Pygame 桌面应用开发对比分析
  • SQL Server 2019安装教程(超详细图文)
  • ZKmall开源商城的移动商城搭建:Uni-app+Vue3 实现多端购物体验
  • 【Linux系统】动静态库的制作
  • 雷卯针对香橙派Orange Pi 5 Ultra开发板防雷防静电方案
  • riscv中断处理软硬件流程总结
  • AOP配置类自动注入