您的位置:首页 > 大数据 > 人工智能

2017 Multi-University Training Contest - Team 5:1006&hdu6090、 Rikka with Graph

2017-08-08 22:08 459 查看
题目:

[align=left]Problem Description[/align]
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 betweeni
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 graphG
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).
 

[align=left]Input[/align]
The first line contains a numbert(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).
 
b7a8

[align=left]Output[/align]
For each testcase, print a single line with a single number -- the answer.
 

[align=left]Sample Input[/align]

1
4 5

 

[align=left]Sample Output[/align]

14

题意:有n个点构成无向图,求这张图的最小G值。就是求和for(i=1;i<=n;i++) for(j=1;j<=n;j++) sum+=dist(i,j)。dist(i,j)为从i到j的最短路径值(若1->2,2->3,则1->3的dist值为2):你可以选择m对(i,j),使dist(i,j)为1,其它dist(i,j)的值为n,然后更新到其他的dist值。ps:dist(i,j)=0。

思路:共有3种情况:由于其为无向图,故邻接矩阵为对称矩阵,则其一半矩阵共有n*(n-1)/2对(i,j)。1、如果m>=n*(n-1)/2,则G=n*(n-1);2、若m>=n-1,则从i=n*(n-1)/2到(n-1),每次i-1则G+2。这个可以自己多去写些数据就可以推出来;3、若m<n-1,则尽可能使m条边连到同一个点上,这样G最小。考虑m=1和m!=1的情况。动手画个图就明白了

CODE:

#include<bits/stdc++.h>
using namespace std;
int a[100005];
int main()
{
int t,i,j;
__int64 n,m,k,l;
scanf("%d",&t);
while(t--){
scanf("%I64d%I64d",&n,&m);
k=n*(n-1);
if(m>=k/2) printf("%I64d\n",k);
else if(m>=(n-1)){
l=k/2-m;
printf("%I64d\n",k+l*2);
}
else{
l=n-(m+1);
__int64 num=l*(n-1)*n;
if(m==1) num+=(1+(n-2)*n)*2;
else num+=(m+(n-1-m)*n)+(1+(m-1)*2+l*n)*m;
printf("%I64d\n",num);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  思维题
相关文章推荐