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

如何做企业网站wordpress如何导航网站模板

如何做企业网站,wordpress如何导航网站模板,曲靖网站推广,网店怎么开店详细教程链接:P3385 【模板】负环 - 洛谷 题目: 思路: 考察 Bellman-Ford 或 SPFA 本题是一个板子题,也是学到了 Bellman-Ford 和 SPFA Bellman-Ford 算法的主要思想其实类似迪杰斯特拉,我们暴力枚举每一个点的每一条边&…

链接: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.dtcms.com/wzjs/834431.html

相关文章:

  • 网站设计网址达建网站的需要
  • 类似建设通的网站软件开发工具包下载
  • 织梦做手机网站做搜狗网站优化排名
  • 佛山新网站建设服务河北网络科技公司有哪些
  • 珠海建设网站首页wordpress 站点身份
  • 专门做酒店的网站wordpress登录循环
  • 青岛网站seowordpress 无法登录界面
  • linux 做网站数据库网站有哪些分类
  • 大连企业网站建站网站访问量突然增加
  • 网站需要哪些手续申请免费网站哪个好
  • 做网站一定要买服务器么烟台网站建设九七
  • 模版网站后期可以更换图片吗网站建设费是什么费用
  • 建设个网站广州哪个网站做h5比较好
  • 有那个网站可以做报名链接的网店设计实训报告
  • 网站建设怎么解析域名织梦的手机端网站模板下载
  • 深圳宝安网站设计公司深圳vi设计多少钱
  • 实验室网站建设意义seo标题优化的方法
  • 奇客影院wordpress宁波seo教程网
  • 网站开发书百度云wordpress 表格提交
  • 南阳定制网站制作价格低杭州做网站模板
  • 不良网站进入窗口软件下载7什么网站上做指甲最便宜
  • 环保h5微网站常用ppt模板网站
  • 中国制造网国际站网址公司网站如何更改内容
  • 怎样建立网站有哪些流程网站系统建设招标
  • dw做的网站怎样做成手机版的9免费建网站
  • 卖代码建设网站企业网络建设公司排名
  • 网站建设发好处wordpress个人收款
  • 外贸soho虚拟公司做网站展示型网站建设价格
  • 英文网站seo 谷歌吴忠市住房和城乡建设厅网站
  • dedecms 做门户网站动漫设计与制作专业大学排名