课上题目代码

2024-02-20 01:12
文章标签 代码 题目 课上

本文主要是介绍课上题目代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

dijkstra和spfa区别 :

dikstra是基于贪心的思想,每次选择最近的点去更新其它点,过后就不再访问。而在spfa算法中,只要有某个点的距离被更新了,就把它加到队列中,去更新其它点,所有每个点有被重复加入队列的可能。

或者跟具体的说区别在于diikstra总是要找到dist最小的元素来作为父节点更新其他点,而不是直接取队头元素(当然如果是优先队列也是取队头元素) :更新的顺序不同主要导致的差异是算法时间复杂度上的不同;功能上当然也造成了一定的区别,比如由于dikstra严格要求每次取的元素都是队列中最小的,这也导致了队头元素的dist值是单调递增的,从而一旦出现负权边就会破坏这个性质,从而导致找不到最优解。

图结构练习——最短路径 | SDUT OnlineJudge

#include <bits/stdc++.h>
using namespace std;
#define xx first
#define yy second
#define endl "\n"
#define int long long
#define pb push_back
typedef pair<int, int> PII;
#define Yshanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = 1e6 + 10, M = 1010, inf = 0x3f3f3f3f;
int n, m;
int g[M][M];
void floyd()
{for (int k = 1; k <= n; k++)for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)g[i][j] = min(g[i][k] + g[k][j], g[i][j]);
}
signed main()
{Yshanqian;while (cin >> n >> m){memset(g, inf, sizeof g);for (int i = 1; i <= n; i++)g[i][i] = 0;for (int i = 1; i <= m; i++){int a, b, c;cin >> a >> b >> c;if (c < g[a][b])g[a][b] = g[b][a] = c;}floyd();cout << g[1][n] << endl;}return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define xx first
#define yy second
#define endl "\n"
#define int long long
#define pb push_back
typedef pair<int, int> PII;
#define Yshanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = 1e4 + 10, M = 1010, inf = 0x3f3f3f3f;
int n, m;
int dis[N], st[N];
vector<PII> g[N];
void spfa()
{queue<int> q;for (int i = 1; i <= n; i++)dis[i] = 1e18;memset(st, 0, sizeof st);dis[1] = 0;q.push(1);st[1] = 1;while (q.size()){int u = q.front();q.pop();st[u] = 0;for (auto ed : g[u]){int v = ed.xx, w = ed.yy;if (dis[v] > dis[u] + w){dis[v] = dis[u] + w;if (!st[v])q.push(v);}}}
}
signed main()
{Yshanqian;while (cin >> n >> m){for (int i = 1; i <= n; i++)g[i].clear();for (int i = 1; i <= m; i++){int a, b, c;cin >> a >> b >> c;g[a].pb({b, c});g[b].pb({a, c});}spfa();cout << dis[n] << endl;}return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define xx first
#define yy second
#define endl "\n"
#define int long long
#define pb push_back
typedef pair<int, int> PII;
#define Yshanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = 1e6 + 10, M = 1010, inf = 0x3f3f3f3f;
int n, m;
int dis[N], st[N];
vector<PII> g[N];
void dijkstra()
{priority_queue<PII, vector<PII>, greater<PII>> q;memset(dis, inf, sizeof dis);memset(st, 0, sizeof st);dis[1] = 0;q.push({0, 1});while (q.size()){int u = q.top().yy;q.pop();if (st[u])continue;st[u] = 1;for (auto ed : g[u]){int v = ed.xx, w = ed.yy;if (dis[v] > dis[u] + w){dis[v] = dis[u] + w;q.push({dis[v], v});}}}
}
signed main()
{Yshanqian;while (cin >> n >> m){for (int i = 1; i <= n; i++)g[i].clear();for (int i = 1; i <= m; i++){int a, b, c;cin >> a >> b >> c;g[a].pb({b, c});g[b].pb({a, c});}dijkstra();cout << dis[n] << endl;}return 0;
}

 数据结构实验之图论七:驴友计划

#include <bits/stdc++.h>
using namespace std;
#define xx first
#define yy second
#define endl "\n"
#define int long long
#define pb push_back
typedef pair<int, int> PII;
#define Yshanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = 1e6 + 10, M = 1010, inf = 0x3f3f3f3f;
int n, m, s, t;
int dis[N], st[N], mon[N];
struct node
{int v, w, cost;
};
vector<node> g[N];
void dijkstra()
{priority_queue<PII, vector<PII>, greater<PII>> q;memset(dis, inf, sizeof dis);memset(st, 0, sizeof st);dis[s] = 0;q.push({0, s});while (q.size()){int u = q.top().yy;q.pop();if (st[u])continue;st[u] = 1;for (auto ed : g[u]){int v = ed.v, w = ed.w, cost = ed.cost;if (dis[v] > dis[u] + w){dis[v] = dis[u] + w;mon[v] = mon[u] + cost;q.push({dis[v], v});}else if (dis[v] == dis[u] + w){if (mon[v] > mon[u] + cost){mon[v] = mon[u] + cost;q.push({dis[v], v});}}}}
}
signed main()
{Yshanqian;int T;cin >> T;while (T--){cin >> n >> m >> s >> t;for (int i = 0; i <= n; i++)g[i].clear();for (int i = 1; i <= m; i++){int a, b, c, d;cin >> a >> b >> c >> d;g[a].pb({b, c, d});g[b].pb({a, c, d});}dijkstra();cout << dis[t] << " " << mon[t] << endl;}return 0;
}

 人活着系列之芳姐和芳姐的猪

#include <bits/stdc++.h>
using namespace std;
#define xx first
#define yy second
#define endl "\n"
#define int long long
#define pb push_back
typedef pair<int, int> PII;
#define Yshanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = 1e6 + 10, M = 1010, inf = 0x3f3f3f3f;
int n, m, k;
int a[N], g[M][M];
void floyd()
{for (int k = 1; k <= m; k++)for (int i = 1; i <= m; i++)for (int j = 1; j <= m; j++)g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
signed main()
{Yshanqian;cin >> n >> m >> k;for (int i = 1; i <= n; i++)cin >> a[i];memset(g, inf, sizeof g);for (int i = 1; i <= m; i++)g[i][i] = 0;for (int i = 1; i <= k; i++){int a, b, c;cin >> a >> b >> c;if (c < g[a][b])g[a][b] = g[b][a] = c;}floyd();int ans = 1e18;for (int i = 1; i <= m; i++){int tmp = 0;for (int j = 1; j <= n; j++){int s = a[j];tmp += g[i][s];}ans = min(ans, tmp);}cout << ans << endl;return 0;
}

这篇关于课上题目代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/726578

相关文章

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

代码随想录冲冲冲 Day39 动态规划Part7

198. 打家劫舍 dp数组的意义是在第i位的时候偷的最大钱数是多少 如果nums的size为0 总价值当然就是0 如果nums的size为1 总价值是nums[0] 遍历顺序就是从小到大遍历 之后是递推公式 对于dp[i]的最大价值来说有两种可能 1.偷第i个 那么最大价值就是dp[i-2]+nums[i] 2.不偷第i个 那么价值就是dp[i-1] 之后取这两个的最大值就是d

pip-tools:打造可重复、可控的 Python 开发环境,解决依赖关系,让代码更稳定

在 Python 开发中,管理依赖关系是一项繁琐且容易出错的任务。手动更新依赖版本、处理冲突、确保一致性等等,都可能让开发者感到头疼。而 pip-tools 为开发者提供了一套稳定可靠的解决方案。 什么是 pip-tools? pip-tools 是一组命令行工具,旨在简化 Python 依赖关系的管理,确保项目环境的稳定性和可重复性。它主要包含两个核心工具:pip-compile 和 pip

题目1254:N皇后问题

题目1254:N皇后问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 题目描述: N皇后问题,即在N*N的方格棋盘内放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在同一斜线上。因为皇后可以直走,横走和斜走如下图)。 你的任务是,对于给定的N,求出有多少种合法的放置方法。输出N皇后问题所有不同的摆放情况个数。 输入

题目1380:lucky number

题目1380:lucky number 时间限制:3 秒 内存限制:3 兆 特殊判题:否 提交:2839 解决:300 题目描述: 每个人有自己的lucky number,小A也一样。不过他的lucky number定义不一样。他认为一个序列中某些数出现的次数为n的话,都是他的lucky number。但是,现在这个序列很大,他无法快速找到所有lucky number。既然

D4代码AC集

贪心问题解决的步骤: (局部贪心能导致全局贪心)    1.确定贪心策略    2.验证贪心策略是否正确 排队接水 #include<bits/stdc++.h>using namespace std;int main(){int w,n,a[32000];cin>>w>>n;for(int i=1;i<=n;i++){cin>>a[i];}sort(a+1,a+n+1);int i=1

html css jquery选项卡 代码练习小项目

在学习 html 和 css jquery 结合使用的时候 做好是能尝试做一些简单的小功能,来提高自己的 逻辑能力,熟悉代码的编写语法 下面分享一段代码 使用html css jquery选项卡 代码练习 <div class="box"><dl class="tab"><dd class="active">手机</dd><dd>家电</dd><dd>服装</dd><dd>数码</dd><dd

生信代码入门:从零开始掌握生物信息学编程技能

少走弯路,高效分析;了解生信云,访问 【生信圆桌x生信专用云服务器】 : www.tebteb.cc 介绍 生物信息学是一个高度跨学科的领域,结合了生物学、计算机科学和统计学。随着高通量测序技术的发展,海量的生物数据需要通过编程来进行处理和分析。因此,掌握生信编程技能,成为每一个生物信息学研究者的必备能力。 生信代码入门,旨在帮助初学者从零开始学习生物信息学中的编程基础。通过学习常用