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

【Luogu】每日一题——Day14. P5960 【模板】差分约束 (图论 + 最短路)

链接:P5960 【模板】差分约束 - 洛谷

题目:

思路:

含多种讨论

我们将题目中的式子稍微变形,即 xc <= y + xc',此时的形态类似于 dis[u] <= dis[v] + w

即对短路,所以如果我们能找到一个最短路满足所有的 dis[c] <= dis[c'] + 1 即可,所以我们直接跑一遍最短路即可

具体的,我们增加一个超级点源,其到每个点的距离都是 0,然后跑一遍 SPFA

对于无解情况,显然是存在负环时无解,此时的方程不断替换最后会形如 x <= x - c,显然无解

对于其他变种,如 xc - xc' >= y,此时有 xc >= y + xc',即变为了求最长路,此时改变一下不等式即可 

来自题解:

代码:

#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" << endl
int n, m;
int vis[5005];
vector<vector<pair<int,int>>> g(5005);
int dis[5005];
int d[5005];
void solve()
{memset(dis, 0x3f, sizeof dis);cin >> n >> m;for (int i = 0; i < m; i++){int u, v, w;cin >> u >> v >> w;g[v].push_back({ u,w });}//超级点源for (int i = 1; i <= n; i++){g[0].push_back({ i ,0});}queue<int> q;q.push(0);vis[0] = 1;dis[0] = 0;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;if (!vis[son.first]){d[son.first]++;if (d[son.first] == n+1){no;return;}q.push(son.first);vis[son.first];}}}}for (int i = 1; i <= n; i++){cout << dis[i] << " ";}cout << endl;
}
signed main()
{//cin.tie(0)->sync_with_stdio(false);int t = 1;//cin >> t;while (t--){solve();}return 0;
}

http://www.dtcms.com/a/300006.html

相关文章:

  • Nacos-服务注册,服务发现(二)
  • mac版SVN客户端
  • Pythong高级入门Day5
  • npm ERR! cb() never called!
  • 昇思学习营-DeepSeek-R1-Distill-Qwen-1.5B 模型LoRA微调学习心得
  • 数据结构 二叉树(2)---二叉树的实现
  • 2025年SEVC SCI2区,混沌编码量子粒子群算法QPSO+柔性车间调度,深度解析+性能实测
  • 每日一题7.26
  • WorkManager vs Flow 适用场景分析
  • 抖音短视频矩阵系统源码搭建---底层框架5年开发分享
  • idea中无法删除模块,只能remove?
  • 二叉搜索树(Binary Search Tree)详解与java实现
  • 代码随想录打卡第十五天
  • 睡眠函数 Sleep() C语言
  • AI Agent开发学习系列 - langchain: 本地大模型调用
  • CMU15445-2024fall-project4踩坑经历
  • 设计自己的小传输协议 状态机解析与封装抽象
  • Java设计模式之行为型模式(中介者模式)实现方式详解
  • 函数参数的解包与顺序匹配机制
  • Go的管道——channel
  • HTML5元素相关补充
  • HighlightingSystem
  • MATLAB近红外光谱分析技术及实践技术应用
  • C++ 类型萃取:深入理解与实践
  • 【AcWing 143题解】最大异或对
  • Android-广播详解
  • 零拷贝应用场景
  • 【Spring AI】大模型服务平台-阿里云百炼
  • 基于cooragent的旅游多智能体的MCP组件安装与其开发
  • javaSE 6