[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 ...
随机推荐
- Gym 100463A Crossings (树状数组 逆序对)
Crossings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description ...
- 获取androdmanifest里面的meta-data
/* * Copyright 2017 JessYan * * Licensed under the Apache License, Version 2.0 (the "License&qu ...
- NO.6: 若不想编译器提供自动生成的函数,就应该明确拒绝
1.为驳回编译器自动生成函数的技能,可把这些函数的声明放入private,如果是继承类型可把base class的这些函数声明private,可在编译期间得到警告
- [转]Robotium环境搭建中的Errors running builder 'Android Resource Manag
转自:http://blog.sina.com.cn/s/blog_68f262210102v75t.html 最近学习了Robotium测试框架,当然学习任何一个框架或是语言之前,第一步就是搭建环境 ...
- 在kubernetes集群中创建redis主从多实例
分类 > 正文 在kubernetes集群中创建redis主从多实例 redis-slave镜像制作 redis-master镜像制作 创建kube的配置文件yaml 继续使用上次实验环境 ht ...
- 第一节 Spring的环境搭建
正在构建,扫一扫,敬请期待 和玩得来的人在一起玩才叫玩! 和玩不来的人在一起玩,那种感觉就像加班啊! 关注胖个人微信公众账号,希望对各位学生有所帮助! --胖先生 Spring框架,什么是Sprin ...
- linux常用端口查询
0 | 无效端口,通常用于分析操作系统1 | 传输控制协议端口服务多路开关选择器2 | 管理实用程序3 | 压缩进程5 | 远程作业登录7 | 回显9 | 丢弃11 | 在线用户13 | 时间17 | ...
- vue npm start 自动打开网页
打开config/index.js,设置: autoOpenBrowser: true,(默认是false,把false改为true即可)
- 在ajax请求后台时在请求标头RequestHeader加token
情景:为了保证系统数据的安全性,一般前后台之间的数据访问会有授权与验证,这里的Token机制相对于Cookie支持跨域访问,在RESTful API里面,验证一般可以使用POST请求来通过验证,使服务 ...
- bzoj千题计划218:bzoj2333: [SCOI2011]棘手的操作
http://www.lydsy.com/JudgeOnline/problem.php?id=2333 上次那个是线段树,再发一个左偏树 维护两种左偏树 第一种是对每个联通块维护一个左偏树 第二种是 ...