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. hdu 1787

    #include<stdio.h> #include<math.h> int huzhi(int n) {//欧拉函数公式 int i,j=n; for(i=2;i<(i ...

  2. segv & mini coredump 调研

    1. mini coredump    a. segv      http://zh.scribd.com/doc/3726406/Crash-N-Burn-Writing-Linux-applica ...

  3. Count Color POJ - 2777 线段树

    Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...

  4. Journey CodeForces - 839C

    There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can r ...

  5. IOS-Storyboard控制器切换之Modal(1)

    Modal模式是指模态切换.新开的界面会挡住之前的界面,使之不能获取焦点. 创建一个singleView模板的程序,打开storyboard文件.拖动2个UIViewController到界面中.按住 ...

  6. Go语言核心之美 3.2-slice切片

    Slice(切片)是长度可变的元素序列(与之相应,上一节中的数组是不可变的),每一个元素都有同样的类型.slice类型写作[]T.T是元素类型.slice和数组写法非常像,差别在于slice没有指定长 ...

  7. C语言中结构体变量之间赋值

    近期,我阅读了某新员工小刘写的C语言代码,发现其对结构体变量之间的赋值不是非常熟悉. 对于两个同样类型的结构体变量,他均採用的是逐个成员变量直接赋值的形式.例如以下的代码演示样例: 如上代码所看到的, ...

  8. hdu, KMP algorithm, linear string search algorithm, a nice reference provided

    reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...

  9. curl HTTP_USER_AGENT

    [root@bigdata-server-01 ~]# curl 13.26.92.20array(27) { ["HTTP_USER_AGENT"]=> string(11 ...

  10. Xubuntu 计划从 19.04 版本开始停止提供 32 位安装镜像(XDE/LXQt的 Lubuntu 成为了目前唯一仍然提供 32 位安装镜像的 Ubuntu 桌面发行版)

    Ubuntu 17.10 以及其他许多 *buntu 衍生品都已在今年早些时候停止提供 32 位安装镜像.但其中有一个依然坚持提供适用于 i386 架构的镜像,它就是 Xubuntu,但现在 Xubu ...