HDU 6090 Rikka with Graph —— 2017 Multi-University Training 5
Rikka with Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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 each testcase, the first line contains two numbers n,m(1≤n≤106,1≤m≤1012).
4 5
题意:有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的更多相关文章
- HDU 6090 Rikka with Graph
Rikka with Graph 思路: 官方题解: 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...
- HDU 5631 Rikka with Graph 暴力 并查集
Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...
- HDU 6091 - Rikka with Match | 2017 Multi-University Training Contest 5
思路来自 某FXXL 不过复杂度咋算的.. /* HDU 6091 - Rikka with Match [ 树形DP ] | 2017 Multi-University Training Conte ...
- HDU 6088 - Rikka with Rock-paper-scissors | 2017 Multi-University Training Contest 5
思路和任意模数FFT模板都来自 这里 看了一晚上那篇<再探快速傅里叶变换>还是懵得不行,可能水平还没到- - 只能先存个模板了,这题单模数NTT跑了5.9s,没敢写三模数NTT,可能姿势太 ...
- HDU 6093 - Rikka with Number | 2017 Multi-University Training Contest 5
JAVA+大数搞了一遍- - 不是很麻烦- - /* HDU 6093 - Rikka with Number [ 进制转换,康托展开,大数 ] | 2017 Multi-University Tra ...
- HDU 6085 - Rikka with Candies | 2017 Multi-University Training Contest 5
看了标程的压位,才知道压位也能很容易写- - /* HDU 6085 - Rikka with Candies [ 压位 ] | 2017 Multi-University Training Cont ...
- HDU 5422 Rikka with Graph
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5424——Rikka with Graph II——————【哈密顿路径】
Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 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两节 ...
随机推荐
- MSSQL sql常用判断语句
.判断数据库是否存在 if exists (select * from sys.databases where name = '数据库名') drop database [数据库名] 2 判断 ...
- Python 学习笔记15 类 - 继承
我们在编程的过程中,并非都是要重头开始.比如其他人已经有现成的类,我们可以使用其他找人编写的类.术语称之为: 继承. 当一个类继承例外一个类时,它可以获得这个类的所有属性和方法:原有的类称之为 父类, ...
- Struts2之获取ServletAPI
1.通过ServletActionContext类 //获取request对象 HttpServletRequest request = ServletActionContext.getRequest ...
- SVN合并主干分支的方法
第一步 第二步 第三步 第四步
- 复制书稿 (dp+贪心)
[题目描述] 现在要把m本有顺序的书分给k个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如不能把第一.第三和第四本书给同一个人抄写 ...
- 115-基于TI TMS320DM6467T Camera Link 机器视觉 智能图像分析平台
基于TI TMS320DM6467无操作系统Camera Link智能图像分析平台 1.板卡概述 该板卡是我公司推出的一款具有超高可靠性.效率最大化.无操作系统的智能视频处理卡,是机器视觉开发上的首选 ...
- 12-低延迟、全接口(HMDI、DVI、YPb Pr、RGB)H.264全高清编码器解码器
低延迟.全接口(HMDI.DVI.YPb Pr.RGB)H.264全高清编码器解码器 一.产品介绍 1.近零延时的H.264压缩到1920x1080p60 该产品提供分辨率为1920x1080p6 ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
- 批量定时任务将rtf文件转为docx,入参是rtf文件夹,生成一个docx文件夹
java,python等语言对于rft的处理很受限,rtf提供了很少的api供外部调用处理,但是对于docx我们却又很多api来处理,所以很多人会有需求将rtf批量转为docx的需求,接下来就来说说解 ...
- bzoj4182 Shopping 点分治+单调队列优化多重背包
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4182 题解 有一个很直观的想法是设 \(dp[x][i]\) 表示在以 \(x\) 为根的子树 ...