我是靠谱客的博主 如意故事,这篇文章主要介绍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
2
3
4
5
6
1 4 5
 

Sample Output
复制代码
1
2
3
4
5
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);


代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部