题面

题解

考虑kruscal的过程

对于三个点\(x, y, x + 1\), 我们可以将\((x, y, z), (y, x + 1, z + 1)\)看做\((x, y, z), (x, x + 1, z + 1)\)

因为当连完\((x, y, z)\)后, \(x\)与\(y\)已经联通, 所以\((y, x + 1, z + 1)\)和\((x, x + 1, z + 1)\)是等价的

所以对于每个连边操作, 我们就变成了连一条边和一个环

考虑怎么优化环上的边的数量

对于这两条边\((x, x + 1, z + 1), (x + 1, x + 2, z + 3)\)

可以发现第二条边的权值就是第一条边的权值+2

所以我们可以用\(f[i]\)代表\(i\)到\(i \% n + 1\)中边权最小的边, 然后更新一圈, 跑一遍最小生成树即可

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#define itn int
#define reaD read
#define N 200001
using namespace std; int n, Q, f[N], cnt, fa[N];
struct edge { int from, to, cost; bool operator < (const edge &p) const { return cost < p.cost; } } e[N << 2]; inline int read()
{
int x = 0, w = 1; char c = getchar();
while(c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); }
while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * w;
} inline void adde(int u, int v, int w) { e[++cnt] = (edge) { u, v, w }; } int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } long long Kruscal()
{
sort(e + 1, e + cnt + 1);
long long ans = 0;
for(int i = 1; i <= cnt; i++)
{
int u = find(e[i].from), v = find(e[i].to), w = e[i].cost;
if(u == v) continue;
fa[u] = v; ans += w;
}
return ans;
} int main()
{
n = read(); Q = read();
for(int i = 1; i <= n; i++) fa[i] = i;
memset(f, 0x3f, sizeof(f));
while(Q--)
{
int u = read() + 1, v = read() + 1, w = reaD();
adde(u, v, w);
f[u] = min(f[u], w + 1); f[v] = min(f[v], w + 2);
}
int pos = 1;
for(int i = 2; i <= n; i++) if(f[i] < f[pos]) pos = i;
for(int i = pos; i <= pos + n - 1; i++) f[i % n + 1] = min(f[i % n + 1], f[(i - 1) % n + 1] + 2);
for(int i = 1; i <= n; i++) adde(i, i % n + 1, f[i]);
printf("%lld\n", Kruscal());
return 0;
}

最新文章

  1. ResultSet can not re-read row data for column 1.
  2. netstat相关
  3. 在VBA中新建工作簿
  4. 解决程序出现“terminate called after throwing an instance of &#39;std::bad_alloc&#39; what(): std::bad_alloc Aborted (core dumped)”的问题
  5. Ubuntu快捷键
  6. html成绩单表格
  7. [Windows驱动]流媒体驱动开发
  8. php字符串与正则表达式试题 Zend权威认证试题讲解
  9. POJ 2406 Power Strings KMP运用题解
  10. static的用法解析
  11. Android studio gradle配置
  12. Leetcode题解(23)
  13. Linux目录架构详解
  14. Linux:软件包安装
  15. Azure 门户中基于角色的访问控制入门
  16. 数据库iops的理解
  17. Xshell连接不上Linux
  18. Spring 4 官方文档学习(十)数据访问之OXM
  19. Angular入门教程二
  20. 简述Java中Http/Https请求监听方法

热门文章

  1. SIP笔记
  2. 【转】CnBlogs自定义博客样式
  3. HTML5之动画优化(requestAnimationFrame)
  4. var正在声明变量
  5. 宝塔控制面板+wordpress搭建个人网站
  6. Flask框架学习篇(一)
  7. 5.Hibernate 核心开发接口
  8. ChinaCock扫描控件介绍-使用TCCBarcodeScanner引起app闪退
  9. Use pkgsrc on ARM
  10. Bootstrap常用的自带插件