博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AT2134 Zigzag MST
阅读量:4319 次
发布时间:2019-06-06

本文共 1735 字,大约阅读时间需要 5 分钟。

题解

这个题目主要是连边很奇怪,但是我们可以发现一个性质:权值是递增的。

于是像下图的连边:(加边方式为\((A_1, B_1, 1)\)

5c78e6cfd3785.png

其实可以等价于如下连边:

5c78e7619a6ee.png

于是我们将其变成了在环上连边。

在环上连边有一点好,就是可以知道边\((i,i+1)\)的边权最小值。

于是将这些边和之前的三元组\((a, b, c)\)放到边集中去,跑kruskal即可。

代码

#include
#include
#include
#include
#define RG register#define int long longinline int read(){ int data = 0, w = 1; char ch = getchar(); while(ch != '-' && (!isdigit(ch))) ch = getchar(); if(ch == '-') w = -1, ch = getchar(); while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar(); return data * w;}const int maxn(2e5 + 10);struct edge { int x, y, w; } e[maxn << 2];inline int cmp(const edge &lhs, const edge &rhs) { return lhs.w < rhs.w; }int dis[maxn], n, Q, e_num, fa[maxn], ans;int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }template
inline void chkmin(T &x, const T &y) { if(y < x) x = y; }inline void add_edge(int x, int y, int w) { e[++e_num] = (edge) {x, y, w}; }signed main(){ n = read(), Q = read(); memset(dis, 0x3f, sizeof dis); for(RG int i = 1, a, b, c; i <= Q; i++) a = read(), b = read(), c = read(), add_edge(a, b, c), chkmin(dis[a], c + 1), chkmin(dis[b], c + 2); for(RG int i = 0; i < n; i++) chkmin(dis[i], dis[(i - 1 + n) % n] + 2); for(RG int i = 0; i < n; i++) chkmin(dis[i], dis[(i - 1 + n) % n] + 2); for(RG int i = 0; i < n; i++) add_edge(i, (i + 1) % n, dis[i]), fa[i] = i; std::sort(e + 1, e + e_num + 1, cmp); for(RG int i = 1; i <= e_num; i++) { if(find(e[i].x) == find(e[i].y)) continue; fa[find(e[i].x)] = find(e[i].y); ans += e[i].w; } printf("%lld\n", ans); return 0;}

转载于:https://www.cnblogs.com/cj-xxz/p/10456973.html

你可能感兴趣的文章
为什么需要配置环境变量
查看>>
$i++,++$i
查看>>
Knockout学习笔记之一
查看>>
Linux学习路径
查看>>
嘉奇科技
查看>>
tomcat 监听地址
查看>>
WdatePicker 日期控件- 功能及示例
查看>>
Go语言实战 - 我需要站内搜索
查看>>
软中断
查看>>
Fail to start neutron-server
查看>>
景安快运挂在磁盘-支持宝塔
查看>>
RubyMine 5.4 发布,支持 Rails 4
查看>>
强大的C# Expression在一个函数求导问题中的简单运用
查看>>
word中交叉引用不能更新的解决方法
查看>>
太过于依赖.NET默认的序列化机制
查看>>
Alpine Linux 2.5.0 发布,面向路由器、防火墙
查看>>
quotatool 1.6.2 发布,磁盘配额管理工具
查看>>
CVSps 3.8 发布,CVS 资料库更改收集
查看>>
构造函数,析构函数是否为虚函数
查看>>
Django缓存使用方法
查看>>