3018: [Usaco2012 Nov]Distant Pastures

时间:2022-10-08 06:29:21

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.