acwing算法提高之图论--单源最短路的建图方式

目录

  • 1 介绍
  • 2 训练

1 介绍

本博客用来记录使用dijkstra算法或spfa算法求解最短路问题的题目。

2 训练

题目1:1129热浪

C++代码如下,

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>using namespace std;const int N = 2510;
int n, m;
vector<vector<pair<int,int>>> g; //first表示next_node,second表示w
int dist[N];
bool st[N];
int snode, enode;void dijkstra() {memset(dist, 0x3f, sizeof dist);dist[snode] = 0;priority_queue<pair<int, int>, vector<pair<int,int>>, greater<pair<int,int>>> h;h.push(make_pair(0, snode));while (!h.empty()) {//确定当前结点中,不在集合s且距离结点snode最近的结点。记作cnodeauto t = h.top();h.pop();int cdist = t.first, cnode = t.second;if (st[cnode]) continue; //如果cnode已经被确定是最小路径上的结点了,则跳过st[cnode] = true; //将它加入到集合中for (auto [next_node, w] : g[cnode]) {if (dist[next_node] > cdist + w) {dist[next_node] = cdist + w;h.push(make_pair(dist[next_node], next_node));}}}return;
}int main() {cin >> n >> m >> snode >> enode;g.resize(n + 10);for (int i = 1; i <= m; ++i) {int a, b, c;cin >> a >> b >> c;g[a].emplace_back(b, c);g[b].emplace_back(a, c);}//求snode到enode的最短距离dijkstra();cout << dist[enode] << endl;return 0;
}

题目2:1128信使

C++代码如下,

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>using namespace std;const int N = 110;
int n, m;
int d[N];
bool st[N];
vector<vector<pair<int,int>>> g;void dijkstra() {memset(d, 0x3f, sizeof d);d[1] = 0;priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> hp; //小根堆hp.push(make_pair(0, 1)); //first表示距离,second表示结点while (!hp.empty()) {auto t = hp.top(); //找到未在集合中,距离最小的结点hp.pop();int a = t.second;if (st[a]) continue; //已经用d[a]更新过了,将它放入集合中st[a] = true;for (auto [b, w] : g[a]) {if (d[b] > d[a] +w) { //d[b]此时比较大,用一个更小值来更新它。d[b] = d[a] + w;hp.push(make_pair(d[b], b));}}}return;
}int main() {cin >> n >> m;g.resize(n + 10);for (int i = 0; i < m; ++i) {int a, b, c;cin >> a >> b >> c;g[a].emplace_back(b, c);g[b].emplace_back(a, c);}dijkstra();int res = 0; //求最大值for (int i = 1; i <= n; ++i) res = max(res, d[i]);if (res == 0x3f3f3f3f) {res = -1;}cout << res << endl;return 0;
}

题目3:1127香甜的黄油

C++代码如下,

#include <iostream>
#include <cstring>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>using namespace std;const int N = 810;
int cow, n, m;
int cnt[N];
int d[N];
bool st[N];
vector<vector<pair<int,int>>> g;void spfa(int start) {//起点为startmemset(d, 0x3f, sizeof d);memset(st, 0, sizeof st);d[start] = 0;queue<int> q;q.push(start);st[start] = true; //结点start在队列中while (!q.empty()) {int t = q.front();q.pop();st[t] = false; //结点t不在队列中了for (auto [b, w] : g[t]) {if (d[b] > d[t] + w) {d[b] = d[t] + w;if (!st[b]) {q.push(b);st[b] = true;}}}}return;
}int main() {cin >> cow >> n >> m;g.resize(n + 10);for (int i = 1; i <= cow; ++i) {int a;cin >> a; //每头牛所在的牧场cnt[a]++;}for (int i = 1; i <= m; ++i) {int a, b, c;cin >> a >> b >> c;g[a].emplace_back(b, c);g[b].emplace_back(a, c);}//spfa()算法 //o(m)时间复杂度,不会被超时long long res = INT_MAX;for (int i = 1; i <= n; ++i) {//第i个牧场作为放糖点spfa(i);long long t = 0; for (int j = 1; j <= n; ++j) t += cnt[j] * d[j];res = min(res, t);}cout << res << endl;return 0;
}

题目4:1126最小花费

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>using namespace std;const int N = 2010;
int n, m;
int snode, enode;
double d[N]; //求最大距离,最大利率
bool st[N]; //是否使用它来更新过
vector<vector<pair<int,int>>> g;void dijkstra() {//d的初始化for (int i = 1; i <= n; ++i) d[i] = 0.0; //初始成0.0memset(st, 0, sizeof st);d[snode] = 1.0;priority_queue<pair<double,int>> hp; //大根堆hp.push(make_pair(1.0, snode));while (!hp.empty()) {auto t = hp.top();hp.pop();int a = t.second;if (st[a]) continue;st[a] = true;for (auto [b, w] : g[a]) {if (d[b] < d[a] * 0.01 * (100 - w)) {d[b] = d[a] * 0.01 * (100 - w);hp.push(make_pair(d[b], b));}}}return;
}int main() {cin >> n >> m;g.resize(n + 10);for (int i = 1; i <= m; ++i) {int a, b, c;cin >> a >> b >> c;g[a].emplace_back(b, c);g[b].emplace_back(a, c);}cin >> snode >> enode;dijkstra();double res = 100.0 / d[enode];printf("%.8f\n", res);return 0;
}

题目5:920最优乘车

C++代码如下,

#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <vector>
#include <queue>using namespace std;const int N = 510;
int n, m;
vector<vector<int>> g;
bool st[N];
int dist[N];void bfs() {memset(dist, 0x3f, sizeof dist);queue<int> q;q.push(1);dist[1] = 0;st[1] = true;while (!q.empty()) {auto t = q.front();q.pop();//t可以走到哪儿for (auto b : g[t]) {if (!st[b]) {dist[b] = dist[t] + 1;q.push(b);st[b] = true;}}}return;
}int main() {cin >> m >> n;g.resize(n + 10);string line;getline(cin, line);for (int i = 0; i < m; ++i) {getline(cin, line);stringstream ssin(line);vector<int> nodes;int node = -1;while (ssin >> node) {nodes.emplace_back(node);}for (int i = 0; i < nodes.size(); ++i) {for (int j = i + 1; j < nodes.size(); ++j) {g[nodes[i]].emplace_back(nodes[j]);}}}bfs();if (dist[n] == 0x3f3f3f3f) puts("NO");else cout << max(dist[n] - 1, 0) << endl;return 0;
}

题目6:903昂贵的聘礼

C++代码如下,

#include <cstring>
#include <iostream>
#include <algorithm>using namespace std;const int N = 110, INF = 0x3f3f3f3f;
int n, m;
int w[N][N], level[N];
int dist[N];
bool st[N];int dijkstra(int down, int up) {memset(dist, 0x3f, sizeof dist);memset(st, 0, sizeof st);dist[0] = 0;for (int i = 1; i <= n + 1; ++i) {int t = -1;for (int j = 0; j <= n; ++j) {if (!st[j] && (t == -1 || dist[t] > dist[j])) {t = j;}}st[t] = true;for (int j = 1; j <= n; ++j) {if (level[j] >= down && level[j] <= up) {dist[j] = min(dist[j], dist[t] + w[t][j]);}}}return dist[1];
}int main() {cin >> m >> n;memset(w, 0x3f, sizeof w);for (int i = 1; i <= n; ++i) w[i][i] = 0;for (int i = 1; i <= n; ++i) {int price, cnt;cin >> price >> level[i] >> cnt;w[0][i] = min(price, w[0][i]);while (cnt--) {int id, cost;cin >> id >> cost;w[id][i] = min(w[id][i], cost);}}int res = INF;for (int i = level[1] - m; i <= level[1]; ++i) res = min(res, dijkstra(i, i + m));cout << res << endl;return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/780748.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

国内如何购买midjourney?midjourney购买教程?midjourney注册方式?

1. Midjourney介绍 Midjourney 是一款备受欢迎的人工智能生成图像工具&#xff0c;它可以通过输入文字描述&#xff0c;自动生成精美的图像。与许多其他图像生成工具不同&#xff0c;Midjourney 不需要安装任何软件&#xff0c;也不受个人电脑性能的限制&#xff0c;因为它运行…

【测试篇】测试眼里的 BUG

文章目录 如何描述一个bug如何定义 bug 的级别BUG 的生命周期跟开发起争执怎么办&#xff08;高频面试题&#xff09; 如何描述一个bug 一个合格的bug描述应该包含以下几个部分&#xff1a; 发现问题的版本问题出现的环境错误重现的步骤预期行为的描述错误行为的描述其他&…

USB-PD

这是目录 写在前面1、概览2、信息2.1 消息结构2.1.1 消息头 3、soft or hard reset1、soft reset2、hard reset 3、TYPE-C相关握手3.1、CC线的状态3.1.1、默认电源值3.2 TYPE-C设备握手协商过程确定握手类型DRP和DRP设备握手 写在前面 1、记录自己的学习PD协议层的文章 1、概…

消息队列经典应用场景

笔者心中,消息队列,缓存,分库分表是高并发解决方案三剑客。 在职业生涯中,笔者曾经使用过 ActiveMQ 、RabbitMQ 、Kafka 、RocketMQ 这些知名的消息队列 。 这篇文章,笔者结合自己的真实经历,和大家分享消息队列的七种经典应用场景。 1 异步&解耦 笔者曾经负责某电…

深入探索Python异步编程:从原理到实践

一、引言 随着计算机技术的发展&#xff0c;多线程、多进程等并发编程技术已经不能满足所有场景的需求。异步编程作为一种新的编程范式&#xff0c;以其轻量级、高效的特点逐渐受到开发者的青睐。Python的asyncio库提供了原生的异步编程支持&#xff0c;使得Python开发者能够轻…

00、SpringBatch 4.x.x版本:简单入门

00、SpringBatch批处理 一、介绍1、什么是批处理&#xff1f;2、官网3、优势4、组织架构5、程序运行架构图 二、入门案例-H2版(内存)1、新建项目2、引入依赖3、新建HelloJob.java 三、入门案例-MySQL版1、引入依赖2、修改 application.yml3、验证 四、案例解析1、EnableBatchPr…

国产桌面操作系统统一身份认证及2FA双因子认证安全升级方案

某金融运营服务公司&#xff0c;主要负责业务处理、客户服务、业务监控、报表统计等金融运营服务&#xff0c;为集团下设二级单位&#xff0c;坐落于一线城市&#xff0c;对政策风向有很高的敏锐度。 该公司已为公司业务人员、客户服务、监督员等配备了数百台国产桌面操作系统…

服务器大请求体问题定位

背景 整个系统,分位微服务A、微服务B,A在调用B的过程中,报400BadRequest,问题定位到修复后,如何发送一个同样的请求进行验证 解决过程 1、查询A服务的日志,发现在调用B的过程中报错400BadRequest,并且请求体非常大300多KB 2、查看B服务的日志,发现请求没有进来 3、发…

Python 基于 OpenCV 视觉图像处理实战 之 OpenCV 简单实战案例 之七 简单图像浮雕效果

Python 基于 OpenCV 视觉图像处理实战 之 OpenCV 简单实战案例 之七 简单图像浮雕效果 目录 Python 基于 OpenCV 视觉图像处理实战 之 OpenCV 简单实战案例 之七 简单图像浮雕效果 一、简单介绍 二、简单图像浮雕效果实现原理 三、简单图像浮雕效果案例实现简单步骤 四、注…

网络七层模型之会话层:理解网络通信的架构(五)

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

【豫都故郡·领航新篇】Springer独立出版 |第二届先进无人飞行系统国际会议(ICAUAS 2024)

会议简介 Brief Introduction 2024年第二届先进无人飞行系统国际会议(ICAUAS 2024) 会议时间&#xff1a;2024年6月14日-16日 召开地点&#xff1a;中国南昌 大会官网&#xff1a;ICAUAS 2024-2024 2nd International Conference on Advanced Unmanned Aerial Systems2024 2nd …

【C++】力扣-415-字符串相加(双指针,图例详解!!!)

目录 一、前言 二、字符串相加 三、共勉 一、前言 最近春招已经开始&#xff0c;看周围的同学都在投递一些大厂的实习&#xff0c;某为的手撕代码 --- 字符串相乘&#xff0c;某讯的手撕代码 --- 字符串相减等。 于是专门去 Leetcode 上搜索了一下&#xff0c;发现这类题目是面…

conda使用记录

linux 使用conda创建新一个新的python环境过程 conda create -n recommendation_env python3.8.18 # 指定python版本 conda env list # 查看所有的环境 conda activate recommendation_env # 激活创建的新环境 pip install flask # 安装依赖 或者 pip install flask版本号 或者…

每日更新5个Python小技能 | 第六期

大家好&#xff01;欢迎阅读每日更新的Python小技能系列&#xff0c;今天是第六期。在这个系列中&#xff0c;我将每天分享5个高级的Python小技巧&#xff0c;帮助大家进一步提升编程技能。让我们开始吧&#xff01; 1. 元类&#xff08;Metaclasses&#xff09; 元类是Pytho…

脑机交互,屏幕是必须?No!让机器人发出激光光点实现脑机接口交互

一般说来&#xff0c;传统脑机接口(BCI)系统的交互过程依靠一个图形化的用户界面&#xff0c;不利于设备的便携性。而一种无屏幕的BCI可以通过让机器人在外界环境中发出刺激从而实现更直接的命令其中机器人使用激光光点凸显环境中的候选对象&#xff0c;而用户的目标则从脑电图…

跳跃游戏-java

题目描述: 给你一个非负整数数组 nums &#xff0c;你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大长度 判断你是否能够到达最后一个下标&#xff0c;如果可以&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 解题思想: …

正则表达式爬取页面图片(<img[^>]*src=“([^“]+))

import re import os import requests if __name__ __main__:# 创建一个文件夹&#xff0c;保存所有的图片if not os.path.exists(./##):os.mkdir(./##)# - 指定urlurl *******headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like …

AIGC新潮流!手势灵动数字人视频、百变模特服装图、3D模型纹理一键生成

1、营销应用:AI生成生成带手势(手部动作)的数字人视频 (1)一个基于扩散模型的数字人生成框架,专注于生成具有全身动作的主播风格视频。该系统通过仅需一分钟的个人视频片段进行训练,便能自动生成具有精确躯干和手部动作的主播风格视频。 (2)定位:该框架定位于解决现…

docker环境配置过程中的常见问题

1、pull镜像问题 docker pull jenkins/jenkins:lts Using default tag: latest Trying to pull repository docker.io/library/centos ... Get https://registry-1.docker.io/v2/library/centos/manifests/latest: Get https://auth.docker.io/token?scoperepository%3Alibr…

自动驾驶-如何进行多传感器的融合

自动驾驶-如何进行多传感器的融合 附赠自动驾驶学习资料和量产经验&#xff1a;链接 引言 自动驾驶中主要使用的感知传感器是摄像头和激光雷达&#xff0c;这两种模态的数据都可以进行目标检测和语义分割并用于自动驾驶中&#xff0c;但是如果只使用单一的传感器进行上述工作…