综合模型及应用(图论学习总结部分内容)

devtools/2024/10/18 7:54:40/

文章目录

前言

由于图论学习总结内容过多,全放在一篇博客过于冗长现进行拆分,本文是综合模型及应用部分,其他部分地址见:图论学习总结(For XCPC)

六、综合模型及应用(以题目总结为主)

分层图思想(包括拆点建图)

e g 1 : 通信线路 eg1:通信线路 eg1:通信线路​​​A-Telephone Lines(蓝书例题)

题目大意

Telephone Lines

解法一:动态规划

​ 仿照动态规划的思想,用 d i s t [ x , i ] dist[x,i] dist[x,i] 表示从 1 1 1 到达 x x x,途中已经指定了 i i i 条电缆免费时,经过路径上最贵的边的花费的最小值。若有一条边 w ( x , y ) w(x,y) w(x,y) d i s t [ y , i ] = max ⁡ ( d i s t [ x , i ] , w ) dist[y,i] = \max(dist[x,i],w) dist[y,i]=max(dist[x,i],w) d i s t [ y , i + 1 ] = d i s t [ x , i ] dist[y,i+1] = dist[x,i] dist[y,i+1]=dist[x,i]。两个式子分别表示不用免费的升级服务,用一次免费的升级服务。可以看到我们的状态转移明显是有后效性的,一种解决方案是利用迭代的思想,借助 S P F A SPFA SPFA算法就行动规,直至所有状态收敛。对于特殊构造的数据 S P F A SPFA SPFA 的时间复杂度可能退化为 O ( N M ) O(NM) O(NM),谨慎使用。

解法二:分层图最短路

​ 从最短路问题的角度去理解,图中的结点可以扩展到二维元组 ( x , i ) (x,i) (x,i) 表示一个结点。对于边 w ( x , y ) w(x,y) w(x,y),我们可以在分层图中 ( x , i ) (x,i) (x,i) ( y , i + 1 ) (y,i+1) (y,i+1) 连一条边权为 0 0 0 的有向边,那么我们就构造出了一个 N × K N\times K N×K 个点 ( N + P ) × K (N+P)\times K (N+P)×K 条边的分层图。其中不同层之间的有向边帮助我们确保了答案的计算只能从低层向高层转移,解决了后效性问题。这类广义的最短路问题被称为分层图最短路问题,我们可以直接在分层图上跑 D i j k s t r a Dijkstra Dijkstra 即可得到答案。 时间复杂度为 O ( ( N + P ) K log ⁡ ( N K ) ) O((N+P)K\log(NK)) O((N+P)Klog(NK))

解法三:二分答案+ 01 B F S 01BFS 01BFS

我们进一步思考本题答案的性质,当支付的钱更多时,合法的升级方案一定包含了花费更少的升级方案,也就是说当 K K K 确定时,我们花费的钱更多,合法的方案也就更多,方案数有单调性,我们考虑二分答案,那么问题就转化为:是否存在合法的方案使得花费不超过 m i d mid mid

​ 对于 c h e a k cheak cheak 函数,我们只需要将花费小于等于 m i d mid mid 的边权设为 0 0 0,其余设为 1 1 1,我们可以用双端队列 B F S BFS BFS 求出边权只包含 0 / 1 0/1 0/1 的单源最短路问题。判断是否 d i s t [ n ] ≤ K dist[n]\le K dist[n]K 即可。时间复杂度为 O ( ( N + P ) log ⁡ max ⁡ L ) O((N+P)\log \max_L) O((N+P)logmaxL)

ac代码参考:分层图最短路 、二分答案+ 01 B F S 01BFS 01BFS

e g 2 : 小雨坐地铁 eg2:小雨坐地铁 eg2:小雨坐地铁​ 1012-小雨坐地铁(nowcoder.com)

题目大意

小雨坐地铁

e g 3 : G . B i c y c l e s eg3: G. Bicycles eg3:G.Bicycles​ Problem - 1915G - Codeforces(注意带点贪心剪枝)

题目大意

All of Slavic’s friends are planning to travel from the place where they live to a party using their bikes. And they all have a bike except Slavic. There are n n n cities through which they can travel. They all live in the city 1 1 1 and want to go to the party located in the city n n n. The map of cities can be seen as an undirected graph with n n n nodes and m m m edges. Edge i i i connects cities u i u_i ui and v i v_i vi and has a length of w i w_i wi.

