Floyd+二分,蓝桥杯国赛2022[环境治理]

2024-05-13 15:12

本文主要是介绍Floyd+二分,蓝桥杯国赛2022[环境治理],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

0环境治理 - 蓝桥云课 (lanqiao.cn)


二、解题报告

1、思路分析

考虑我们治理时间越长,灰尘度和越低,具有单调性

考虑 二分治理天数mid,1~n可以降低与其相连边 mid / n 点的边权

1 ~ mid % n 可以额外降低与其相连边 1点 的边权

注意边权不能低于临界值

每次更新边权跑一遍floyd

二分最小答案即可

2、复杂度

时间复杂度: O(n ^ 3 * logk)空间复杂度:O(n ^ 3)

3、代码详解

#include <bits/stdc++.h>
using i64 = long long;
const int N = 105;
int f[N][N], g[N][N], w[N][N];
int n, Q;
bool check(int x) {// 1 ~ n 增加 x / n, 1 ~ x % n 额外增加1 // 考虑O(n^2) 改边// O(n^3) 求最短路// 注意边权下限int a = x / n, b = x % n;memcpy(f, g, sizeof g);for(int i = 1; i <= n; i ++)for(int j = 1; j <= n; j ++)f[i][j] = f[j][i] = std::max(f[i][j] - a, w[i][j]);for(int i = 1; i <= b; i ++)for(int j = 1; j <= n; j ++)f[i][j] = f[j][i] = std::max(f[i][j] - 1, w[i][j]);for(int k = 1; k <= n; k ++)for(int i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) f[i][j] = std::min(f[i][j], f[i][k] + f[k][j]);i64 s = 0;for(int i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) s += f[i][j];return s <= Q;
}int main() {std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);std::cin >> n >> Q;for(int i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) std::cin >> g[i][j];int l = 0, r = 0;for(int i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) std::cin >> w[i][j], r = std::max(g[i][j] - w[i][j], r);r *= n;int ans = -1;while (l <= r) {int mid = l + r >> 1;check(mid) ? r = (ans = mid) - 1 : l = mid + 1;}std::cout << ans;return 0;
}

这篇关于Floyd+二分,蓝桥杯国赛2022[环境治理]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

uva 10099(floyd变式)

题意: 有一个导游要带着一群旅客从一个城市到达另一个城市,每个城市之间有最大的旅客流量限制。 问最少几趟能将这些旅客从一个城市搞到另一个城市。 解析: 用floyd找出最小流量中的最大边,然后次数就是   ceil(总人数 / 最大承载量 - 1),-1的意思是导游每次也要在车上。 ps.老司机哭晕在厕所 代码: #include <iostream>#includ

uva 10048(floyd变式)

题意: 求两个点之间经过的路径中最大噪声最小的值。 解析: floyd的变式,每次取g[i][k] g[k][j]中的大边与当前边g[i][j]比较,取小。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#includ

poj 1734 (floyd求最小环并打印路径)

题意: 求图中的一个最小环,并打印路径。 解析: ans 保存最小环长度。 一直wa,最后终于找到原因,inf开太大爆掉了。。。 虽然0x3f3f3f3f用memset好用,但是还是有局限性。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#incl

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

poj 3104 二分答案

题意: n件湿度为num的衣服,每秒钟自己可以蒸发掉1个湿度。 然而如果使用了暖炉,每秒可以烧掉k个湿度,但不计算蒸发了。 现在问这么多的衣服,怎么烧事件最短。 解析: 二分答案咯。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <c

poj 3258 二分最小值最大

题意: 有一些石头排成一条线,第一个和最后一个不能去掉。 其余的共可以去掉m块,要使去掉后石头间距的最小值最大。 解析: 二分石头,最小值最大。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <c

poj 2594 二分图最大独立集

题意: 求一张图的最大独立集,这题不同的地方在于,间接相邻的点也可以有一条边,所以用floyd来把间接相邻的边也连起来。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <sta

hdu 4517 floyd+记忆化搜索

题意: 有n(100)个景点,m(1000)条路,时间限制为t(300),起点s,终点e。 访问每个景点需要时间cost_i,每个景点的访问价值为value_i。 点与点之间行走需要花费的时间为g[ i ] [ j ] 。注意点间可能有多条边。 走到一个点时可以选择访问或者不访问,并且当前点的访问价值应该严格大于前一个访问的点。 现在求,从起点出发,到达终点,在时间限制内,能得到的最大