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. Java线程:堵塞队列与堵塞栈

    一.堵塞队列 Java定义了堵塞队列的接口java.util.concurrent.BlockingQueue,堵塞队列是一个指定长度的队列,当试图向队列中添加元素而队列已满,或者是想从队列移出元素而 ...

  2. Flex移动应用程序开发的技巧和窍门(二)

    范例文件 flex-mobile-dev-tips-tricks-pt2.zip 这是关于Flex移动应用程序开发的技巧和窍门的一系列文章中的第二部分.第一部分 内容主要集中讨论了视图之间以及应用程序 ...

  3. python bottle 简介

    bottle是一个轻量级的python web框架, 可以适配各种web服务器,包括python自带的wsgiref(默认),gevent, cherrypy,gunicorn等等.bottle是单文 ...

  4. 触摸滑动插件 Swiper

    Swiper Swiper  是纯javascript打造的滑动特效插件,面向手机.平板电脑等移动终端. Swiper中文网里已有详细的使用介绍,我就不多做介绍了. http://www.swiper ...

  5. HDU3410(单调队列)

    Passing the Message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. [No0000B7]If else 与 三元表达式? : 效率对比

    先看 if else 一段代码 using System; class Program { private static void Main() { ; ) i = -; ; Console.Writ ...

  7. css之描点定位方式

    <!-- 描点定位的两张方式 --> <!-- 1.通过id定位 --> <!-- 2.通过name定位 只能用a--> <div> <a hre ...

  8. 你不知道的document.write

    使用document.write向文档输出写内容; document.write用法:document.write("要输出的内容"); 其实document.write()有两种 ...

  9. 蓝桥网试题 java 基础练习 时间转换

    --------------------------------------- --------------------------------------- import java.util.*; ...

  10. 微服务框架概览之 Netty

    Netty 是什么 Netty 提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序 Netty 架构图 为什么选择Netty 通过对Netty的分析,我 ...