[BZOJ4992] [Usaco2017 Feb]Why Did the Cow Cross the Road(spfa)
2024-09-06 13:40:44
把每个点和曼哈顿距离距离它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;
}
最新文章
- iTunes安装app总是提示授权失败
- AndroidInject项目使用动态代理增加对网络请求的支持
- POJ 1584	 A Round Peg in a Ground Hole --判定点在形内形外形上
- Leetcode 98. Validate Binary Search Tree
- selenium通过WebDriverWait实现ajax测试,实现等页面元素加载完成
- 三十、【C#.Net开发框架】WCFHosting服务主机的利用WCF服务通讯和实现思路
- Mybatis配置
- linux 病毒 sfewfesfs
- Android Environment FAQ (Frequently Asked Question)
- 标准模板库(STL)学习探究之stack
- [RxJS] Marble diagrams in ASCII form
- 推荐一款JavaScript日历控件:kimsoft-jscalendar
- HDU 6069 Counting Divisors
- 从JDK源码角度看线程的阻塞和唤醒
- oracle利用redo恢复
- CAN自收自发问题小结
- 运维rpm语法
- MapReduce作业的工作原理
- nginx关于限制请求数和连接数
- Retrofit的通讯方式示例
热门文章
- C#遍历文件夹下全部文件
- Android学习总结(十)———— Intent的使用
- MFC U盘检测
- vba控制图表,excel图表,一键完成
- SQL增删查改语句
- 诊断 Grid Infrastructure 启动问题 (文档 ID 1623340.1)
- var、let、const声明变量的区别
- 51nod——1174 区间中最大的数(ST)
- URAL1561 Winnie the Pooh
- Zookeeper 集群 BindException: Cannot assign requested address 解决方案