题目要求出所有合法点对间的最短路径的平均值,因此我们应当求出所有合法最短点对的最 短路径之和,再除以合法点对个数。

题目中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的更多相关文章

  1. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  2. 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 ...

  3. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. but for|lest,in case和for fear (that)|confidential|item|adapted for|fee|debates| retain|substantial|proceeded to|refrain from|clear|perceive

    He ________ you if you ________ to see him that afternoon.  A. might tell, were going  B. told, were ...

  2. XP停止更新不用愁 瑞星XP护盾给你持续保护

    4月8日,微软正式结束了Windows XP的支持,所有XP系统将不会再收到来自微软提供的补丁和安全更新等服务,叱咤OS江湖十几年的一代操作系统终于完美谢幕.但谢幕不等于消失,据相关机构统计,虽然微软 ...

  3. 关于后端下载后端返回的blob类型文件的下载

    关于后端返回blob类型的文件下载记录,在请求的时候前端设置响应类型 responseType: 'blob', const blob = new Blob([r], {type: r.type}); ...

  4. Ajax 请求参数过多导致 400 错误 and BCryptPasswordEncoder 加密判断

    2019/06/19 先分享一种密码加密方式: Spring Security 提供了 BCryptPasswordEncoder类, 实现Spring的PasswordEncoder接口使用BCry ...

  5. SpringMVC 数据校验(JSR-303)

    项目中,通常使用较多的是前端的校验,比如页面中js校验以及form表单使用bootstrap校验.然而对于安全要求较高点建议在服务端进行校验. 服务端校验: 控制层controller:校验页面请求的 ...

  6. python Dom

    Dom(Document) 称为:文档对象模型,是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.DOM把网页和脚本以及其他的编程语言联系了起 ...

  7. EL表达式详细教程

    EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user.sex} 所有EL都是以${ ...

  8. Chrome 调试 react-native 通过Network面板查看网络请求

    参考 https://github.com/facebook/react-native/issues/934 三楼 真机或模拟器下 Debug JS Remotely, 会打开chrome,地址为ip ...

  9. 使用apache mail发送邮件错误解决办法

    今天在写发送邮件的程序时发现了以下两个些错误,贴出来跟大家分享分享 希望对大家有帮助. 错误一: Exception in thread "main" java.lang.NoCl ...

  10. JVM、JRE和JDK三者间的区别和联系

    简介:我们利用JDK(调用JAVA API)开发了属于我们自己的JAVA程序后,通过JDK中的编译程序(javac)将我们的文本java文件编译成JAVA字节码,在JRE上运行这些JAVA字节码,JV ...