题目来源:http://acm.hdu.edu.cn/.php?pid=6090
题意
有n个点,相互之间并没有连接,给出m条边,至于这m条边是哪个点和哪个点相连都是无所谓的,只要最后任意两点之间的距离的总和最小就可以,其中,i到j的距离的计算:若是i和j之间没有联通,那么距离为n,若已连通,就是路径经过的边的条数,输出最小值。
思路
这样的连接方式使得两点之间若是存在边,最大不会超过2。
代码
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long LL;
const double eps=1e-6;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
LL n,m;
scanf("%lld%lld",&n,&m);
if(m<n-1)
{
printf("%lld\n",(n*(n*n-n-m*m-m)/2+m*m)*2);
}
else
{
if(m>n*(n-1)/2)
m=n*(n-1)/2;
LL k=m-n+1;
printf("%lld\n",2*(n*(n-1)-m));
}
}
}