传送门

把每个点和曼哈顿距离距离它3步或1步的点连一条边,边权为3 * t + a[x][y]

因为,走3步,有可能是3步,也有可能是1步(其中一步拐了回来)

最后,把终点和曼哈顿距离距离它1步和2布的点连一条边,边权为 步数 * t

跑一边spfa即可

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 1000001
#define idx(i, j) ((i - 1) * n + j) using namespace std; int n, t, cnt;
int a[101][101], d[4][N][2], tot[4];
int head[N], to[N], next[N], val[N], dis[N];
bool vis[N];
queue <int> q; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline void add(int x, int y, int z)
{
to[cnt] = y;
val[cnt] = z;
next[cnt] = head[x];
head[x] = cnt++;
} inline void spfa()
{
int i, u, v;
memset(dis, 127, sizeof(dis));
dis[1] = 0;
q.push(1);
while(!q.empty())
{
u = q.front();
q.pop();
vis[u] = 0;
for(i = head[u]; ~i; i = next[i])
{
v = to[i];
if(dis[v] > dis[u] + val[i])
{
dis[v] = dis[u] + val[i];
if(!vis[v])
{
q.push(v);
vis[v] = 1;
}
}
}
}
} int main()
{
int i, j, k, l, x, y;
n = read();
t = read();
for(i = 1; i <= 3; i++)
for(j = 0; j <= i; j++)
{
d[i][++tot[i]][0] = j, d[i][tot[i]][1] = i - j;
d[i][++tot[i]][0] = j, d[i][tot[i]][1] = -i + j;
d[i][++tot[i]][0] = -j, d[i][tot[i]][1] = i - j;
d[i][++tot[i]][0] = -j, d[i][tot[i]][1] = -i + j;
}
memset(head, -1, sizeof(head));
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
a[i][j] = read();
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
for(l = 1; l <= 3; l += 2)
for(k = 1; k <= tot[l]; k++)
{
x = i + d[l][k][0];
y = j + d[l][k][1];
if(1 <= x && x <= n && 1 <= y && y <= n)
add(idx(i, j), idx(x, y), 3 * t + a[x][y]);
}
for(i = 1; i <= 2; i++)
for(j = 1; j <= tot[i]; j++)
{
x = n + d[i][j][0];
y = n + d[i][j][1];
if(1 <= x && x <= n && 1 <= y && y <= n)
add(idx(x, y), n * n, i * t);
}
spfa();
printf("%d\n", dis[n * n]);
return 0;
}

  

最新文章

  1. iTunes安装app总是提示授权失败
  2. AndroidInject项目使用动态代理增加对网络请求的支持
  3. POJ 1584 A Round Peg in a Ground Hole --判定点在形内形外形上
  4. Leetcode 98. Validate Binary Search Tree
  5. selenium通过WebDriverWait实现ajax测试,实现等页面元素加载完成
  6. 三十、【C#.Net开发框架】WCFHosting服务主机的利用WCF服务通讯和实现思路
  7. Mybatis配置
  8. linux 病毒 sfewfesfs
  9. Android Environment FAQ (Frequently Asked Question)
  10. 标准模板库(STL)学习探究之stack
  11. [RxJS] Marble diagrams in ASCII form
  12. 推荐一款JavaScript日历控件:kimsoft-jscalendar
  13. HDU 6069 Counting Divisors
  14. 从JDK源码角度看线程的阻塞和唤醒
  15. oracle利用redo恢复
  16. CAN自收自发问题小结
  17. 运维rpm语法
  18. MapReduce作业的工作原理
  19. nginx关于限制请求数和连接数
  20. Retrofit的通讯方式示例

热门文章

  1. C#遍历文件夹下全部文件
  2. Android学习总结(十)———— Intent的使用
  3. MFC U盘检测
  4. vba控制图表,excel图表,一键完成
  5. SQL增删查改语句
  6. 诊断 Grid Infrastructure 启动问题 (文档 ID 1623340.1)
  7. var、let、const声明变量的区别
  8. 51nod——1174 区间中最大的数(ST)
  9. URAL1561 Winnie the Pooh
  10. Zookeeper 集群 BindException: Cannot assign requested address 解决方案