课上题目代码

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

相关文章

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

IDEA与MyEclipse代码量统计方式

《IDEA与MyEclipse代码量统计方式》文章介绍在项目中不安装第三方工具统计代码行数的方法,分别说明MyEclipse通过正则搜索(排除空行和注释)及IDEA使用Statistic插件或调整搜索... 目录项目场景MyEclipse代码量统计IDEA代码量统计总结项目场景在项目中,有时候我们需要统计

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

MySQL实现多源复制的示例代码

《MySQL实现多源复制的示例代码》MySQL的多源复制允许一个从服务器从多个主服务器复制数据,这在需要将多个数据源汇聚到一个数据库实例时非常有用,下面就来详细的介绍一下,感兴趣的可以了解一下... 目录一、多源复制原理二、多源复制配置步骤2.1 主服务器配置Master1配置Master2配置2.2 从服

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据