我是靠谱客的博主 如意故事,最近开发中收集的这篇文章主要介绍2017杭电多校06Rikka with Graph Rikka with Graph,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Rikka with Graph

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


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=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 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(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
 

Statistic |  Submit |  Clarifications |  Back





题意:给出n个顶点,m条边,要求图的最小距离
思路:题中给出两点距离的值是经过的边数,所以尽可能的把所有的点连成菊花链;
分三种情况讨论:
1.当m<=n-1
分别计算连通点,孤立点,连通与孤立三种情况
result=(m+1-1)*(m+1-1)*2+(n-m-1)*(n-m-1)*n+(m+1)*(n-m-1)*2*n;
2.当m>n-1&&m<n*(n-1)/2
转换为边数为n*(n-1)/2的图的基础上删掉了n*(n-1)/2-m条边,每删掉一条边,距离数加上2
result=n*(n-1)+(n*(n-1)/2-m)*2;
3.当m>=n*(n-1)/2
显然此类情况图中所有的点都能直接相连,所以result=n*(n-1);


代码如下:
#include <iostream>

using namespace std;
typedef long long ll;
int main()
{
    int t;
    scanf("%d",&t);
    ll n,m;
    while(t--)
    {
        scanf("%lld%lld",&n,&m);
        ll tmp=n*(n-1)/2;
        ll result=0;
        if(m>=tmp)
        {
            result=n*(n-1);
        }
        else if(m>n-1&&m<tmp)
        {
            result=(n*(n-1))+(tmp-m)*2;
        }
        else
        {
            ll p=m+1,q=n-m-1;
            result=m*m*2+(n-m-1)*(n-m-2)*n+p*q*2*n;
        }
        cout<<result<<endl;
    }
    return 0;
}


最后

以上就是如意故事为你收集整理的2017杭电多校06Rikka with Graph Rikka with Graph的全部内容,希望文章能够帮你解决2017杭电多校06Rikka with Graph Rikka with Graph所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部