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;
    }
    


  • 相关阅读:
    Oracle数据库的经典问题 snapshot too old是什么原因引起的
    在服务器上排除问题的头五分钟
    MySQL的redo log结构和SQL Server的log结构对比
    MySQL优化---DBA对MySQL优化的一些总结
    事务分类
    扩展HT for Web之HTML5表格组件的Renderer和Editor
    iOS平台快速发布HT for Web拓扑图应用
    HT for Web的HTML5树组件延迟加载技术实现
    Zip 压缩、解压技术在 HTML5 浏览器中的应用
    百度地图、ECharts整合HT for Web网络拓扑图应用
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387439.html
Copyright © 2011-2022 走看看