hdu 6090(二)

Rikka with Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 72    Accepted Submission(s): 51



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 betweeni and j, we make dist(i,j) equal to n.

Then, we can define the weight of the graph G (wG) as ni=1nj=1dist(i,j).

Now, Yuta has n nodes, and he wants to choose no more than m pairs of nodes (i,j)(ij) and then link edges between each pair. In this way, he can get an undirected graphG 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(1t10), the number of the testcases.

For each testcase, the first line contains two numbers n,m(1n106,1m1012).
 

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,現在讓你求m條邊構成的圖中,所有的點相連所需要的長度和最小。

解題思路:找規律,發現m在不同的範圍內,所求的答案有不同的公式表示,每兩個點之間共有n*(n-1)/2種可能如果m大於等於這個數就直接爲n*(n-1)如果少於它則一個邊一個邊的進行拆除沒拆一個邊就增加2然後到一個臨界點爲m=n-1一個點分別連其他的點可以根據上一個算出,當m小於這個數的時候就分爲孤立的點了,有一部分相連,就和上一種情況一樣然後分聯通的,孤立的,聯通的與孤立的分別計算。

  1. #include<cstdio>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int t;  
  6.     scanf("%d",&t);  
  7.     while(t--)  
  8.     {  
  9.         long long n,m,p,q;  
  10.         scanf("%lld%lld",&n,&m);  
  11.         long long ans=0;  
  12.         int flag=0;  
  13.         if(m>n*(n-1)/2)///在這個範圍內代表每兩個點之間都可以一步連到所以爲n*(n-1)  
  14.         {  
  15.             printf("%lld\n",n*(n-1));  
  16.             flag=1;  
  17.         }  
  18.         else if(m>=n-1&&m<=n*(n-1)/2)///在這個範圍內與m爲n*(n-1)/2相比每少一條邊就減少2  
  19.       {  
  20.             ans+=((n*(n-1)/2)-m)*2+n*(n-1);  
  21.         }  
  22.         else///這個範圍內分爲孤立的點與連在一起的點,分三塊計算  
  23.         {  
  24.             p=m+1;  
  25.             q=n-m-1;  
  26.             ans+=q*p*n*2+q*(q-1)*n+(p-1)*(p-1)*2;  
  27.         }  
  28.         if(flag==0)  
  29.         printf("%lld\n",ans);  
  30.     }  
  31. }  
#include<cstdio>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long n,m,p,q;
        scanf("%lld%lld",&n,&m);
        long long ans=0;
        int flag=0;
        if(m>n*(n-1)/2)///在這個範圍內代表每兩個點之間都可以一步連到所以爲n*(n-1)
        {
            printf("%lld\n",n*(n-1));
            flag=1;
        }
        else if(m>=n-1&&m<=n*(n-1)/2)///在這個範圍內與m爲n*(n-1)/2相比每少一條邊就減少2
      {
            ans+=((n*(n-1)/2)-m)*2+n*(n-1);
        }
        else///這個範圍內分爲孤立的點與連在一起的點,分三塊計算
        {
            p=m+1;
            q=n-m-1;
            ans+=q*p*n*2+q*(q-1)*n+(p-1)*(p-1)*2;
        }
        if(flag==0)
        printf("%lld\n",ans);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章