hdu 6090(一)

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 ∑(i=1 ~n)∑(j=1~n)dist(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≤10^6,1≤m≤10^12).

Output
For each testcase, print a single line with a single number – the answer.

Sample Input

1
4 5

Sample Output

14

题目大意
现在你有N个点,M条无向边,每条边可以随意连接任意两个点。每条边的边长为1,dis(i,j)表示 i 到 j 的最短路径,如果i,j之间没有路径可以到达,那么dis(i,j)=n。请找到一种连边方式,使得所有点对(i,j)[ i != j ]之间的最短路径之和最小。

tips:菊花图,就是整张图长得跟菊花一样,有一个点在中心,然后其他点都有一条直接与它相连的边。
菊花图的花瓣,除去中心点以外的所有点。

题解
如果m>=n-1,那就先建一张菊花图,然后剩下的边用来连接花瓣上面的边….
如果m

#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#define pa pair<long long , int >
#define LiangJiaJun main
using namespace std;
int T;
long long n,m,ans;
int w33ha(){
    ans=0;
    scanf("%I64d%I64d",&n,&m);
    if(m >= n - 1){
       m -= (n - 1);
       ans += 2*(n - 1);
       ans += 2*(n - 1)*(n - 2);
       if( m > 0){
           ans -= min(2*m ,( n - 1 )*( n - 2 ));
       }
    }
    else{
       long long gf = n - (m + 1);
       ans += 2*m ;
       ans += 2*(m - 1) * m;
       ans += 2 * n * gf * (m + 1);
       ans += gf * (gf - 1) * n;
    }
    printf("%I64d\n",ans);
    return 0;
}
int LiangJiaJun (){
    scanf("%d",&T);
    while(T--)w33ha();
    return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值