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两节 ...
随机推荐
- AUC
https://www.cnblogs.com/earendil/p/9400275.html
- [LeetCode] 421. Maximum XOR of Two Numbers in an Array(位操作)
传送门 Description Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Fin ...
- C#计算两个日期的相隔天数
DateTime start = Convert.ToDateTime(dateStart.ToShortDateString()); DateTime end = Convert.ToDateTim ...
- 什么是php扩展
PHP扩展英文为PHP Extension and Application Repository,简称pear(下面都以pear简称),中文全称为PHP扩展与应用库.是为了创建一个类似于Perl CP ...
- mysql优化工具(索引优化)
mysql优化工具 1.pt-duplicate-key-checker(检查数据库的重复索引),这款工具可以帮助我们找到重复的索引并且还会给你删除重复索引的建议语句,非常好用. 2.
- IDEA中web项目maven项目手动打war包的方式
手动打包 https://blog.csdn.net/ibigboy/article/details/90287963 tomcat部署web项目方法 https://www.cnblogs.com/ ...
- docker镜像源设置
由于docker默认镜像源为国外官方源,下载速度较慢.设置国内镜像源可加速 修改文件 /etc/docker/daemon.json vi /etc/docker/daemon.json 添加以下内容 ...
- 【记录】elastiasearch 6.4.3 版本 SearchRequestBuilder字段排序,BoolQueryBuilder
参考地址: 1:https://www.cnblogs.com/shoutn/p/8027960.html 2:https://www.jianshu.com/p/8402eb930ae7 3:htt ...
- 2018-11-3-git-分支改名
title author date CreateTime categories git 分支改名 lindexi 2018-11-3 12:49:9 +0800 2018-2-13 17:23:3 + ...
- nginx日志切割脚本shell
nginx-log-rotate.sh: #!/bin/bash#---------------------------------------------# Comment:Used for rot ...