Codeforces 821D Okabe and City 题解
来源:互联网 发布:js时间格式化 编辑:程序博客网 时间:2024/06/05 07:05
题目链接
Codeforces 821D: Okabe and City
题意
在一个n 行m 列的矩阵中,最初有k 盏路灯是亮的,Okabe 想要从坐标为
(1,1) 的位置走到坐标为(n,m) 的位置,但是Okabe 只能走路灯亮着的地方,如果Okabe 站在路灯原先就亮着的地方,他可以支付一枚硬币,来让某一行或者某一列的路灯全部都亮起来,但这只是暂时的,一旦他离开自己用硬币支付而亮起来的路灯下,这些路灯如果最初不是亮着的,就会马上熄灭,问从(1,1) 到(n,m) ,Okabe 最少需要支付多少硬币,如果他无法到达(n,m) ,则输出-1。
题解
如果两个路灯的行、列差的绝对值之和等于1,那么Okabe 就不需要支付硬币就可以走到另一个路灯下,否则,如果两个路灯的行、列差的绝对值有一个小于等于2,Okabe 就可以支付一枚硬币走到另一个路灯下,其他情况下为不可到达,这样就可以根据给定的亮着的路灯坐标构一张图,然后找单源最短路径,这里用的是spfa 单源最短路径。
有两种方法构图,一种是一边搜一边构图,另一种是先构图,然后搜最短路。前天刚学链式前向星构图,想用一下,结果在第28 个测试RE 了,因为如果所有的点都在一条直线上,就总共有5×107 条边,边太多存不下……
过题代码
#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <list>#include <map>#include <set>#include <queue>#include <stack>#include <algorithm>#include <climits>#include <cfloat>#include <string>#include <cstring>#include <ctime>#include <cstdlib>using namespace std;#define LL long longconst int inf = 100000000;const int maxn = 10005;struct Node { int x; int y;};Node node[maxn];int n, m, k, firstindex;int dis[maxn];bool end;bool inque[maxn];queue<int> que;int spfa() { // 初始化 int dist, absx, absy; for(int i = 0; i < k; ++i) { dis[i] = inf; } que.push(firstindex); inque[firstindex] = true; dis[firstindex] = 0; // spfa int tmp; while(!que.empty()) { tmp = que.front(); que.pop(); inque[tmp] = false; // 访问tmp 点连接的所有边 for(int i = 0; i < k; ++i) { if(i != tmp) { dist = inf; absx = abs(node[i].x - node[tmp].x); absy = abs(node[i].y - node[tmp].y); if(absx + absy == 1) { dist = 0; } else if(absx <= 2 || absy <= 2) { dist = 1; } if(dis[i] > dis[tmp] + dist) { dis[i] = dis[tmp] + dist; if(!inque[i]) { inque[i] = true; que.push(i); } } } } } if(dis[k - 1] == inf) { return -1; } else { return dis[k - 1]; }}int main() { // 初始化 end = false; scanf("%d%d%d", &n, &m, &k); // 输入被点亮的灯 for(int i = 0; i < k; ++i) { scanf("%d%d", &node[i].x, &node[i].y); // 判断终点是否被点亮 if(node[i].x == n && node[i].y == m) { end = true; } // 查找起点 if(node[i].x == 1 && node[i].y == 1) { firstindex = i; } } // 如果终点没有被点亮,就增加终点 if(!end) { node[k].x = n + 1; node[k++].y = m + 1; } // spfa 搜索最短路 printf("%d\n", spfa()); return 0;}
纪念一下第一次用链式前向星构图RE 代码……
#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <list>#include <map>#include <set>#include <queue>#include <stack>#include <algorithm>#include <climits>#include <cfloat>#include <string>#include <cstring>#include <ctime>#include <cstdlib>using namespace std;#define LL long longconst int inf = 100000000;const int maxn = 10005;struct Edge { int w; int to; int next;};struct Node { int x; int y;};Node node[maxn];Edge edge[1000005];int n, m, k, cnt, firstindex;int dis[maxn], head[maxn];bool end;bool inque[maxn];queue<int> que;void add(int u, int v, int w) { edge[cnt].to = v; edge[cnt].w = w; edge[cnt].next = head[u]; head[u] = cnt++;}int spfa() { // 初始化 for(int i = 0; i < k; ++i) { dis[i] = inf; } que.push(firstindex); inque[firstindex] = true; dis[firstindex] = 0; // spfa int tmp, next; while(!que.empty()) { tmp = que.front(); que.pop(); inque[tmp] = false; // 访问tmp 点连接的所有边 next = head[tmp]; while(next != -1) { if(edge[next].w + dis[tmp] < dis[edge[next].to]) { dis[edge[next].to] = edge[next].w + dis[tmp]; if(!inque[edge[next].to]) { inque[edge[next].to] = true; que.push(edge[next].to); } } next = edge[next].next; } } if(dis[k - 1] == inf) { return -1; } else { return dis[k - 1]; }}int main() { scanf("%d%d%d", &n, &m, &k); // 所有的边的next都指向-1 end = false; memset(head, -1, sizeof(head)); // 输入被点亮的灯 for(int i = 0; i < k; ++i) { scanf("%d%d", &node[i].x, &node[i].y); // 判断终点是否被点亮 if(node[i].x == n && node[i].y == m) { end = true; } // 查找起点 if(node[i].x == 1 && node[i].y == 1) { firstindex = i; } } // 如果终点没有被点亮,就增加终点 if(!end) { node[k].x = n + 1; node[k++].y = m + 1; } // 构图 for(int i = 0; i < k; ++i) { for(int j = 0; j < i; ++j) { int absx = abs(node[j].x - node[i].x); int absy = abs(node[j].y - node[i].y); if(absx + absy == 1) { add(i, j, 0); add(j, i, 0); } else if(absx <= 2 || absy <= 2) { add(i, j, 1); add(j, i, 1); } } } // spfa 搜索最短路 printf("%d\n", spfa()); return 0;}
阅读全文
1 0
- Codeforces 821D Okabe and City 题解
- Codeforces 821 D. Okabe and City
- codeforces 821d Okabe and City
- Codeforces Round #420 (Div. 2) 821D Okabe and City
- Codeforces 821D Okabe And City 最短路
- D. Okabe and City codeforces 最短路
- CF 821D Okabe and City
- CF-Codeforces Round #420 (Div. 2)-D-Okabe and City
- Codeforces 821D Okabe and City (拆点+思维建图+spfa)
- Codeforces Round #420 (Div. 2) 821D Okabe and City 思维建图+dij
- Codeforces Round #420 (Div. 2) 821D Okabe and City 思维建图+spfa
- Codeforces 821C Okabe and Boxes 题解
- Codeforces 821D Okabe and City【思维建图+Dij+优先队列优化】好题~好题~
- Codeforces Round #420 (Div. 2) D. Okabe and City (最短路)
- Codeforces Round #420 (Div. 2) D. Okabe and City(最短路)
- Codeforces Round #420 (Div. 2) D. Okabe and City 最短路
- Codeforces Round #420 (Div. 2) D. Okabe and City(最短路)
- Codeforces 821A Okabe and Future Gadget Laboratory 题解
- 网络安全传输系统(3)-加密传输
- java之模糊查询
- windows中的eclipse工程在Ubuntu中打开出现中文乱码
- solrcore的schema.xml
- Genymotion unfortunately has stopped
- Codeforces 821D Okabe and City 题解
- Why does MariaDB 10.2 use InnoDB instead of XtraDB?
- 《数据结构学习与实验指导》4-4:Windows消息队列
- DCMTK学习之无法解析的外部符号 Netbios dcmdata.lib
- Python 解决 :NameError: name 'reload' is not defined 问题
- C++——通过域名获取ip
- 创建和销毁对象
- eclipse 安装springide插件
- 欢迎使用CSDN-markdown编辑器