HDU - 4474 bfs好题
这个BFS并不是很好想。。 最主要的一点是每个余数只会被拿出来一次更新其他余数, 然后我用d[ i ]表示
到达 i 这个余数最短需要多长,然后从高位往低位贪心,判断成立的时候忘记了如果0被ban掉了这个判断会
出问题,都想到这里了为什么没有想到直接去bfs找答案呢??? 我TM蠢爆。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int n, m, tot, vis[N], from[N], who[N], ans[N];
bool is[];
void init() {
tot = ;
memset(vis, , sizeof(vis));
memset(is, , sizeof(is));
}
int main() {
int cas = ;
while(scanf("%d%d", &n, &m) != EOF) {
init();
for(int i = ; i <= m; i++) {
int x; scanf("%d", &x);
is[x] = true;
}
queue<int> que;
for(int i = ; i < ; i++) {
if(is[i]) continue;
if(i%n==) {
ans[tot++] = i;
break;
}
vis[i%n] = ;
from[i%n] = ;
who[i%n] = i;
que.push(i%n);
}
if(!tot) {
while(!que.empty() && !tot) {
int u = que.front(); que.pop();
for(int i = ; i < ; i++) {
if(is[i]) continue;
int v = (u*+i)%n;
if(!v) {
ans[tot++] = i;
while(u) {
ans[tot++] = who[u];
u = from[u];
}
reverse(ans, ans + tot);
break;
}
if(vis[v]) continue;
vis[v] = vis[u] + ;
from[v] = u;
who[v] = i;
que.push(v);
}
}
}
printf("Case %d: ", cas++);
if(!tot) puts("-1");
else {
for(int i = ; i < tot; i++)
printf("%d", ans[i]);
puts("");
}
}
return ;
} /*
*/
HDU - 4474 bfs好题的更多相关文章
- Saving Princess claire_(hdu 4308 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- BFS简单题套路_Codevs 1215 迷宫
BFS 简单题套路 1. 遇到迷宫之类的简单题,有什么行走方向的,先写下面的 声明 ; struct Status { int r, c; Status(, ) : r(r), c(c) {} // ...
- POJ-2251 Dungeon Master (BFS模板题)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】
Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- hdu 4474 大整数取模+bfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474 (a*10+b)%c = ((a%c)*10+b%c)%c; 然后从高位开始枚举能填的数字填充, ...
- HDU 1312 (BFS搜索模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...
- HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...
随机推荐
- struct和typedef struct区别
分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int a; }Stu; 于是在声明变量的时候就可 ...
- windows10安装配置scikit-learn步骤
安装python-2.7.13.msi(到C:\Python27),把C:\Python27和C:\Python27\Scripts添加到系统环境变量 安装numpy:下载numpy-1.11.3+m ...
- c++刷题(27/100)反转单项链表,链表的倒数第k个
题目1:调整数组顺序使奇数位于偶数前面 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位 ...
- 如何清理休眠文件(hiberfil.sys)
如果使用了休眠功能,那么打开系统盘就会有一个很大(5.36G)的hiberfil.sys文件,它是将用户正在运行的程序,保存在这里,再启动系统就很快了.如要清理它(不用休眠功能,或者临时腾出空间),可 ...
- atan2 atan
atan2 definition In terms of the standard arctan function, whose range is (−π/2, π/2), it can be exp ...
- python端口扫描
简易版: #author:Blood_Zero #coding:utf-8 import socket import sys PortList=[21,22,23,25,80,135] # host= ...
- UBIFS文件系统简介 与 利用mkfs.ubifs和ubinize两个工具制作UBI镜像 (完整理解版本)
UBI文件系统简介 在linux-2.6.27以前,谈到Flash文件系统,大家很多时候多会想到cramfs.jffs2.yaffs2等文件系统. 它们也都是基于文件系 统+mtd+flash设备的架 ...
- The data protection operation was unsuccessful. This may have been caused by not having the user profile loaded for the current thread's user context,
在iis7.0布署网站后运行的错误,大致意思是:数据保护操作是不成功的.这可能是由于没有为当前线程的用户加载用户配置文件的导致 解决办法: 先为自己的网站新建一个应用程序池,然后新建的应用程序池上右键 ...
- idea中使用tomcat 方式启动spring boot项目
Spring boot 的main 入口启动方式相信都会用,直接运行main直接就启动了,但是往往这种方式并不是最佳的启动方式,比如运维的层面更希望调整tomcat的调优参数,而只使用嵌入启动方式很难 ...
- [NOI2014]购票 「树上斜率优化」
首先易得方程,且经过变换有 $$\begin{aligned} f_i &= \min\limits_{dist_i - lim_i \le dist_j} \{f_j + (dist_i - ...