HDU 6090 Rikka with Graph

来源:互联网 发布:淘宝店铺无线端装修 编辑:程序博客网 时间:2024/05/24 06:27

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

Source

2017 Multi-University Training Contest - Team 5

Recommend

liuyiding | We have carefully selected several similar problems for you: 6132 6131 6130 6129 6128

題意:
给你n个点,m条边。
让你构建一个图形,使得,每一个点到所有其他的点的距离和的和最小。
如果某两点之间无法到达, 取值n。

你要我怎么思考咧,那我肯定是贪心的去想,虽然和常规贪心没有什么关系。

分析:
1.如果边足够多,以至于每两个点都是直接连接。那么就是n组点(n-1)条边就可以解决问题。也就是当m>=n(n-1)/2; ans=n*(n-1);
2.如果边很少,我们就让其摆成中心辐射状,其余的散点也没别的办法,= =像一朵小花的样子 = =
3.两者之间 肯定还是小花的样子,但是新加进来的边有什么作用呢。
那就是把两点之间直接联通,距离变成1。每加进来一条边就是这样,算一算最后是不是到达了第一种情况,也是一种判断方式。

#include<iostream>#include<cstring>using namespace std;#define ll long long intint main(){    int t;    cin >> t;    while (t--)    {        ll n, m, ans = 0;        cin >> n >> m;        ans = (n - 1)*(n - 1) * 2;        if (m >= n*(n - 1) / 2)        {            cout << n*(n - 1) << endl;            continue;        }        if (m < n - 1)        {            cout << 2 * m*m + n*(n + m)*(n - m - 1) << endl;        }        else        {            cout << ans - (m - n + 1) * 2 << endl;        }    }    return 0;}