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)

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-1);
        if(m>0){
            if(m<=n-1){
                res=res-m*(m-1)*(n-2)-2*m*(n-1);
            }
            else if(m>n*(n-1)/2)
                res=n*(n-1);
            else{
                res=res-(n-1)*(n-2)*(n-2)-2*(n-1)*(n-1)-2*(m-n+1);
            }
        }
        printf("%lld\n", res);
    }
    return 0;
}
时间: 2024-10-19 13:11:40

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

hdu 6090 Rikka with Graph

Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 425    Accepted Submission(s): 270 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation,

HDU 5422:Rikka with Graph

Rikka with Graph Accepts: 353 Submissions: 1174 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 勇太有一张nn个点mm条边的无向图,每一条边的长度都是1.现在他想再在这张图上连上一条连接两个不同顶点边,使得1号点到nn号点的最短路尽可能的短

hdu 5424 Rikka with Graph II 哈密顿通路

Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 367    Accepted Submission(s): 90 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situatio

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

Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1051    Accepted Submission(s): 266 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situati

hdu 5422 Rikka with Graph(简单题)

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: Yuta has a non-direct graph with n vertices and m edges. The length of each edge is 1.

hdu 5424 Rikka with Graph II (BestCoder Round #53 (div.2))(哈密顿通路判断)

http://acm.hdu.edu.cn/showproblem.php?pid=5424 哈密顿通路:联通的图,访问每个顶点的路径且只访问一次 n个点n条边 n个顶点有n - 1条边,最后一条边的连接情况: (1)自环(这里不需要考虑): (2)最后一条边将首和尾连接,这样每个点的度都为2: (3)最后一条边将首和除尾之外的点连接或将尾和出尾之外的点连接,这样相应的首或尾的度最小,度为1: (4)最后一条边将首和尾除外的两个点连接,这样就有两个点的度最小,度都为1 如果所给的图是联通的话,那

HDU 5631 Rikka with Graph

如果原图不连通,直接输出0. 如果原图连通,删除X条边之后要保证新图连通,再看数据是n+1条边-->因此,最多只能删去两条边. 因为n=100,可以枚举进行验证,枚举删去每一条边是否连通,枚举删去每两条边是否连通,验证是否连通可以用并查集,可以BFS. #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<vector> #include&l

hdu 5631 Rikka with Graph(图)

n个点最少要n-1条边才能连通,可以删除一条边,最多删除2条边,然后枚举删除的1条边或2条边,用并查集判断是否连通,时间复杂度为O(n^3) 这边犯了个错误, for(int i=0;i<N;i++){ fa[i]=i; } 这个将i<=N,导致错误,值得注意 AC代码: 1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cst

hdu 6088 Rikka with Rock-paper-scissors (2017 多校第五场 1004) 【组合数学 + 数论 + 模意义下的FFT】

题目链接 首先利用组合数学知识,枚举两人的总胜场数容易得到 这还不是卷积的形式,直接搞的话复杂度大概是O(n^2)的,肯定会TLE.但似乎和卷积有点像?想半天没想出来..多谢Q巨提醒,才知道可以用下面这个公式进行转化 最后,化得的公式为 另外注意,上式右边是一个卷积的形式,但是,所得和的第一项是不需要加上的(不过图中公式没有体现).结合实际意义大概就是,i==0&&j==0时,gcd(i,j)不存在约数d,虽然0可以被任意正整数整除 & 第一项不为0 #include<bit