zoukankan      html  css  js  c++  java
  • 2017杭电多校06Rikka 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;
    }
    


  • 相关阅读:
    快速排序
    将指定目录下的所有子文件或子目录加载到TreeView
    导入英汉文本,用字符串切割,泛型集合存储的英汉字典
    取年月日的字符串方法
    简化的MVC-导入模板HTML,导入数据txt,用字符串方法生成JS菜单
    索引器的使用
    打开文件练习
    泛型委托
    将正则表达式转化成确定的有限自动机
    青蛙过桥
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387439.html
Copyright © 2011-2022 走看看