Slavic doesn’t have a bike, but what he has is money. Every city has exactly one bike for sale. The bike in the i i i-th city has a slowness factor of s i s_{i} si. Once Slavic buys a bike, he can use it whenever to travel from the city he is currently in to any neighboring city, by taking w i ⋅ s j w_i \cdot s_j wisj time, considering he is traversing edge i i i using a bike j j j he owns.

Slavic can buy as many bikes as he wants as money isn’t a problem for him. Since Slavic hates traveling by bike, he wants to get from his place to the party in the shortest amount of time possible. And, since his informatics skills are quite rusty, he asks you for help.

What’s the shortest amount of time required for Slavic to travel from city 1 1 1 to city n n n? Slavic can’t travel without a bike. It is guaranteed that it is possible for Slavic to travel from city 1 1 1​ to any other city.

e g 4 : R i n n e L o v e s G r a p h eg4: Rinne\ Loves\ Graph eg4:Rinne Loves Graph​ NC22594, 1031-Rinne Loves Graph(nowcoder.com)

题目大意

Rinne Loves Graph​
rating 2400

平面图思想

最短路图

其他

一些综合题
e g 1 : eg1: eg1: C-Decrement on the Tree(2020牛客多校第十场 / 2024牛客五一集训派对Day 3)

题面

2020牛客多校10_C

题目分析

  • 题目大意是给你一棵树要支持边权修改,每次操作选一条路径,把路径上的边权全部减少 1 1 1。问减到 0 0 0​​ 的最小次数。
  • 读完题我们发现,其实我们要将这棵树拆分成诺干条简单路径。
  • 这样我们就转化成每个点需要占用多少次路径使其减为一,我们可以考虑点的连边情况,我们可以贪心的想,对于一个点的出和入能多匹配就多匹配。
  • 怎么使其能覆盖就覆盖呢?
    • 如果 m a x ≤ s u m / 2 max \le sum/2 maxsum/2,则会多出 s u m % 2 sum\%2 sum%2 的数量
    • 如果 m a x > s u m / 2 max>sum/2 max>sum/2,则会多出 2 × m a x − s u m 2\times max-sum 2×maxsum​ 的数量
  • 看到在带边权的树上操作,我们一个惯用的套路就是:边权转点权
  • 这样我们只需要把这些加起来除个2就好了。
  • 边权修改的话

ac代码参考:代码查看 (nowcoder.com)

e g 3 : eg3: eg3:​ K-Stack_2024牛客五一集训day5 / 2021牛客暑期多校2

题目大意

题目分析

CCPC_Guilin__J_httpscodeforcescomgym104008problemJDP_106"> e g 4 eg 4 eg4:Permutation\ Puzzle $ 2022 C C P C G u i l i n − J 2022\ CCPC\ Guilin - J 2022 CCPC GuilinJ (金牌题,拓扑排序+DP+贪心)

题目大意

Little r e l y t 871 relyt871 relyt871 is solving a puzzle. The key to the puzzle is a permutation containing numbers 1 … n 1 \dots n 1n. The values at some positions of the permutation are already fixed, and r e l y t 871 relyt871 relyt871 needs to fill numbers into the remaining positions.

Besides, little r e l y t 871 relyt871 relyt871 has gathered m m m extra requirements about the permutation. Let the solution be represented as p 1 , p 2 , . . . , p n p_1,p_2,...,p_n p1,p2,...,pn, each clue is a pair of indices ( u i , v i ) (u_i,v_i) (ui,vi), which means that p u i < p v i p_{u_i} < p_{v_i} pui<pvi​ should be satisfied in the solution.

Little r e l y t 871 relyt871 relyt871​ wonders if all requirements may be satisfied at the same time. Write a program to find a valid solution when there is one.

题目分析

​ 这是一道灵活运用 拓扑排序 + D P 拓扑排序+DP 拓扑排序+DP求最短路的构造题,想到图论不难,难点是想到用图论算法这么做。

