3018: [Usaco2012 Nov]Distant Pastures

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 43  Solved: 20
[Submit][Status][Discuss]

Description

Farmer John's farm is made up of an N x N grid of pastures, where each pasture contains one of two different types of grass. To specify these two types of grass, we use the characters ( and ), so for example FJ's farm might look like the following grid:
(())
)()(
)(((
))))
When Bessie the cow travels around the farm, it takes her A units of time to move from a pasture to an adjacent pasture (one step north, south, east, or west) with the same grass type, or B units of time to move to an adjacent pasture with a different grass type. Whenever Bessie travels from one pasture to a distant pasture, she always uses a sequence of steps that takes the minimum amount of time. Please compute the greatest amount of time Bessie will ever need to take while traveling between some pair of pastures on the farm.
 
问题描述
给定一个n×n的一个网格,每个格子有一个字符,要么是’(‘,要么是’)’。每个格子和它的上下左右的四个格子相邻,对于相邻的两个格子xy,从x走到y的过程中,如果xy中的字符相同,消耗A单位时间,如果xy中字符不同,消耗B单位时间。定义点S到点T的时间为D(S,T),现在想请你求出网格中最大的D(S,T)。
 

Input

       第一行三个整数nAB
       接下来n行描述这个n×n的网格。
 

Output

 
       一个整数,最大的D(S,T)。
 

Sample Input

