[USACO17FEB]Why Did the Cow Cross the Road I G
一开始想写$DP$,发现直接转移完全有后效性
所以本小蒟蒻写了个最短路
每走三步就要吃草是这个题最难搞的地方,我们建图时不妨只对于距离等于三的点连边
考虑完全覆盖所有情况,从一个点走一步,两步,然后三步,和直接走三步代价是等价的
这样从每个点到与其曼哈顿距离为三的所有点连边即可
考虑到终点的答案,对于所有小于三步到终点的位置到终点的代价,找到最小值即为答案
有个坑就是比如右左右这种走法,我们也需要从一个点向其周围的点连边
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define int long long
using namespace std;
const int maxn=;
struct edge{
int next,to,dis;
}e[*maxn];
int n,t,g[][],len[maxn],head[maxn],cnt,ans=1e9;
int dx[]={-,-,-,-,-,,,,,,,,,,,-};
int dy[]={-,-,,,,-,-,-,,,,,,,-,};
bool exist[maxn];
inline void add(int x,int y,int d)
{
e[++cnt].next=head[x];
e[cnt].to=y;
e[cnt].dis=d;
head[x]=cnt;
}
inline int make(int x,int y)
{
return x*n+y;
}
int dijkstra()
{
priority_queue<pair<int,int> >q;
memset(len,0x3f,sizeof(len));
q.push(make_pair(,make(,)));
len[make(,)]=;
while(!q.empty())
{
int u=q.top().second;
q.pop();
if(exist[u])
continue;
exist[u]=;
for(int v,i=head[u];i;i=e[i].next)
if(len[v=e[i].to]>len[u]+e[i].dis)
{
len[v]=len[u]+e[i].dis;
q.push(make_pair(-len[v],v));
}
}
}
void check(int x,int y)
{
for(int i=;i<;i++)
if(x+dx[i]>&&x+dx[i]<=n&&y+dy[i]>&&y+dy[i]<=n)
add(make(x,y),make(x+dx[i],y+dy[i]),*t+g[x+dx[i]][y+dy[i]]);
}
signed main()
{
scanf("%lld%lld",&n,&t);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%lld",&g[i][j]);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
check(i,j);
dijkstra();
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(*n-i-j<)
ans=min(ans,len[make(i,j)]+(*n-i-j)*t);
printf("%lld\n",ans);
return ;
}
[USACO17FEB]Why Did the Cow Cross the Road I G的更多相关文章
- 洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G
//神题目(题目一开始就理解错了)... 题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's far ...
- [Luogu3659][USACO17FEB]Why Did the Cow Cross the Road I G
题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...
- 洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)
题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer ...
- P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】
题外话:维护区间交集子集的小套路 开两个树状数组,一个维护进入区间,一个维护退出区间 $Query:$ 给定询问区间$l,r$和一些其他区间,求其他区间中与$[l,r]$交集非空的区间个数 用上面维护 ...
- [USACO17FEB]Why Did the Cow Cross the Road III G
嘟嘟嘟 首先看到这种序列的问题,我就想到了逆序对,然后就想如何把这道题转化. 首先要满足这个条件:ai <bi.那么我们把所有数按第一次出现的顺序重新赋值,那么对于新的数列,一定满足了ai &l ...
- [USACO17FEB]Why Did the Cow Cross the Road III G (树状数组,排序)
题目链接 Solution 二维偏序问题. 现将所有点按照左端点排序,如此以来从左至右便满足了 \(a_i<a_j\) . 接下来对于任意一个点 \(j\) ,其之前的所有节点都满足 \(a_i ...
- P3660 [USACO17FEB]Why Did the Cow Cross the Road III G
Link 题意: 给定长度为 \(2N\) 的序列,\(1~N\) 各处现过 \(2\) 次,i第一次出现位置记为\(ai\),第二次记为\(bi\),求满足\(ai<aj<bi<b ...
- 洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S
P3662 [USACO17FEB]Why Did the Cow Cross the Road II S 题目描述 The long road through Farmer John's farm ...
- 洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S
P3663 [USACO17FEB]Why Did the Cow Cross the Road III S 题目描述 Why did the cow cross the road? Well, on ...
随机推荐
- PSP(3.13——3.15)以及周记录
3.13 0:00 4:00 10 230 Cordova插件1,的尝试 N min 10:50 11:30 10 30 英语百词斩 N min 18:00 22:55 20 275 写博客 Y mi ...
- win 批处理
前言 批处理文件(batch file)包含一系列 DOS命令,通常用于自动执行重复性任务.用户只需双击批处理文件便可执行任务,而无需重复输入相同指令.编写批处理文件非常简单,但难点在于确保一切按顺序 ...
- FFT自看
https://www.cnblogs.com/RabbitHu/p/FFT.html 先去看这个... 我觉得代码还是https://blog.csdn.net/WADuan2/article/d ...
- 【刷题】BZOJ 2001 [Hnoi2010]City 城市建设
Description PS国是一个拥有诸多城市的大国,国王Louis为城市的交通建设可谓绞尽脑汁.Louis可以在某些城市之间修建道路,在不同的城市之间修建道路需要不同的花费.Louis希望建造最少 ...
- 学习Spring Boot:(十一) 自定义装配参数
前言 SpringMVC 中 Controller 中方法的参数非常灵活,得益于它的强大自动装配,这次将根据上次遗留下的问题,将研究下装配参数. 正文 SpringMVC中使用了两个接口来处理参数: ...
- 【BZOJ1071】[SCOI2007]组队(神仙题)
[BZOJ1071][SCOI2007]组队(神仙题) 题面 BZOJ 洛谷 题解 首先把式子整理一下,也就是\(A*h+B*v\le C+A*minH+B*minV\) 我们正常能够想到的做法是钦定 ...
- 【uoj7】 NOI2014—购票
http://uoj.ac/problem/7 (题目链接) 题意 给出一棵有根树,每次从一个节点出发可以买票到达它的一定范围内的祖先.问对于每一个点,到达根的最小花费是多少. Solution 右转 ...
- 【bzoj4066】 简单题
http://www.lydsy.com/JudgeOnline/problem.php?id=4066 (题目链接) 题意 维护一个矩阵,两个操作,给某一个元素加上A,求其中一个子矩阵的元素之和.强 ...
- RabbitMQ的生产者和消费者
低级错误:启动程序的时候报错:socket close: 原因在配置文件中写的端口是:15672,应该是5672: client端通信口5672管理口15672server间内部通信口25672erl ...
- 改变 小程序 select 多选框 选中图片
https://www.jianshu.com/p/11eb5b0bfe1a 注意 博客介绍的 在 wxss backgroung-image 中引入小程序内图片是不可的,传到cdn上才实现