前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >3018: [Usaco2012 Nov]Distant Pastures

3018: [Usaco2012 Nov]Distant Pastures

作者头像
HansBug
发布2018-04-11 10:27:31
6890
发布2018-04-11 10:27:31
举报
文章被收录于专栏:HansBug's LabHansBug's Lab

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秒掉此题的人了么么哒)

 1 /**************************************************************
 2     Problem: 3018
 3     User: HansBug
 4     Language: Pascal
 5     Result: Accepted
 6     Time:904 ms
 7     Memory:808 kb
 8 ****************************************************************/
 9  
10 type
11     point=^node;
12     node=record
13                g,w:longint;
14                next:point;
15     end;
16 var
17    i,j,k,l,m,n,x,y,f,r:longint;
18    a:array[0..10000] of point;
19    c,g:array[0..10000] of longint;
20    d:array[0..100000] of longint;
21    b:array[0..50,0..50] of longint;
22    ch:char;p:point;
23 function max(x,y:longint):longint;
24          begin
25               if x>y then max:=x else max:=y;
26          end;
27 function trans(x,y:longint):longint;
28          begin
29               exit((x-1)*n+y);
30          end;
31 procedure add(x,y,z:longint);
32           var p:point;
33           begin
34                new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p;
35                new(p);p^.g:=x;p^.w:=z;p^.next:=a[y];a[y]:=p;
36           end;
37 procedure spfa(z:longint);
38           var i,j,f,r:longint;p:point;
39           begin
40                fillchar(g,sizeof(g),0);
41                fillchar(c,sizeof(c),-1);
42                d[1]:=z;f:=1;r:=2;g[z]:=1;c[z]:=0;
43                while f<r do
44                      begin
45                           p:=a[d[f]];
46                           while p<>nil do
47                                 begin
48                                      if (c[p^.g]=-1) or (c[p^.g]>(c[d[f]]+p^.w)) then
49                                         begin
50                                              c[p^.g]:=c[d[f]]+p^.w;
51                                              if g[p^.g]=0 then
52                                                 begin
53                                                      g[p^.g]:=1;
54                                                      d[r]:=p^.g;
55                                                      inc(r);
56                                                 end;
57                                         end;
58                                      p:=p^.next;
59                                 end;
60                           g[d[f]]:=0;inc(f);
61                      end;
62                for i:=1 to n do for j:=1 to n do l:=max(l,c[trans(i,j)]);
63           end;
64  
65 begin
66      readln(n,x,y);
67      for i:=1 to n*n do a[i]:=nil;
68      for i:=1 to n do
69          begin
70               for j:=1 to n do
71                   begin
72                        read(ch);
73                        case ch of
74                             '(':b[i,j]:=1;
75                             ')':b[i,j]:=0;
76                        end;
77                   end;
78               readln;
79          end;
80      for i:=1 to n do
81          for j:=1 to n do
82              begin
83                   if i<n then add(trans(i,j),trans(i+1,j),abs(b[i,j]-b[i+1,j])*(y-x)+x);
84                   if j<n then add(trans(i,j),trans(i,j+1),abs(b[i,j]-b[i,j+1])*(y-x)+x);
85              end;
86      l:=0;
87      for i:=1 to n*n do spfa(i);
88      writeln(l);
89      readln;
90 end.    
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-04-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 3018: [Usaco2012 Nov]Distant Pastures
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • HINT
  • Source
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档