​ 根据所给限制建图,将 p u < p v p_u < p_v pu<pv 的限制视为一条 u → v u → v uv 的单向边,题目保证会得到一个 D A G DAG DAG。 设在 D A G DAG DAG 上存在一条从 𝑢 到 𝑣 的边数为 k k k 的路径,若 p u p_u pu 已知而 p v p_v pv 未知,则可以推出 p v ≥ p u + k p_v ≥ p_u +k pvpu+k; 若 p u p_u pu 未知而 p v p_v pv 已知,则可以推出 p u ≤ p v − k p_u ≤ p_v − k pupvk。 首先我们通过上述规则推导出所有位置的取值范围 [ L i , R i ] [L_i , R_i] [Li,Ri]。对于已知位置,显然 L i = R i = P i L_i = R_i = P_i Li=Ri=Pi,对于未知位置,可以用 拓扑排序 + d p 拓扑排序 + dp 拓扑排序+dp 来求解。先正向拓扑排序求出 L L L:对于边 (𝑢, 𝑣),做转移 L v = max ⁡ ( L v , L u + 1 ) L_v = \max(L_v, L_u + 1) Lv=max(Lv,Lu+1),然后反向拓扑排序求出 R R R:对于边 (𝑢, 𝑣),做转移 R u = min ⁡ ( R u , R v − 1 ) R_u = \min(R_u, R_v − 1) Ru=min(Ru,Rv1)

​ 于是转化为如下问题:给定 𝑘 个区间和 𝑘 个互不相同的数,我们需要给每个数匹配一个包含它的区间,此外每个区间匹配的数还要满足一些拓扑关系(形如 P u < P v P_u<P_v Pu<Pv的约束)。如果暂时不考虑拓扑关系的话是一个经典问题,存在一个简单的贪心做法:从小到大枚举所有数,当枚举到 𝑥 时,从所有左端点 ≤ x ≤ x x 且还没被匹配的区间中,选择右端点最小的那个匹配给 𝑥,这个过程用优先队列优化。

​ 然后分析一下区间的性质:如果存在边 (𝑢, 𝑣),根据转移方程可得 L u + 1 ≤ L v , R u + 1 ≤ R v L_u + 1 ≤ L_v,R_u + 1 ≤ R_v Lu+1LvRu+1Rv,按照上述贪心做法, [ L u , R u ] [L_u, R_u] [Lu,Ru] 一定比 [ L v , R v ] [L_v, R_v] [Lv,Rv] 更早被匹配到,即一定满足 P u < P v P_u < P_v Pu<Pv。所以直接贪心求出来的就是原问题的合法解,如果贪心无解则原问题一定无解。 总时间复杂度 O ( m + n log ⁡ n ) O(m + n \log n) O(m+nlogn)

启发:对于 拓扑排序 + D P 拓扑排序+DP 拓扑排序+DP不仅可以用来就 D A G DAG DAG的最短路,对于这种可行解的构造问题,我们可以用 拓扑排序 + D P 拓扑排序+DP 拓扑排序+DP将答案优化到一个区间内,此时可能会将问题转化为另一个较简单直白的问题,如本题转化为了一个简单的贪心问题。当然,对于一些构造题,可能对于一些限制条件,使得答案符合一种拓扑关系,即可考虑使用拓扑排序限制合法答案,或直接进行构造。

