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

【Luogu】每日一题——Day1. P3385 【模板】负环

链接:P3385 【模板】负环 - 洛谷

题目:

思路:

考察 Bellman-Ford 或 SPFA

本题是一个板子题,也是学到了 Bellman-Ford 和 SPFA

Bellman-Ford 算法的主要思想其实类似迪杰斯特拉,我们暴力枚举每一个点的每一条边,如果能更新最小距离,那么就替换,同时标记该次我们进行了松弛操作,通常情况下 n-1 次松弛操作后我们就能找到最短路了,但是由于可能存在负环,因此我们要注意多判断一次

负环顾名思义就是权值为负的环,既然它为负,那么我们就可以走无数次使得这个环的权值无限小

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 int long long
#define yes cout << "YES" << endl
#define no cout << "NO" << endlvoid solve()
{int n, m;cin >> n >> m;vector<vector<pair<int,int>>> g(n + 1);for (int i = 0; i < m; i++){int u, v, w;cin >> u >> v >> w;if (w >= 0){g[u].push_back({ v,w });g[v].push_back({ u,w });}else{g[u].push_back({ v,w });}}int flag = 0;vector<int> dis(n + 1, 1e18);dis[1] = 0;for (int i = 1; i <= n; i++){flag = 0;for (int i = 1; i <= n; i++){if (dis[i] == 1e18) continue;for (auto &son : g[i]){if (dis[son.first] > dis[i] + son.second){dis[son.first] = dis[i] + son.second;flag = 1;}}}}flag ? yes : no;
}
signed main()
{//cin.tie(0)->sync_with_stdio(false);int t = 1;cin >> t;while (t--){solve();}return 0;
}
#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 int long long
#define yes cout << "YES" << endl
#define no cout << "NO" << endlvoid solve()
{int n, m;cin >> n >> m;vector<vector<pair<int,int>>> g(n + 1);for (int i = 0; i < m; i++){int u, v, w;cin >> u >> v >> w;if (w >= 0){g[u].push_back({ v,w });g[v].push_back({ u,w });}else{g[u].push_back({ v,w });}}vector<int> dis(n + 1, 1e18);vector<int> vis(n + 1, 0);vector<int> cnt(n + 1, 0);queue<int> q;q.push(1);dis[1] = 0;vis[1] = 1;while (!q.empty()){auto t = q.front();q.pop();vis[t] = 0;for (auto son : g[t]){if (dis[son.first] > dis[t] + son.second){dis[son.first] = dis[t] + son.second;cnt[son.first] = cnt[t] + 1;if (cnt[son.first] >= n){yes;return;}if (!vis[son.first])q.push(son.first), vis[son.first] = 1;}}}no;
}
signed main()
{//cin.tie(0)->sync_with_stdio(false);int t = 1;cin >> t;while (t--){solve();}return 0;
}

 

http://www.xdnf.cn/news/1106029.html

相关文章:

  • 上位机知识篇---高效下载安装方法
  • Script Error产生的原因及解法
  • 机器学习详解
  • Day58
  • Java基础-String常用的方法
  • 隆重介绍 Xget for Chrome:您的终极下载加速器
  • Linux入门篇学习——Linux 编写第一个自己的命令,make 工具和 makefile 文件
  • 嵌入式八股文之 GPIO
  • 鸿蒙系统安全机制全解:安全启动 + 沙箱 + 动态权限实战落地指南
  • 【驱动】移植CH340驱动,设置 udev 规则,解决和 BRLTTY 的冲突
  • Word表格默认格式修改成三线表,一劳永逸,提高生产力!
  • FREERTOS根本不能使用连续接收串口思想
  • P4597 序列 sequence题解
  • 跟着Carl学算法--二叉树【3】
  • 猿人学js逆向比赛第一届第十九题
  • 【每日算法】专题八_分治_归并排序
  • RLHF:人类反馈强化学习 | 对齐AI与人类价值观的核心引擎
  • Windows解决 ping 127.0.0.1 一般故障问题
  • 阿里云服务器,CentOS7.9上安装YApi 接口管理平台
  • Redis概念和基础
  • AI基建还能投多久?高盛:2-3年不是问题,回报窗口才刚开启
  • 学习C++、QT---21(QT中QFile库的QFile读取文件、写入文件的讲解)
  • MySQL内置函数(8)
  • Windows删除文件或者拔出U盘显示正在使用/占用解决办法
  • 必备软件推荐:1、Everything:Windows 文件查找的终极利器
  • CSS和CSS3区别对比
  • [面试] 手写题-插入排序
  • 网络安全第一次作业
  • 史上最详细Java并发多线程(面试必备,一篇足矣)
  • 视频翻译用什么软件?这里有5个高效推荐