2016 Multi-University Training Contest 1 T3
题目要求出所有合法点对间的最短路径的平均值,因此我们应当求出所有合法最短点对的最 短路径之和,再除以合法点对个数。
题目中Guard之间有着很不自然的制约关系,每个Guard的周围和同行、列都不能有其余的 Guard,但不可路过的格子却只有本身一格。由此发现,任意两点间的最短路径只会至多被 一个Guard所干扰。
我们可以先算出没有Guard制约的所有最短路径长度之和,再分别对于每个 Guard求出必须经过他的点对个数,原本必须经过它的最短路要多绕两格。
对于第i行每个#点,他对于上面第j行的每个#点在纵坐标路径长度的贡献是abs(i-j),列也一样。然后就可以o(n*m)的算出路径和。
对于G点,和他在同一列的和不同列的可以分开算。
#include<cstring>
#include<cstdio>
#define mem(a) memset(a,0,sizeof(a))
int gl[],gh[],_,i,j,n,m;
double x,sum,ans,numl[],numh[],p;
char c[][];
int main()
{
//freopen("1.in","r",stdin); freopen("1.out","w",stdout);
scanf("%d",&_);
while (_--)
{
sum=;
mem(numh);mem(numl);mem(gl);mem(gh);
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
{
scanf("%s",c[i]+);
for (j=;j<=m;j++)
{
if (c[i][j]=='#')
{
sum+=;
numl[j]+=;
numh[i]+=;
}
else
{
gl[j]=i;
gh[i]=j;
}
}
}
ans=;sum=sum*sum;
for (i=;i<=n;i++)
{
for (j=;j<i;j++)
{
x=(i-j)*numh[j]/sum;
ans+=x*numh[i];
}
for (j=i+;j<=m;j++)
{
x=(j-i)*numh[j]/sum;
ans+=x*numh[i];
}
}
for (i=;i<=m;i++)
{
for (j=;j<i;j++)
{
x=(i-j)*numl[j]/sum;
ans+=x*numl[i];
}
for (j=i+;j<=m;j++)
{
x=(j-i)*numl[j]/sum;
ans+=x*numl[i];
}
}
for (i=;i<=n;i++)
for (j=;j<=m;j++)
if(c[i][j]=='G')
ans+=((i-)*(n-i)*+(j-)*(m-j)*)/sum; for (j=;j<=n;j++){
if (gh[j-]== || gh[j]==) continue;
if (gh[j-]<gh[j]){
p=(gh[j-]-)*(m-gh[j])/sum;
for (i=j-;i>;i--){
if (gh[i]== || gh[i]>gh[i+]) break;
p+=1.0*(gh[i]-)*(m-gh[j])/sum;
}
}else {
p=(m-gh[j-])*(gh[j]-)/sum;
for (i=j-;i>;i--){
if (gh[i]== || gh[i]<gh[i+]) break;
p+=(m-gh[i])*(gh[j]-)/sum;
}
}
ans+=4.0*p;
}
for (j=;j<=m;j++){
if (gl[j-]== || gl[j]==) continue;
if (gl[j-]<gl[j]){
p=(gl[j-]-)*(n-gl[j])/sum;
for (i=j-;i>;i--){
if (gl[i]== || gl[i]>gl[i+]) break;
p+=(gl[i]-)*(n-gl[j])/sum;
}
}else {
p=(n-gl[j-])*(gl[j]-)/sum;
for (i=j-;i>;i--){
if (gl[i]== || gl[i]<gl[i+]) break;
p+=(n-gl[i])*(gl[j]-)/sum;
}
}
ans+=4.0*p;
} printf("%.4f\n",ans);
}
return ;
}
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
17652872 | 2016-07-20 19:02:27 | Accepted | 5725 | 717MS | 2528K | 2012 B | G++ | lbz007 |
2016 Multi-University Training Contest 1 T3的更多相关文章
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 2016 Al-Baath University Training Camp Contest-1 E
Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- 2016 Al-Baath University Training Camp Contest-1 J
Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...
- 2016 Al-Baath University Training Camp Contest-1 I
Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...
- 2016 Al-Baath University Training Camp Contest-1 H
Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...
- 2016 Al-Baath University Training Camp Contest-1 G
Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...
- 2016 Al-Baath University Training Camp Contest-1 F
Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...
- 2016 Al-Baath University Training Camp Contest-1 D
Description X is well known artist, no one knows the secrete behind the beautiful paintings of X exc ...
随机推荐
- hadoop报错:could only be replicated to 0 nodes, instead of 1
错误 [root@hadoop test]# hadoop jar hadoop.jarcom.hadoop.hdfs.CopyToHDFS 14/01/26 10:20:00 WARN hdfs.D ...
- 整理struct sockaddr和struct sockaddr_in
struct sockaddr定义在/usr/include/linux/socket.h struct sockaddr { unsigned short ss_family; - } struct ...
- unittest(22)- p2p项目实战(8)-test_class_auto_incre
# 8.test_class_auto_incre # 使用ddt import requests import unittest from p2p_project_7.tools.http_requ ...
- 吴裕雄--天生自然 R语言开发学习:基本统计分析(续三)
#---------------------------------------------------------------------# # R in Action (2nd ed): Chap ...
- 吴裕雄--天生自然KITTEN编程:对话
- SVN图标含义说明
最经都在用Svn,对他上面的很多状态图标不是很理解,看了看它的帮助文档,说的很清楚,特地截张图. Svn不同状态图标及说明 - 简单 - 简单 Normal A fresh checked ...
- Django中的Model.objects.create() 和 Model() 的区别?
Django 官方文档说明 objects.create 是 A convenience method for creating an object and saving it all in one ...
- 免密码 ssh 到其它机器
背景:在配置 hadoop 的时候这样设置会比较方便.目标:A 机器上输入 ssh root@B 可以直接访问,不需要输入密码 步骤: 首先在 A 机器上生成密钥对,一路回车 1 ssh-keygen ...
- lvs/dr+keepalived搭建成功后,开启防火墙,虚拟IP不能访问,但是真实IP却可以访问
lvs/dr+keepalived搭建成功后,开启防火墙,虚拟IP不能访问,但是真实IP却可以访问,如果关闭防火墙虚拟IP就可以访问网站了,这个问题肯定是防火墙在作怪. 经过这两的不懈奋斗和大家的帮助 ...
- openCryptoki安装
什么是OpenCryptoki OpenCryptoki提供Linux下的PKCS#11库和工具,支持包括TPM和IBM加密硬件以及软件令牌. 目前(2019/05/06)最新release版为3.1 ...