ac代码参考:[Submission #256911647 - Codeforces

搭平台建图
e g 1 : eg1: eg1:Meeting (nowcoder.com)(UVALive7250 / hduoj 5521,15年沈阳站银牌题)

题目大意

Meeting

输入描述,注意数据范围

The first line contains an integer T ( 1 ≤ T ≤ 6 ) T (1≤T≤6) T(1T6), the number of test cases. Then T T T test cases
follow.

The first line of input contains n n n and m m m. 2 ≤ n ≤ 1 0 5 2≤n≤10^5 2n105. The following m lines describe the sets E i ( 1 ≤ i ≤ m ) E_i (1≤i≤m) Ei(1im). Each line will contain two integers t i ( 1 ≤ t i ≤ 1 0 9 ) t_i(1≤t_i≤10^9) ti(1ti109) and S i ( S i > 0 ) S_i (S_i>0) Si(Si>0) firstly. Then S i S_i Si integer follows which are the labels of blocks in E i E_i Ei. It is guaranteed that ∑ S i ≤ 1 0 6 ∑S_i≤10^6 Si106​.

e g 2 : eg2: eg2: Problem - 1941G - Codeforces(搭平台跑最短路,相当于把图集合化,cf rating 2000)

题目大意

Building bridges did not help Bernard, and he continued to be late everywhere. Then Rudolf decided to teach him how to use the subway.

Rudolf depicted the subway map as an undirected connected graph, without self-loops, where the vertices represent stations. There is at most one edge between any pair of vertices.

Two vertices are connected by an edge if it is possible to travel directly between the corresponding stations, bypassing other stations. The subway in the city where Rudolf and Bernard live has a color notation. This means that any edge between stations has a specific color. Edges of a specific color together form a subway line. A subway line cannot contain unconnected edges and forms a connected subgraph of the given subway graph.

An example of the subway map is shown in the figure.

Rudolf claims that the route will be optimal if it passes through the minimum number of subway lines. Help Bernard determine this minimum number for the given departure and destination stations.

最小树形图
e g 1 : eg1: eg1: [1036-[ S C O I 2012 SCOI2012 SCOI2012] 滑雪与时间胶囊](https://ac.nowcoder.com/acm/contest/26077/1036)

题目大意

滑雪与时间胶囊

模型综合运用练习题

回家的路 (洛谷 P 3831 P3831 P3831 [ S H O I 2012 ] [SHOI2012] [SHOI2012]

Problem - G - Codeforces( 2021 C C P C 2021CCPC 2021CCPC哈尔滨,银牌题,最短路+状压期望DP)

Problem - B - Codeforces( 2023 I C P C 2023ICPC 2023ICPC山东省赛,金牌题,拓扑排序,优先队列优化)

Problem - J - Codeforces( 2023 I C P C 2023ICPC 2023ICPC山东省赛,金牌题,图论背景+位运算,类似于数位DP的思想)


http://www.ppmy.cn/devtools/41114.html

相关文章

vty、带内/带外管理、带内/带外ip简介

1、vty是什么&#xff1f; (virtual teletype)虚拟电传打字机 漫谈VTY (qq.com) 视频链接&#xff1a;使用20世纪30年代的电传打字机作为Linux系统的终端 https://hackaday.com/2020/04/15/logging-into-linux-with-a-1930s-teletype/ 2、console端口和vty端口的区别&#xf…

掌握QPainter:Qt中的绘图艺术

目录标题 1. QPainter概述2. 创建QPainter对象3. 绘制基本图形4. 绘制文本5. 绘制图像6. 使用画笔&#xff08;QPen&#xff09;7. 使用画刷&#xff08;QBrush&#xff09;8. 图形变换9. 抗锯齿与优化10. 实例代码与解析11. 总结 在Qt的世界里&#xff0c;QPainter是一位多才多…

vue-img-cutter 图片裁剪详解

前言&#xff1a;vue-img-cutter 文档&#xff0c;本文档主要讲解插件在 vue3 中使用。 一&#xff1a;安装依赖 npm install vue-img-cutter # or yarn add vue-img-cutter # or pnpm add vue-img-cutter 二&#xff1a;构建 components/ImgCutter.vue 组件 <script se…

MySQL_DDL语句

1.Data类临时数据的弊端 我们之前在将ServletJSP配合处理请求的过程中 数据库起到一个存取数据的作用 但是我们之前的案例中 数据是在Data类中临时定义的 并不是从数据库中获取的 这样做是不好的 因为每一次服务器关闭之后 那么部署在其上的类也会随着卸载 紧接着和类相挂钩的静…

Milvus入门初探

引言 Milvus 是一款开源的向量数据库&#xff0c;专为处理向量搜索任务而设计。它支持多种类型的向量&#xff0c;如浮点向量、二进制向量等&#xff0c;并且可以处理大规模的向量数据。Milvus 在 AI 应用中非常流行&#xff0c;尤其是在需要执行相似性搜索或最近邻搜索的场景…

python 基础(笔记)

文章目录 1. 环境安装2. 第一个程序 hello word3. 注释4. 变量4.1 变量声明4.2 命名规则4.3 命名规范 5. 运算符5.1 算术运算符5.2 赋值运算符5.3 比较运算符 6. 数据类型6.1 数据类型6.2 数据类型的转换 7. 字符串操作7.1 字符串定义的几种方式7.2 字符串拼接7.3 字符串格式化…

Google Gemma 2B 微调实战(IT科技新闻标题生成)

本文我将使用 Google 的 Gemma-2b 模型来微调一个基于IT科技新闻正文来生成对应标题的模型。并且我将介绍如何使用高度集成的训练框架来进行快速微调。 开始前 为了尽可能简化整个流程,我将使用 linux-cn 数据集[1]作为本次训练任务的训练数据。 模型选择使用 Gemma-2b[2],…

个人模拟面试java2

文章目录 SQL如何进行优化数据库性能调优1. 性能评估2. 确定调优目标 3. 监控和分析4. 硬件和配置调优5. 数据库设计和模式优化6. 查询优化7. 并发控制8. 缓存策略9. 定期维护10. 监控和持续调优 Redis支持哪些数据类型&#xff1f;Redis如何实现持久化&#xff1f;如何解决Red…