问题描述

We have a graph with N vertices, numbered 0 through N−1. Edges are yet to be added.

We will process Q queries to add edges. In the i-th (1≦iQ) query, three integers A**i,B**i and C**i will be given, and we will add infinitely many edges to the graph as follows:

  • The two vertices numbered A**i and B**i will be connected by an edge with a weight of C**i.
  • The two vertices numbered B**i and A**i+1 will be connected by an edge with a weight of C**i+1.
  • The two vertices numbered A**i+1 and B**i+1 will be connected by an edge with a weight of C**i+2.
  • The two vertices numbered B**i+1 and A**i+2 will be connected by an edge with a weight of C**i+3.
  • The two vertices numbered A**i+2 and B**i+2 will be connected by an edge with a weight of C**i+4.
  • The two vertices numbered B**i+2 and A**i+3 will be connected by an edge with a weight of C**i+5.
  • The two vertices numbered A**i+3 and B**i+3 will be connected by an edge with a weight of C**i+6.
  • ...

Here, consider the indices of the vertices modulo N. For example, the vertice numbered N is the one numbered 0, and the vertice numbered 2N−1 is the one numbered N−1.

输入格式

The input is given from Standard Input in the following format:

N Q

A1 B1 C1

A2 B2 C2

:

AQ BQ CQ

输出格式

Print the total weight of the edges contained in a minimum spanning tree of the graph.

样例输入

7 1

5 2 1

样例输出

21

解析

考虑等价替换。对于一次加边操作,由于 (x,y) 的权值比 (y,x+1) 的权值小,考虑kruskal 的过程,在考虑边 (y,x+1,z+1) 的时候,x,y 一定已在一个连通块里。于是,我们可以将 (y,x+1,z+1) 等价替换为 (x,x+1,z+1)。同理,(x+1,y+1,z+2) 可替换为(y,y+1,z+2)。经过这样的替换后,我们发现,一次加边操作会使图上产生两个环,以a为起点的环的路径为c+1,c+3,c+5,......,以b为起点的环的路径为c+2,c+4,c+6,......,同时还有(a,b,c)的边。设\(f[i]\)表示从i-1到i之间的边中最小的,我们可以用类似于最大前缀的方式,求出每一个f:

\[f[i+1]=max(f[i+1],f[i])
\]

注意\(f[n+1]=f[1]\),因为在更新一圈后可能还有可以被后来的点更新的,所以我们要更新两圈。

最后,图中只剩下q+n条边,用Kruskal求最大生成树即可。

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define int long long
#define N 400002
using namespace std;
struct edge{
int u,v,w;
}e[N];
int n,q,i,f[N],num,fa[N];
int read()
{
char c=getchar();
int w=0;
while(c<'0'||c>'9') c=getchar();
while(c<='9'&&c>='0'){
w=w*10+c-'0';
c=getchar();
}
return w;
}
int my_comp(const edge &x,const edge &y)
{
return x.w<y.w;
}
int find(int x)
{
if(x!=fa[x]) fa[x]=find(fa[x]);
return fa[x];
}
int Kruscal()
{
for(int i=1;i<=n;i++) fa[i]=i;
sort(e+1,e+num+1,my_comp);
int cnt=n,ans=0;
for(int i=1;i<=num;i++){
int f1=find(e[i].u),f2=find(e[i].v);
if(f1!=f2){
fa[f1]=f2;
cnt--;
ans+=e[i].w;
}
}
return ans;
}
signed main()
{
memset(f,0x3f,sizeof(f));
n=read();q=read();
for(i=1;i<=q;i++){
int a,b,c;
a=read()+1;b=read()+1;c=read();
e[++num]=(edge){a,b,c};
f[a]=min(f[a],c+1);
f[b]=min(f[b],c+2);
}
bool flag=0;
for(i=1;i<=n;i++){
f[i%n+1]=min(f[i]+2,f[i%n+1]);
if(i==n&&!flag) i=0,flag=1;
}
for(i=1;i<=n;i++) e[++num]=(edge){i,i%n+1,f[i]};
cout<<Kruscal()<<endl;
return 0;
}

