Codeforces Round 977 (Div. 2)E1 Digital Village (Easy Version)(Floyd,贪心)

embedded/2024/10/9 3:31:10/

题目链接

Codeforces Round 977 (Div. 2)E1 Digital Village (Easy Version)

思路

首先,我们注意到 n n n的最大值只有 400 400 400

因此,我们可以先用 F l o y d Floyd Floyd算法预处理出任意两座城市之间的最大延迟时间。

之后,我们通过在线操作,每次贪心地选出最优的一个城市,并不断更新答案。

即,我们先选出 k = 1 k=1 k=1时的最优解,之后从剩下的点里面挑出一个能够使 k = 2 k=2 k=2时最优的点,以此类推。

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 4e2 + 5;
const int mod = 998244353;
const int inf = 0x3f3f3f3f3f3f3f3f;
int n, m, p;
void solve()
{cin >> n >> m >> p;vector<int>city(p);for (int &val : city){cin >> val;}vector<vector<int>>dist(n + 1, vector<int>(n + 1, inf));for (int i = 1, u, v, w; i <= m; i++){cin >> u >> v >> w;dist[u][v] = min(dist[u][v], w);dist[v][u] = min(dist[v][u], w);}for (int i = 1; i <= n; i++)dist[i][i] = 0;for (int k = 1; k <= n; k++){for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){dist[i][j] = min(dist[i][j], max(dist[i][k], dist[k][j]));}}}vector<int>ans(n + 1);int sum = inf, idx = -1;for (auto u : city){int res = 0;for (auto v : city){res += dist[u][v];}if (sum >= res){idx = u;sum = res;for (auto v : city){ans[v] = dist[u][v];}}}set<int>st;st.insert(idx);cout << sum << " ";for (int i = 2; i <= n; i++){if (sum == 0){cout << sum << " ";continue;}int maxx = 0;idx = -1;for (auto u : city){if (st.count(u)) continue;int res = 0;for (auto v : city){if (st.count(v)) continue;if (ans[v] > dist[u][v]){res += ans[v] - dist[u][v];}}if (maxx <= res){maxx = res;idx = u;}};st.insert(idx);sum -= maxx;for (auto v : city){if (st.count(v)) continue;ans[v] = min(ans[v], dist[idx][v]);}cout << sum << " ";}cout << endl;
}
signed main()
{ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int test = 1;cin >> test;for (int i = 1; i <= test; i++){solve();}return 0;
}

http://www.ppmy.cn/embedded/124306.html

相关文章

足球青训后台管理系统:Spring Boot实现指南

2 相关技术简介 2.1 Java技术 Java是一门伟大的纯面向对象的编程语言和编程语言。同时&#xff0c;它还是Java语言从嵌入式开发到企业级开发的平台。Java凭借其一次编译&#xff0c;任何地方执行的优点&#xff0c;使得盛行的web应用程序有大量的Java编译&#xff0c;很好地支…

CSS中的class与id

定义 class&#xff08;类&#xff09; 在 CSS 中&#xff0c;class是一种用于为 HTML 元素分组的属性。多个 HTML 元素可以共享同一个class名称。例如&#xff1a; 在 HTML 中&#xff0c;可以有多个<div>元素使用同一个class&#xff0c;像<div class "box&qu…

深度学习在计算机视觉中的应用

引言 深度学习的兴起标志着计算机视觉领域的革命&#xff0c;尤其是在图像识别、物体检测、图像分割等任务中&#xff0c;深度学习展现了无与伦比的性能。随着技术的不断发展&#xff0c;尤其是2024年&#xff0c;深度学习在计算机视觉中的应用范围和技术深度都得到了显著提升…

扣子创建的智能体,发布成api,使用java进行调用

扣子平台的api是我见过最不友好的&#xff0c;折腾了半天才调通。基础版和专业版&#xff0c;建议还是选择专业版吧&#xff08;因为相同的问题会得到不同的结果&#xff09; public static void main(String[] args) { String prompt ““输入下面的信息&#xff1a;我路过街…

ubuntu 安装kvm 创建windos虚拟机

查看主机服务器是否能虚拟化 egrep -c (vmx|svm) /proc/cpuinfo 如果输出的数字大于 0&#xff0c;则表示系统支持硬件虚拟化 配置网络&#xff08;这里要新建一个网桥&#xff0c;与本机的物理网卡enp5s0f0绑定&#xff0c;通过这个网桥连接创建的虚拟机&#xff09; netwo…

安全服务面试总结

154.mysql 安全要如何做&#xff1f; Mysql 账户权限安全 第 61 页 共 152 页 Mysql 数据的网络安全配置 密码策略安全 Mysql 日志 Mysql 数据库服务所在主机安全配置部署 SQL 注入检测、防御模块 mysqld 安全相关启动选项 mysql 备份策略 155.sqlserver public 权…

基于SSH的酒店管理系统的设计与实现 (含源码+sql+视频导入教程+文档+PPT)

&#x1f449;文末查看项目功能视频演示获取源码sql脚本视频导入教程视频 1 、功能描述 基于SSH的酒店管理系统拥有三种角色 管理员&#xff1a;用户管理、房间分类管理、房间信息管理、开房管理、退房管理、开房和预订记录查询等 前台&#xff1a;房间分类管理、房间信息管…

git(1) -- 环境配置

1. 配置文件 编辑~/.gitconfig文件&#xff0c;内容如下。 [user]email xflming163.comname xflm [core]editor vim [color]diff autostatus autobranch autoui true [commit]template /home/xflm/configuser/git-commit.template [diff]tool bc4 [difftool]prompt …