2017ACM暑期多校联合训练 - Team 5 1006 HDU 5205 Rikka with Graph (找规律)
Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
For an undirected graph G with n nodes and m edges, we can define the distance between (i,j) (dist(i,j)) as the length of the shortest path between i and j. The length of a path is equal to the number of the edges on it. Specially, if there are no path between i and j, we make dist(i,j) equal to n.
Then, we can define the weight of the graph G (wG) as ∑ni=1∑nj=1dist(i,j).
Now, Yuta has n nodes, and he wants to choose no more than m pairs of nodes (i,j)(i≠j) and then link edges between each pair. In this way, he can get an undirected graph G with n nodes and no more than m edges.
Yuta wants to know the minimal value of wG.
It is too difficult for Rikka. Can you help her?
In the sample, Yuta can choose (1,2),(1,4),(2,4),(2,3),(3,4).
Input
The first line contains a number t(1≤t≤10), the number of the testcases.
For each testcase, the first line contains two numbers n,m(1≤n≤106,1≤m≤1012).
Output
For each testcase, print a single line with a single number -- the answer.
Sample Input
1
4 5
Sample Output
14
题意:
n个点,你可以连接任意<=m条边,得到图G,WG = ∑ ∑ dist(i,j) 其中(ij都是从1到n)
dis(i,j)定义为从i到j经过的边的个数。如果从i无法到达j,则定义为n
问 怎么连边能让WG最小,最小的wg是多少
分析:
1.首先可以确定的一点肯定是边越多越好,这样才能保证尽可能多的两点直接相连,所以在不超过完全图的边的个数情况下,边的条数尽可能大,如果超过达到完全图所需要的边数即m>=n(n-1)/2,那么变成完全图,即答案是n(n-1) ,因为每一条边都要考虑两个方向
2.否则有两种情况(即没有达到完全图的情况)
(1).m>=n-1(相当于能构成一个连通图,任意两点之间可以直接或者间接的到达)
如果图恰好能组成深度为2的树(有一个点为中心(跟节点),直接连接着剩余的n-1个点),那么根节点和剩下n-1个点距离是1,剩余n-1个节点之间都能够通过根节点间接的到达,距离互相都是2,此时这棵树的WG=2*((n-1)+(2*(n-1)*(n-2))/2)
但是我们要考虑没有这么恰好的情况,组成一个连通图用n-1条边,那么剩下m-(n-1)条边当然是连在距离为2的点之间最好,那么此时这棵树的WG=2*( (n-1) + (2*(n-1)*(n-2))/2- (m-(n-1)) ) = 2*(n^2-n-m)
(2).m<n-1 (连通图都构不成,肯定存在不管直接还是间接都无法相连的边)
即无法组成连通图,那么使用m条边,m+1个点按照前一种情况组成一棵树,这棵树的WG1 = 2*(m)^2,余下的点之间都是不通的都是n。组成树的这x个点和其余n-x个点距离和是 WG2=2*n*(m+1)*(n-m-1),,未组成树的n-x个点和除自己之外每个点距离和是WG3=n*(n-m-1)*(n-m-2),故WG =WG1+WG2+WG3
代码:
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
ll n, m;
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%lld%Illd",&n,&m);
ll ans = 0;
if(m>=(n*(n-1))/2) ans = n*(n-1);///n个点的胡话,最多连接(n*(n-1))/2条边就能保证任意的两个点直接相连
else if (m >= n-1)
{
ans = 2*(n*n - n - m);
}
else
{
ans= 2*m*m+(n-m-1)*(m+1)*n*2+(n-m-1)*(n-m-2)*n;
}
printf("%lld\n",ans);
}
return 0;
}
2017ACM暑期多校联合训练 - Team 5 1006 HDU 5205 Rikka with Graph (找规律)的更多相关文章
- 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)
题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...
- 2017ACM暑期多校联合训练 - Team 7 1010 HDU 6129 Just do it (找规律)
题目链接 Problem Description There is a nonnegative integer sequence a1...n of length n. HazelFan wants ...
- 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...
- 2017ACM暑期多校联合训练 - Team 5 1001 HDU 6085 Rikka with Candies (模拟)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- 2017ACM暑期多校联合训练 - Team 2 1011 HDU 6055 Regular polygon (数学规律)
题目链接 **Problem Description On a two-dimensional plane, give you n integer points. Your task is to fi ...
- 2017ACM暑期多校联合训练 - Team 1 1006 HDU 6038 Function (排列组合)
题目链接 Problem Description You are given a permutation a from 0 to n−1 and a permutation b from 0 to m ...
- 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)
题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...
- 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)
题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...
- 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)
题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...
随机推荐
- 第七周PSP(10.27-11.03)
psp 进度条 项目 细则 总计 代码行数 0 随笔字数 0 知识点 无 累计曲线 饼图
- 【Linux 命令】- tail命令
linux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新,tail会自己主动刷新,确保你看到最新的档 ...
- 第203天:js---Array对象常用方法
1.shift:删除原数组的第一项,返回删除元素的值:如果数组为空则返回undefined //shift:删除原数组的第一项,返回删除元素的值:如果数组为空则返回undefined var arr ...
- wp开发(三)--赚取收益篇
App开发完毕了,是否有赚取收益的想法呢?下面很浅显地介绍两种常用赚取收益的方法. 一. 收费 在发布应用时,可以对应用进行定价,发布到商城之后,用户付费才可以下载,当然也可以提供试用版.收益状况可以 ...
- Eclipse中使用git提交代码,报错Testng 运行Cannot find class in classpath的解决方案
一.查找原因方式 1.点击Project——>Clear...——>Build Automatically 2.查看问题 二.报错因素 1.提交.xlsx文件 2.提交时,.xlsx文件被 ...
- python 内置函数02
1. lambda 匿名函数 lambda 参数: 返回值 #常规计算两个数相加的函数 def func(a,b): return a+b print(func(1,9)) #lambda函数 my_ ...
- 【刷题】BZOJ 5293 [Bjoi2018]求和
Description master 对树上的求和非常感兴趣.他生成了一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的k 次方和,而且每次的k 可能是不同的.此处节点深度的定义是这个节点到 ...
- 【BZOJ4943】【NOI2017】蚯蚓排队(哈希)
[BZOJ4943][NOI2017]蚯蚓排队(哈希) 题面 BZOJ 洛谷 UOJ 题解 记得去年看网络同步赛的时候是一脸懵逼的. 昨天看到\(zsy\)做了,今天就看了看.. 这不是\(Hash\ ...
- hdu5279 YJC plays Minecraft 【分治NTT】
题目链接 hdu5279 题解 给出若干个完全图,然后完全图之间首尾相连并成环,要求删边使得两点之间路径数不超过\(1\),求方案数 容易想到各个完全图是独立的,每个完全图要删成一个森林,其实就是询问 ...
- CDN公共库、前端开发常用插件一览表(VendorPluginLib)
=======================================================================================前端CDN公共库===== ...