您的位置:首页 > 其它

2017多校五 1006题 hdu 6090 Rikka with Graph 贪心 构造

2017-08-09 11:23 393 查看
题目链接

题意:

n 个点之间要加 <= m 条边使得 ∑ni=1∑nj=1dist(i,j)
最小,若两点不连通则 dist 为 n

官方题解:

考虑贪心地一条一条边添加进去。

当 m
\leq n-1m≤n−1 时,我们需要最小化距离为 nn 的点对数,所以肯定是连出一个大小为 m+1m+1 的联通块,剩下的点都是孤立点。在这个联通块中,为了最小化内部的距离和,肯定是连成一个菊花的形状,即一个点和剩下所有点直接相邻。

当 m
> n-1m>n−1 时,肯定先用最开始 n-1n−1 条边连成一个菊花,这时任意两点之间距离的最大值是 22。因此剩下的每一条边唯一的作用就是将一对点的距离缩减为 11。

这样我们就能知道了最终图的形状了,稍加计算就能得到答案。要注意 mm 有可能大于 \frac{n(n-1)}{2}​2​​n(n−1)​​。
Code:

#include <bits/stdc++.h>
#define maxn 10010
using namespace std;
typedef long long LL;
LL ans[110], b[maxn];
void work() {
LL n, m;
scanf("%lld%lld", &n, &m);
LL lim1 = n - 1, add = (n - 1) * (n - 2) >> 1;
LL lim2 = lim1 + add, tot1 = (n - 1) * (n - 1) * 2;
if (m >= lim2) { printf("%lld\n", n * (n - 1)); return; }
if (m >= lim1) { printf("%lld\n", tot1 - 2 * (m - lim1)); return; }
LL nn = m + 1;
LL tot0 = (nn - 1) * (nn - 1) * 2 + ((n - nn) * nn * 2 + (n - nn) * (n - nn - 1)) *n;
printf("%lld\n", tot0);
}
int main() {
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: