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 ...
随机推荐
- saltstack进阶
查看minion端的文件内容 [root@linux-node2 ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 1 ...
- [计算机网络-数据链路层] CSMA、CSMA/CA、CSMA/CD详解
1.CSMA(载波侦听多路访问协议) CSMA 当其他节点检测到信道被占用时不发送数据.但是当数据发送完后其他节点同时检测到信道为空闲,之后又在同一时刻发送数据,可能再次产生冲突. 2.CSMA/CD ...
- Java知识点整理(三)
如何设计出高可用的分布式架构 分布式架构 CDN简介 分布式缓存和本地缓存区别 高并发场景常用技术解决方案 JVM优化示例 Docker和JVM区别 Java开发人员需要注意的五大Docker误区 D ...
- FZU2125_简单的等式
题目想到了就是一个水题. 提示一下,S(x,m)是一个很小的数.(不超过100) 这样直接枚举这个数,然后求方程的整数解,并且判断是否满足条件即可. ——————(一开始还用Pollard大整数分解+ ...
- HDU 4758——Walk Through Squares——2013 ACM/ICPC Asia Regional Nanjing Online
与其说这是一次重温AC自动机+dp,倒不如说这是个坑,而且把队友给深坑了. 这个题目都没A得出来,我只觉得我以前的AC自动机的题目都白刷了——深坑啊. 题目的意思是给你两个串,每个串只含有R或者D,要 ...
- bzoj5090[lydsy11月赛]组题
裸的01分数规划,二分答案,没了. #include<cstdio> #include<algorithm> using namespace std; const int ma ...
- CODE FESTIVAL 2016 qualA Grid and Integers
划年代久远的水 题意 有一个R*C的棋盘,要求在每个格子上填一个非负数,使得对任意一个2*2的正方形区域,左上角和右下角的数字之和等于左下角和右上角的数字之和.有一些格子已经被填上了数字,问现在能否满 ...
- 【loj6307】「雅礼国庆 2017 Day1」Clique 贪心
题目描述 数轴上有 $n$ 个点,第 $i$ 个点的坐标为 $x_i$ 权值为 $w_i$ .两个点 $i,j$ 之间存在一条边当且仅当 $|x_i−x_j|\le w_i+w_j$ . 你需要求出这 ...
- 苹果手机连wifi跳不出来登录网页解决办法
1.点开要连接wifi后面的小叹号“!”,打开“自动登录” 2.打开设置,关闭SAFARI的“阻止弹窗” 3.重新连接wifi
- Expect the Expected UVA - 11427(概率dp)
题意: 每天晚上你都玩纸牌,如果第一次就赢了,就高高兴兴的去睡觉,如果输了就继续玩.假如每盘游戏你获胜的概率都为p,每盘游戏输赢独立.如果当晚你获胜的局数的比例严格大于p时才停止,而且每天晚上最多只能 ...