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=1∑nj=1dist(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≤106,1≤m≤1012).
 

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的更多相关文章

  1. 2017杭电多校第六场1008 Kirinriki

    传送门 Kirinriki Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  2. 2017杭电多校第五场11Rikka with Competition

    Rikka with Competition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  3. 2017杭电多校第五场Rikka with Subset

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. 2017杭电多校第六场1011Classes

    传送门 Classes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  5. 2017杭电多校第六场03Inversion

    传送门 Inversion Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  6. 2017杭电多校第七场1011Kolakoski

    Kolakoski Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Tota ...

  7. 2017杭电多校第七场1005Euler theorem

    Euler theorem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) ...

  8. 2017杭电ACM集训队单人排位赛 - 6

    2017杭电ACM集训队单人排位赛 - 6 排名 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 59 1 X X 1 1 X X 0 1 ...

  9. 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)

    以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...

随机推荐

  1. 【ZJOI2017 Round1练习】D8T1 mushroom(点分治)

    题意: 思路: num[a[u]]表示存在a[u]这个颜色且终点在u子树中的链长总和 ans[i]表示以当前的u为根,前面的子树对i的贡献之和 ..]of longint; size,f,ans,su ...

  2. 调用系统相机拍照,保存照片,调用系统裁剪API对照片处理,显示裁剪之后的照片

    package com.pingyijinren.test; import android.annotation.TargetApi; import android.app.Notification; ...

  3. CSS font-family 属性

    CSS font-family 属性 实例 为段落设置字体: p { font-family:"Times New Roman",Georgia,Serif; } 亲自试一试 浏览 ...

  4. MongoDB小结26 - 地理空间索引

    现在有一种查询变得越来越流行(尤其是移动设备):找到离当前位置最近的N个场所. MongoDB专为平面坐标查询做了专门的索引,称为地理空间索引. 同样需要用ensureIndex创建,不过,参数是两个 ...

  5. MapReduce获取分片数目

    问题 MapReduce Application中mapper的数目和分片的数目是一样的,可是分片数目和什么有关呢? 默认情况下.分片和输入文件的分块数是相等的.也不全然相等,假设block size ...

  6. 滚动载入server端内容——样例

    网页代码例如以下 <!doctype html> <html> <head> <meta charset="utf-8"> < ...

  7. DOM 事件类

    DOM事件的级别: DOM级别一共可以分4个级别:DOM0级.DOM1级. DOM2级 .DOM3级.而DOM事件级别分为3个级别:DOM0级事件处理.DOM2级事件处理.DOM3级事件处理 1. D ...

  8. 复合页( Compound Page )

    复合页(Compound Page)就是将物理上连续的两个或多个页看成一个      独立的大页,它能够用来创建hugetlbfs中使用的大页(hugepage).      也能够用来创建透明大页( ...

  9. 函数计算 触发式计算 日志 MP3 图片 合成视频

    函数计算 触发式计算   日志  MP3 图片 合成视频 [start_time]:20120511 06:59:11 [20120511 06:59:11_0.4950568322522534]: ...

  10. Hadoop安装—— WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platf

    今天在安装hadoop完成测试创建用户目录失败在网上找到了原因记录一下原文地址 http://blog.csdn.net/l1028386804/article/details/51538611 配置 ...