Rikka with Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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

题意:有n各点,问取其中至多m对点连成边,每条边的权值为1,求连好之后所有点之间的最短路(记为dis(i,j))的和的最小值。若两个点不是连通的,则这两条边的dis取作n。

思路:贪心。

1.当m<=n-1时,我们尽可能每一条边都把不同的点连通,我们可以把 ① 点作为根节点,每加入一条边,就从这个根节点连接到另一个不在连通块里的点(见下图,虚线代表下一条连接的边)。

对于被连接的点来说,它到根节点的距离从 n -> 1, 到其他在子节点的距离从 n -> 2, 所以加入第 i 个点后,原先总距离之和减少了 2*[(n-1)+(i-1)*(n-2)]。由于m=0(即没有边)时总距离和为 n*n*(n-1), 此时总距离和为

2.当m>n-1时,剩余的点两两相连,由于每两个子节点之间距离都是2,每连一条边都只有一对点的距离从2变为1,所以每多连一条边,总距离减少 2*1,所以在上式的基础上减去 2*(m-(n-1)) 即可。注意当m > n*(n-1)/2时最多能取n*(n-1)/2对点, res=n*(n-1)。

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long LL;
int main()
{
LL n,m;
int T;
cin>>T;
while(T--)
{
scanf("%lld %lld", &n, &m);
LL res=n*n*(n-);
if(m>){
if(m<=n-){
res=res-m*(m-)*(n-)-*m*(n-);
}
else if(m>n*(n-)/)
res=n*(n-);
else{
res=res-(n-)*(n-)*(n-)-*(n-)*(n-)-*(m-n+);
}
}
printf("%lld\n", res);
}
return ;
}

HDU 6090 Rikka with Graph —— 2017 Multi-University Training 5的更多相关文章

  1. HDU 6090 Rikka with Graph

    Rikka with Graph 思路: 官方题解: 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...

  2. HDU 5631 Rikka with Graph 暴力 并查集

    Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...

  3. HDU 6091 - Rikka with Match | 2017 Multi-University Training Contest 5

    思路来自 某FXXL 不过复杂度咋算的.. /* HDU 6091 - Rikka with Match [ 树形DP ] | 2017 Multi-University Training Conte ...

  4. HDU 6088 - Rikka with Rock-paper-scissors | 2017 Multi-University Training Contest 5

    思路和任意模数FFT模板都来自 这里 看了一晚上那篇<再探快速傅里叶变换>还是懵得不行,可能水平还没到- - 只能先存个模板了,这题单模数NTT跑了5.9s,没敢写三模数NTT,可能姿势太 ...

  5. HDU 6093 - Rikka with Number | 2017 Multi-University Training Contest 5

    JAVA+大数搞了一遍- - 不是很麻烦- - /* HDU 6093 - Rikka with Number [ 进制转换,康托展开,大数 ] | 2017 Multi-University Tra ...

  6. HDU 6085 - Rikka with Candies | 2017 Multi-University Training Contest 5

    看了标程的压位,才知道压位也能很容易写- - /* HDU 6085 - Rikka with Candies [ 压位 ] | 2017 Multi-University Training Cont ...

  7. HDU 5422 Rikka with Graph

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

  8. HDU 5424——Rikka with Graph II——————【哈密顿路径】

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

  9. HDU 6162 - Ch’s gift | 2017 ZJUT Multi-University Training 9

    /* HDU 6162 - Ch’s gift [ LCA,线段树 ] | 2017 ZJUT Multi-University Training 9 题意: N节点的树,Q组询问 每次询问s,t两节 ...

随机推荐

  1. HTML --JS 选择框

    <html> <head> <title>选择框</title> <script language="JavaScript"& ...

  2. Learning OSG programing---osgShape

    本例示范了osg中Shape ---- 基本几何元素的绘制过程.参照osg官方文档,Shape 类包含以下子类: 在示例程序中,函数createShapes函数用于生成需要绘制的几何形状. osg:: ...

  3. Spring cloud学习--Zuul02

    过滤器 Zuul包括两部分内容:请求的路由和过滤.而实际上请求的路由也是通过过滤器实现的,例如理由映射主要通过pre类型的过滤器完成,它将请求路径与配置的路由规则进行匹配,找到需要转发的目标地址:请求 ...

  4. <搬运> SQL语句百万数据量优化方案

    一:理解sql执行顺序 在sql中,第一个被执行的是from语句,每一个步骤都会产生一个虚拟表,该表供下一个步骤查询时调用,比如语句:select top 10 column1,colum2,max( ...

  5. Pandas处理缺失的数据

    处理丢失数据 有两种丢失数据: None np.nan(NaN) import numpy as np import pandas from pandas import DataFrame 1. No ...

  6. 相对路径 分类: C# 2015-06-11 15:41 8人阅读 评论(0) 收藏

    .绝对路径     绝对路径是指文件在硬盘上真正存在的路径.例如"bg.jpg"这个图片是存放在硬盘的"E:\book\网页布局代码\第2章"目录下,那么 &q ...

  7. Windows程序设计--(一)起步

    1.3 你的第一个Windows程序 1.3.2 Windows 对应程序 #include <windows.h> int WINAPI WinMain(HINSTANCE hInsta ...

  8. 七、WebApi跨域操作

    情景:测试后返回"no response from server"? 请求响应: js调试控制台: 一.搜索安装 Microsoft.AspNet.WebApi.Cors 二.添加 ...

  9. Vue-鼠标按键修饰符

    left .right .middle 这些修饰符会限制处理函数仅响应特定的鼠标按钮. 如下例子 <div id="app"> <input type=" ...

  10. spring cloud学习一--Eureka服务注册与发现

    spring cloud Eureka是基于Netflix Eureka服务发现注册产品的二次封装,它提供了服务注册功能(Service Registry)和服务发现功能(Service Discov ...