2017杭电多校06Rikka with Graph
2024-08-30 19:42:09
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).
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).
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
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;
}
最新文章
- Http相关知识
- 提额 APP
- 使用 PHPMailer 发送邮件
- 自动化部署教程(一) redhat安装jenkins
- vs2010 rdlc .net4.0 卸载 Appdomain 时出错。 (异常来自 HRESULT:0x80131015) 解决办法
- 搭建DNS服务器
- Delphi 程序结构
- mq_setattr
- 【C++基础】 多态 虚函数
- html-----013----实体字符/HTML URL 编码
- 如何查看Windows下端口占用
- jquery中的uploadfile关于图片上上传的插件的应用
- voa 2015.4.29
- bis和bic命令实现或和异或运算
- 解决:fatal error LNK1104: 无法打开文件“libc.lib”
- Python爬取网易云歌单
- JavaScript常用事件参考
- 2018/03/14 每日一个Linux命令 之 ln
- Jackson2.1.4 序列化对象时对属性的过滤
- 自动下单tomcat版本问题