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

题目中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. 密码子演化假说|凝固事件假说|立体化学假说|共进化假说|代谢途径相关性假说|四重兼并|假四重兼并|最小损伤原则|AU-rich|GC-rich|逐步进化假说|分子机制进化假说

    生命组学 将密码子表重排后发现,嘌呤嘧啶含量不同,密码子的氨基酸种类由第一二位决定,同时第三位变化大却没有蛋白质层面上实质性的改变,这说明第三位氨基酸是用于维持氨基酸组成不发生变化同时保证蛋白质稳定性 ...

  2. 如何手动添加jar包到本地maven仓库

    环境 win10    idea工具 1.确认已经安装好 mvn环境   MAVEN_HOME   D:\Tool\apache-maven-3.5.2 Path %MAVEN_HOME%\bin 2 ...

  3. 831. KMP字符串

    给定一个模式串S,以及一个模板串P,所有字符串中只包含大小写英文字母以及阿拉伯数字. 模板串P在模式串S中多次作为子串出现. 求出模板串P在模式串S中所有出现的位置的起始下标. 输入格式 第一行输入整 ...

  4. css 控制li点与文字的距离

    ul>li 默认的样式如下 前面的点与文字是有间距的 可见,通过设置 text-indent 属性可以调整li点与文字的距离

  5. 刷金币全自动脚本 | 让Python每天帮你薅一个早餐钱(送源码)

    刷金币全自动脚本 | 让Python每天帮你薅一个早餐钱(送源码) 测试开发社区  6天前 文章转载自公众号  AirPython , 作者 星安果 阅读文本大概需要 12 分钟. 1 目 标 场 景 ...

  6. Logback 标准xml参考

    强制: [强制]应用中不可直接使用日志系统(Log4j.Logback)中的 API,而应依赖使用日志框架SLF4J 中的 API,使用门面模式的日志框架,有利于维护和各个类的日志处理方式统一.imp ...

  7. 会编程的 AI + 会修 Bug 的 AI,等于什么 ?

    2017-02-25 Python开发者 (点击上方公众号,可快速关注) 关于人工智能未来的畅想,除了家庭服务机器人,快递无人机,医用机器人等等,Lucas Carlson 认为人工智能在另外一个领域 ...

  8. 印度IT产业今年裁员5.6万,自动化大潮下安有完卵

    [腾讯科技编者按]业界媒体Quatz撰文指出,对于印度科技从业者来说,2017年是噩梦连连的一年.直到几年前,IT业都还是印度提供就业岗位最多的行业之一,但在今年,这个1600亿美元规模的行业裁掉了5 ...

  9. Python2.7错误处理FileNotFoundError报错NameError: name 'FileNotFoundError' is not defined

    错误信息如下: 原因是FileNotFoundError是python3.0中的写法,而Python2.7中应写为IOError.

  10. USB小白学习之路(8)FX2LP cy7c68013A——Slave FIFO 与FPGA通信(转)

    此博客转自CSDN:http://blog.csdn.net/xx116213/article/details/50535682 这个博客只对自己理解CY7C68013的配置有一定的帮助,对于配置CY ...