HDU 6090 Rikka with Graph

来源:互联网 发布:windows server 2012 编辑:程序博客网 时间:2024/05/28 16:15

题目地址
题意:给你n个点和m条边,让你去尽可能的连点,如果是两点之间不能到达,他们的距离就是n,反之则为距离为连的长度
思路:先所有的边都用去连1,这样的话就是连n-1条边(得到的情况就是与1相连的点距离为1,其他为2),所以判断一下能不能有n-1条边,如果有的话剩余的边就连非1的点,这样的话和就是减少2的代价,然后这样求就好了(看代码)

#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <algorithm>#define N 10010#define LL __int64#define lson l,mid,ans<<1#define rson mid+1,r,ans<<1|1#define getMid (l+r)>>1#define movel ans<<1#define mover ans<<1|1using namespace std;const LL mod = 1e9 + 7;const double eps = 1e-9;const int inf = 1 << 28;LL getx(LL x){    LL p = x - 1;    return p * 2 + p*(p - 1) * 2;}int main() {    cin.sync_with_stdio(false);    int T;    LL n, m;    LL ans;    cin >> T;    while (T--) {        cin >> n >> m;        m = min(n*(n - 1) / 2, m);        if (m < n - 1) {            ans = getx(m + 1);//有m+1个点相连的代价            LL p = n - (m + 1);//还有多少点没有连            ans += (m + 1) * p * n * 2;            ans += p*(p - 1)*n;        }        else {            m -= (n - 1);            ans = getx(n);            ans -= m * 2;        }        cout << ans << endl;    }    return 0;}