1. 程式人生 > >洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G

洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G

ron truct com ostream mat als then con copy

//神題目(題目一開始就理解錯了)。。。

題目描述

Why did the cow cross the road? Well, one reason is that Farmer John‘s farm simply has a lot of roads, making it impossible for his cows to travel around without crossing many of them.

奶牛們為什麽要穿馬路?一個原因只是因為FJ的牧場的路實在是太多了,使得奶牛們每天不得不穿梭在許許多多的馬路中央

FJ‘s farm is arranged as an N \times NN×N square grid of fields (3 \leq N \leq 1003N100), with a set of N-1N1 north-south roads and N-1N1 east-west roads running through the interior of the farm serving as dividers between the fields. A tall fence runs around the external perimeter, preventing cows from leaving the farm. Bessie the cow can move freely from any field to any other adjacent field (north, east, south, or west), as long as she carefully looks both ways before crossing the road separating the two fields. It takes her TT units of time to cross a road (0 \leq T \leq 1,000,0000T1,000,000).

FJ的牧場可以看作是一塊 N\times NN×N 的田地(3\le N\le 1003N100),N-1N1 條南北向的道路和 N-1N1 條東西向的道路貫穿整個牧場,同時是每塊田野的分界線。牧場的最外面是一圈高大的柵欄以防止奶牛離開牧場。Bessie只要穿過分離兩塊田野的道路,就可以從任何田野移動到與其相鄰的田野裏去(北,東,南或西)。當然,Bessie穿過每一條馬路都是需要TT 時間的。(0\le T\le 1,000,0000T1,000,000)

One day, FJ invites Bessie to visit his house for a friendly game of chess. Bessie starts out in the north-west corner field and FJ‘s house is in the south-east corner field, so Bessie has quite a walk ahead of her. Since she gets hungry along the way, she stops at every third field she visits to eat grass (not including her starting field, but including possibly the final field in which FJ‘s house resides). Some fields are grassier than others, so the amount of time required for stopping to eat depends on the field in which she stops.

有一天,FJ邀請Bessie來他家下棋,Bessie從牧場的西北角出發,FJ的家在牧場的東南角。因為Bessie在中途可能會餓,所以她每走過三塊田野就要停下來,享用她所在田野上的新鮮的牧草(不包括Bessie的出發點,但是可能會包括終點FJ的家),牧場上有些田野的牧草長得比其他地方茂盛,所以Bessie對應的停留時間也會變長。

Please help Bessie determine the minimum amount of time it will take to reach FJ‘s house.

請幫幫Bessie計算出她走到FJ家的最短時間。

輸入輸出格式

輸入格式:

The first line of input contains NN and TT. The next NN lines each contain NN positive integers (each at most 100,000) describing the amount of time required to eat grass in each field. The first number of the first line is the north-west corner.

接下來 NN 行,每行 NN 個數表示每塊田野Bessie需要停留的時間(每塊最多不超過100,000100,000),第一行的第一塊田野是牧場的西北角

輸出格式:

Print the minimum amount of time required for Bessie to travel to FJ‘s house.

一行一個整數表示Bessie走到FJ家的最短時間

輸入輸出樣例

輸入樣例#1: 復制
4 2
30 92 36 10
38 85 60 16
41 13 5 68
20 97 13 80
輸出樣例#1: 復制
31

說明

The optimal solution for this example involves moving east 3 squares (eating the "10"), then moving south twice and west once (eating the "5"), and finally moving south and east to the goal.

對於樣例,Bessie先向東走過了三塊田野(在“10”停留),再向南走兩步,又向西走了一步(在“5”停留),最後向南走一步,再向東走一步到達FJ的家(不用停留),總共時間是15(停留時間)+16(穿馬路時間)=31

感謝@jzqjzq 提供翻譯

題目解釋:牛可以往回走

例: 1 ——2——3

  牛可以1->2->1->2;

技術分享

就可以類比於牛走一步和走三步(但是走一步時的時間也是加上走3步的時間)

將所有的方向枚舉出來。。。然後。。寬搜?

沒了。。。

(根本沒有洛谷原來的題解麻煩嘛)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<queue>
 6 using namespace std;
 7 int n,v[110][110];
 8 long long dis[110][110],t;
 9 bool vis[110][110];
10 struct node
11 {
12     int x,y;
13 };
14 int dx[]={0,0,3,-3,1,1,2,2,-1,-1,-2,-2,0,0,1,-1};
15 int dy[]={3,-3,0,0,2,-2,1,-1,2,-2,1,-1,1,-1,0,0};
16 void spfa(int x,int y)
17 {
18     queue<node>q;
19     q.push((node){x,y});
20     memset(dis,0x7f,sizeof(dis));
21     dis[x][y]=0; vis[x][y]=1;
22     do
23     {
24     node u=q.front();q.pop();
25         vis[u.x][u.y]=0;
26         for(int i=0;i<16;i++)
27         {    int tx=u.x+dx[i],ty=u.y+dy[i];
28             if(tx>=1&&tx<=n&&ty>=1&&ty<=n)
29             {
30              if(dis[tx][ty]>dis[u.x][u.y]+v[tx][ty]+t*3)
31             {    
32                 dis[tx][ty]=dis[u.x][u.y]+v[tx][ty]+t*3;
33                 if(!vis[tx][ty])
34                 {
35                     vis[tx][ty]=1;q.push((node){tx,ty});
36                     }
37                 }
38             }
39         }
40     }while(q.size()!=0);
41 }
42 int main()
43 {    ios::sync_with_stdio(false);
44     cin>>n>>t;
45     for(int i=1;i<=n;i++)
46         for(int j=1;j<=n;j++)
47             cin>>v[i][j];
48     spfa(1,1);
49     long long ans;
50     ans=dis[n][n];//參照題解:牛不可能直接到達,所以得枚舉終點3步之內的所有點的值+位移的時間; 
51     ans=min(ans,dis[n][n-1]+t);
52     ans=min(ans,dis[n-1][n]+t);
53     ans=min(ans,dis[n][n-2]+t*2);
54     ans=min(ans,dis[n-2][n]+t*2);
55     ans=min(ans,dis[n-1][n-1]+t*2);
56     printf("%lld",ans);
57     return 0;
58 }
59     

洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G