当前位置: 首页 > 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.dtcms.com/a/335488.html

相关文章:

  • 中国大学MOOC--C语言程序设计第十周字符串(下)
  • openEuler等Linux系统中如何复制移动硬盘的数据
  • HTTPS 配置与动态 Web 内容部署指南
  • Hadoop入门
  • SpringCloud 06 服务容错 Sentinel
  • NY270NY273美光固态闪存NY277NY287
  • 黎阳之光:以动态感知与 AI 深度赋能,引领电力智慧化转型新革命
  • mysql||事务相关知识
  • nertctl使用了解
  • Node.js导入MongoDB具体操作
  • IoT/HCIP实验-5/基于WIFI的智慧农业实验(LwM2M/CoAP+PSK+ESP8266 连接到 IoTDA)
  • python study notes[4]
  • Vue深入组件:Props 详解3
  • 【adb端口5555】烽火hg680-gy_烽火hg680-gc安卓9线刷烧录包 解决用一段时间就提示升级的问题
  • 回溯剪枝的 “减法艺术”:化解超时危机的 “救命稻草”(一)
  • 如何在 Ubuntu 24.04、22.04 或 20.04 Linux 中更改计算机名称
  • 智能化管理:开启海洋牧场新时代
  • 字节 Golang 大模型应用开发框架 Eino简介
  • Vue深入组件:Props 详解2
  • es7.17.x es服务yellow状态的排查查看节点,分片状态数量
  • 42 C++ STL模板库11-容器4-forward_list
  • C++算法竞赛:位运算
  • 线程(基本概念和相关命令)
  • CT01-反转链表(Java)
  • 从零开始:SpringBoot与KingbaseES的完美融合实践
  • 基于飞算JavaAI的可视化数据分析集成系统项目实践:从需求到落地的全流程解析
  • Java 大视界 -- Java 大数据分布式计算在基因测序数据分析与精准医疗中的应用(400)
  • Excel 表格数据自动填充
  • 【线程安全(二) Java EE】
  • 基于飞算JavaAI实现布隆过滤器防止缓存穿透:原理、实践与全流程解析