3 1 2
(((
()(
(()

Sample Output

5

样例说明
左上角到右下角所需的时间为5,是最大值。

数据范围
100%的数据满:1 <= n <= 30,1 <= A <= 1,000,000,1 <= B <= 1,000,000。

HINT

 

Source

Silver

题解:本来一开始想到的是floyd暴力乱搞,但是在这里面复杂度是O(N^6)的,显然爆(HansBug:更何况这么稀疏的图这么玩不挂才怪= =)

于是根据囧神(HansBug:orzJSZKC)的做法,开始枚举起点玩spfa,于是居然很神奇的AC了QAQ(HansBug:我去这都能A,不过再一看N<=30也就懂了^_^)

(PS:我居然成了继囧神以后第一个Pascal秒掉此题的人了么么哒)

 /**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ type
point=^node;
node=record
g,w:longint;
next:point;
end;
var
i,j,k,l,m,n,x,y,f,r:longint;
a:array[..] of point;
c,g:array[..] of longint;
d:array[..] of longint;
b:array[..,..] of longint;
ch:char;p:point;
function max(x,y:longint):longint;
begin
if x>y then max:=x else max:=y;
end;
function trans(x,y:longint):longint;
begin
exit((x-)*n+y);
end;
procedure add(x,y,z:longint);
var p:point;
begin
new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p;
new(p);p^.g:=x;p^.w:=z;p^.next:=a[y];a[y]:=p;
end;
procedure spfa(z:longint);
var i,j,f,r:longint;p:point;
begin
fillchar(g,sizeof(g),);
fillchar(c,sizeof(c),-);
d[]:=z;f:=;r:=;g[z]:=;c[z]:=;
while f<r do
begin
p:=a[d[f]];
while p<>nil do
begin
if (c[p^.g]=-) or (c[p^.g]>(c[d[f]]+p^.w)) then
begin
c[p^.g]:=c[d[f]]+p^.w;
if g[p^.g]= then
begin
g[p^.g]:=;
d[r]:=p^.g;
inc(r);
end;
end;
p:=p^.next;
end;
g[d[f]]:=;inc(f);
end;
for i:= to n do for j:= to n do l:=max(l,c[trans(i,j)]);
end; begin
readln(n,x,y);
for i:= to n*n do a[i]:=nil;
for i:= to n do
begin
for j:= to n do
begin
read(ch);
case ch of
'(':b[i,j]:=;
')':b[i,j]:=;
end;
end;
readln;
end;
for i:= to n do
for j:= to n do
begin
if i<n then add(trans(i,j),trans(i+,j),abs(b[i,j]-b[i+,j])*(y-x)+x);
if j<n then add(trans(i,j),trans(i,j+),abs(b[i,j]-b[i,j+])*(y-x)+x);
end;
l:=;
for i:= to n*n do spfa(i);
writeln(l);
readln;
end.

3018: [Usaco2012 Nov]Distant Pastures的更多相关文章

  1. BZOJ3016: [Usaco2012 Nov]Clumsy Cows

    3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 71  Solved: 52[Submi ...

  2. 3016: [Usaco2012 Nov]Clumsy Cows

    3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 91  Solved: 69[Submi ...

  3. 【BZOJ】3016: [Usaco2012 Nov]Clumsy Cows(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3016 之前yy了一个贪心,,,但是错了,,就是枚举前后对应的字符(前面第i个和后面第i个)然后相同答 ...

  4. BZOJ 3016 [Usaco2012 Nov]Clumsy Cows:贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3016 题意: 给你一个括号序列,问你至少修改多少个括号,才能使这个括号序列合法. 题解: ...

  5. [Usaco2012 Nov]Concurrently Balanced Strings

    Description [Brian Dean, 2012] Farmer John's cows are all of a very peculiar breed known for its dis ...

  6. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  7. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  8. BZOJ USACO 银组 水题集锦

    最近刷银组刷得好欢快,好像都是水题,在这里吧他们都记录一下吧(都是水题大家一定是道道都虐的把= =)几道比较神奇的题到时再列出来单独讲一下吧= =(其实我会说是BZOJ蹦了无聊再来写的么 = =) [ ...

  9. Usaco2012-2013 金组 题解 (暂缺Hill walk以及Figue eight)

    https://files.cnblogs.com/files/Winniechen/usaco2012-2013.pdf 做的不是很好,还请见谅! 如果有什么疑问,可以QQ上找我. QQ号:1967 ...

随机推荐

  1. Webx3学习笔记(2)——基本流程

    Webx3项目是运行在jetty/tomcat这种Web应用容器中的,Web应用的模式都是请求-响应的.一个请求通过浏览器发出,封装为HTTP报文到达服务端,被容器接受到,封装为HttpRequest ...

  2. 如何去掉Atom的右键菜单?

    Win+R -- regedit -- Ctrl+F -- 搜索"Atom"(或者直接"Open with Atom") -- 找到有个值为Open with ...

  3. spring mvc 返回页面数据

    handler package com.stone.controller; import java.util.Map; import javax.servlet.http.HttpServletReq ...

  4. 每一个Servlet只有一个实例,多个线程

    每一个Servlet只有一个实例,多个线程: Servlet: package com.stono.servlet.synchronize; import javax.servlet.http.Htt ...

  5. jQuery查询性能考虑

    http://blog.163.com/neusoft_hao@yeah/blog/static/120544724201282810510215/

  6. 一步一步学Java IO

    1.基本概念 1.1.InputStream 最基本的字节输入流,抽象类,定义了读取原始字节的所有基本方法1.1.1.public abstract int read() throws IOExcep ...

  7. 与中国最顶尖sharepoint工程师共舞

    最近又跳了,来到某家外企.自以为善能称心如意,谁知乃井里之蛙. 给我的最大感触是,做sharepoint一定要做过非常大型的部署开发,没有经过这种淬炼,天天闷声研究,做一些页面功能,对技术提升毫无帮助 ...

  8. css3 2d转换3d转换以及动画的知识点汇总

    css3 2d转换 2d转换的方法: 1.移动 translate(x, y) 可以改变元素的位置,x.y可为负值: 2.缩放 scale(x, y) 可以对元素进行水平和垂直方向的缩放,x.y的取值 ...

  9. Python学习--16 正则表达式

    正则表达式是一种描述性的语言,用来匹配字符串.凡是符合规则的字符串,我们认为就是匹配了. 正则表达式并非Python独有的,它与语言无关.很多语言都支持正则表达式. 我们经常用正则表达式来匹配电子邮件 ...

  10. Linux编程之从零开始搭建RPC分布式系统

    我一毕业进公司就接触到了RPC,主要是使用前辈们搭建好的RPC框架以及封装好的RPC函数进行业务开发,虽说使用RPC框架开发已经近半年了,但一直想知道如何从零开始搭建起这么一个好用的分布式通信系统框架 ...