Atcoder2134 Zigzag MST的更多相关文章

  1. [题解] [AtCoder2134] Zigzag MST

    题面 题解 考虑kruscal的过程 对于三个点\(x, y, x + 1\), 我们可以将\((x, y, z), (y, x + 1, z + 1)\)看做\((x, y, z), (x, x + ...

  2. 【AtCoder2134】ZigZag MST(最小生成树)

    [AtCoder2134]ZigZag MST(最小生成树) 题面 洛谷 AtCoder 题解 这题就很鬼畜.. 既然每次连边,连出来的边的权值是递增的,所以拿个线段树xjb维护一下就可以做了.那么意 ...

  3. 【题解】 AT2134 Zigzag MST

    [题解]AT2134 Zigzag MST 一道MST好题 \(Anson\)有云: 要么是减少边的数量. 要么是改变连接边的方式. 那么如何减少边的数量呢?很简单,把所有不可能对答案产生贡献的边去掉 ...

  4. AT2134 Zigzag MST

    题面 题解 这个题目主要是连边很奇怪,但是我们可以发现一个性质:权值是递增的. 于是像下图的连边:(加边方式为\((A_1, B_1, 1)\)) 其实可以等价于如下连边: 于是我们将其变成了在环上连 ...

  5. AT2134 Zigzag MST 最小生成树

    正解:最小生成树 解题报告: 先放下传送门QAQ 然后这题,首先可以发现这神奇的连边方式真是令人头大,,,显然要考虑转化掉QAQ 大概看一下可以发现点对的规律是,左边++,交换位置,再仔细想下,就每个 ...

  6. 题解 [AT2134] Zigzag MST

    题面 解析 我们先考虑一下加一条边(x,y,z)会成什么亚子: (还有很多边不画了...) 然后我们把这个图单独拿出来: 我们可以发现,对于最小生成树的贡献, 它是等价于下面这张图的(因为连通性一样) ...

  7. Atcoder CODE FESTIVAL 2016 Final G - Zigzag MST[最小生成树]

    题意:$n$个点,$q$次建边,每次建边选定$x,y$,权值$c$,然后接着$(y,x+1,c+1),(x+1,y+1,c+2),(y+1,x+2,c+3),(x+2,y+2,c+4)\dots$(画 ...

  8. [LeetCode] Zigzag Iterator 之字形迭代器

    Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...

  9. [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

随机推荐

  1. 源码编译安装Apache/2.4.37-------踩了无数坑,重装了十几次服务器才会的,不容易啊!

    1.先进入/usr/local/中创建三个文件夹 apr apr-util apache cd /usr/local目录 mkdir apr mkdir apr-util mkdir apache 2 ...

  2. Django学习之数据库与ORM

    二.ORM表模型 表(模型)的创建: 1.ORM之增(create.save) 一对多(ForeignKey): 多对多(ManyToManyField()): 2.ORM之删(delete) 3.O ...

  3. 如何吸引用户打开自己发送的EDM邮件

    一般来说,邮件发送到用户的收件箱,但用户不一定会阅读.因为每个用户收到的邮件都很多.那么,究竟应该如何吸引读者打开自己的EDM邮件呢? 只有当用户认识并信任发件人的时候,此时邮件的打开率是最高的,可以 ...

  4. Delphi XE2 之 FireMonkey 入门(30) - 数据绑定: TBindingsList: TBindExpression 的 OnAssigningValue 事件

    Delphi XE2 之 FireMonkey 入门(30) - 数据绑定: TBindingsList: TBindExpression 的 OnAssigningValue 事件 表达式中的函数有 ...

  5. Python高效率遍历文件夹寻找重复文件

    前言 为什么要写这篇文章呢...主要还是业务中有个需求,遍历一个将近200w数据的文件夹,大部分还都是视频文件那种,但是这玩意用的次数还不多,做文件夹index也不是很ok,所以写了一个脚本来处理这个 ...

  6. 一个简单的INI读写文件类,基于C++的模板编程实现,使用超级方便

    GITHUB链接:https://github.com/brofield/simpleini 主体代码: /** @mainpage <table> <tr><th> ...

  7. 使用NHibernate连接MySQL数据库及增删改查

    学习资料 http://www.sikiedu.com/course/51/task/891/show https://www.codeproject.com/Articles/26123/NHibe ...

  8. PYTHON2.7之前需要独立安装pip

    如果python2版本是>=2.7.9, python3版本是>=3.4, pip已将一起随python安装成功了. 对于Python 2.6,你需要更旧setuptools.适用于Pyt ...

  9. java对象的方法属性和代码块的加载顺序

    1.静态变量 2.静态代码块 3.局部代码块 4.构造函数 5.普通代码块 6.静态方法 7.普通方法 8.普通属性 for example: package com.JavaTest2; publi ...

  10. xmake新增智能代码扫描编译模式

    最近给xmake增加了一个新特性,对于一份工程源码,可以不用编写makefile,也不用编写各种make相关的工程描述文件(例如:xmake.lua,makefile.am, cmakelist.tx ...