Rikka with Graph
来源:互联网 发布:js给class加样式 编辑:程序博客网 时间:2024/05/29 15:37
原题链接:点击查看原题
题意:
给你n个点,你可以在这n个点之间任意连<=m条线,使∑ni=1∑nj=1dist(i,j).的值最小。
其中dist(i,j)表示如过i和j之间有通路,则dist(i,j)的值为i和j之间经过的边数。如a--->b---c
(前提是没有a--->c,如过有的话,就取 1 而不是 2 ,因为要使值最小)那么dist(a,c)等于2
因为经过了两条边,如过a和b之间没有通路,那么dist(a,b)就等于n。
解题思路:
假设这些点相互都相连,即n*(n-1)/2=m,则答案为:n*(n-1),
假设这些点是联通的,即:m>=n-1,我们可以根据上种情况接着思考,如果每有一个不相互连通,则就要加2,
即:n*(n-1)+(n*(n-1)/2-m)*2.
假设有一部分相连,即:m<n-1,
q表示没相连的点,p为相连的点。则:p=m+1,q=n-m-1。
对于没有相连的:q*(q-1)*n
对于相连的:(p-1)*(p-1)*2;//由(1+(m-1)*2)*m+m化简得到
对于没相连的点与相连的:q*p*n;
把上边的相加就是最终答案。
原代码:
#include <iostream>using namespace std;int main(){ int t; cin>>t; while(t--) { long long n,m; cin>>n>>m; if(n*(n-1)/2<=m){ cout<<n*(n-1)<<endl; continue; } if(m>=n-1) cout<<n*(n-1)+(n*(n-1)/2-m)*2<<endl; else { long long p=m+1; long long q=n-m-1; long long ans=q*p*n*2+q*(q-1)*n+2*(p-1)*(p-1); cout<<ans<<endl; } } return 0;}
阅读全文
0 0
- Rikka with Graph
- Bc Rikka with Graph
- Rikka with Graph<hdoj5631>
- Rikka with Graph HDU
- HDU5631 Rikka with Graph
- Rikka with Graph||HDU5631
- Rikka with Graph
- HDU5631 Rikka with Graph
- HDU6090 Rikka with Graph
- HDU6090 Rikka with Graph
- HDU6090 Rikka with Graph
- hdu6090 Rikka with Graph
- Rikka with Graph
- Rikka with Graph
- hdu6090 Rikka with Graph
- HDU6090-Rikka with Graph
- hdu 5422 Rikka with Graph
- HDU 5422:Rikka with Graph
- Ubuntu14.04中创建SWAP交换分区
- 快速简单理解——直接插入排序
- PAT-A 1002. A+B for Polynomials (25)
- linux主机使用rdesktop连接windows进行usb重定向
- mysql的三种安装方式
- Rikka with Graph
- redis开启远程访问
- c#--wpf数据绑定binding
- DIV标签详细介绍
- StringUtils常用方法+StringUtils详细介绍
- 【机房合作】报表
- Python3.X下安装Scrapy (转自鱼c论坛作者lumber2388779)
- 异常在 mscorlib.dll 中发生
- 蓝创十周年大庆,诚邀创业者共分享