这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式。

1.将s中二进制第k位变成0的处理方式:

s = s & (~(1 << pos));

将s中二进制第k位变成1的处理方式:

s = s | (1 << pos);

2.二进制运算:

[1] & : 1 & 1 = 1 , 1 & 0 = 0 , 0 & 0 = 0;

高速推断奇偶性:

    if(a & 1);//为奇数
if(!(a & 1));//为偶数

[2] |  : 1 | 1 = 1 , 0 | 1 = 1 , 0 | 0 = 0;

[3] ~ : (按位取反) 比方 10011 取反后为 01100;

[4] ^  :  
假设參加运算的两个二进制同号,结果为0,异号结果为1,比方 1 ^ 1 = 0 , 0 ^ 0 = 0 ,1 ^ 0 = 1 , 0 ^ 1 = 1;

使用方法的话:比方101010100 将后4位翻转,那么能够让它 ^ 000001111 (相当于对指定的一段二进制位取反了)

交换2个值不运用暂时变量:

    a = a ^ b;
b = b ^ a;
a = a ^ b;

资料:http://www.cnblogs.com/qkhhxkj/archive/2011/06/29/2093894.html

14062589 658 It's not a Bug, it's a Feature! Accepted C++ 2.525 2014-08-19 03:16:25

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<string>
#include<sstream>
#include<ctime>
using namespace std;
#define _PI acos(-1.0)
#define INF (1 << 10)
#define esp 1e-9
#define MOD 100000007
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pill;
/*===========================================
===========================================*/
#define MAXD 100 + 10
int n,m;
struct Bug{
vector<int>s[2];
vector<int>c[2];
int cost ;
}bug[MAXD];
int Judge(int s,int cur){
for(int i = 0 ; i < bug[cur].s[0].size(); i++){
int pos = bug[cur].s[0][i]; /*必须有bug的位置*/
if(!((s >> pos) & 1))
return -1;
}
for(int i = 0 ; i < bug[cur].s[1].size(); i++){
int pos = bug[cur].s[1][i]; /*必须没bug的位置*/
if((s >> pos) & 1)
return -1;
}
for(int i = 0 ; i < bug[cur].c[0].size(); i++){
int pos = bug[cur].c[0][i]; /*改完之后存在BUG的地方*/
s = s | (1 << pos);
}
for(int i = 0 ; i < bug[cur].c[1].size(); i++){
int pos = bug[cur].c[1][i]; /*改完之后不存在BUG的地方*/
s = s & (~(1 << pos));
}
return s;
}
int solve(){
int s = (1 << n) - 1;
map<int,int>vis;
priority_queue<pill,vector<pill>,greater<pill> >q;
q.push(make_pair(0,s));
while(!q.empty()){
pill temp = q.top(); q.pop();
int dist = temp.first;
int node = temp.second;
vis[node] = 1;
if(node == 0) /*全部bug都弥补了*/
return dist;
for(int i = 0 ; i < m ; i++){
int ans = Judge(node,i);
if(ans >= 0 && !vis[ans]){
q.push(make_pair(dist + bug[i].cost,ans));
}
}
}
return -1;
}
int main(){
int Case = 1;
while(scanf("%d%d",&n,&m)){
if(!n && !m)
break;
for(int i = 0 ; i < m ; i++){
char str[MAXD];
scanf("%d%s",&bug[i].cost,str);
bug[i].s[0].clear(); bug[i].s[1].clear();
bug[i].c[0].clear(); bug[i].c[1].clear();
int L = strlen(str);
for(int j = 0 ; j < L; j++)
if(str[j] == '+')
bug[i].s[0].push_back(L - j - 1); /*必须有补丁的位置*/
else if(str[j] == '-')
bug[i].s[1].push_back(L - j - 1); /*必须没补丁的位置*/
scanf("%s",str);
L = strlen(str);
for(int j = 0 ; j < L; j++)
if(str[j] == '+')
bug[i].c[0].push_back(L - j - 1); /*打完后有补丁的位置*/
else if(str[j] == '-')
bug[i].c[1].push_back(L - j - 1); /*打完没补丁的位置*/
}
int ans = solve();
printf("Product %d\n",Case ++);
if(ans < 0)
printf("Bugs cannot be fixed.\n");
else
printf("Fastest sequence takes %d seconds.\n",ans);
printf("\n");
}
return 0;
}

【UVA】658 - It&#39;s not a Bug, it&#39;s a Feature!(隐式图 + 位运算)的更多相关文章

  1. It&#39;s not a Bug, It&#39;s a Feature! (poj 1482 最短路SPFA+隐式图+位运算)

    Language: Default It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Su ...

  2. UVA 658 状态压缩+隐式图+优先队列dijstla

    不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...

  3. 【uva 658】It's not a Bug, it's a Feature!(图论--Dijkstra或spfa算法+二进制表示+类“隐式图搜索”)

    题意:有N个潜在的bug和m个补丁,每个补丁用长为N的字符串表示.首先输入bug数目以及补丁数目.然后就是对M个补丁的描述,共有M行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,第 ...

  4. UVA - 658 It's not a Bug, it's a Feature! (隐式图的最短路,位运算)

    隐式的图搜索,存不下边,所以只有枚举转移就行了,因为bug的存在状态可以用二进制表示,转移的时候判断合法可以用位运算优化, 二进制pre[i][0]表示可以出现的bug,那么u&pre[i][ ...

  5. uva_658_It&#39;s not a Bug, it&#39;s a Feature!(最短路)

    It's not a Bug, it's a Feature! Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & ...

  6. UVa 658 (Dijkstra) It's not a Bug, it's a Feature!

    题意: 有n个BUG和m个补丁,每个补丁用一个串表示打补丁前的状态要满足的要求,第二个串表示打完后对补丁的影响,还有打补丁所需要的时间. 求修复所有BUG的最短时间. 分析: 可以用n个二进制位表示这 ...

  7. 倒水问题UVA 10603——隐式图&&Dijkstra

    题目 给你三个容量分别为 $a,b,c$ 的杯子,最初只有第3个杯子装满了水,其他两个杯子为空.最少需要到多少水才能让一个某个杯子中的水有 $d$ 升呢?如果无法做到恰好 $d$ 升,就让某个杯子里的 ...

  8. UVa 10603 Fill (BFS && 经典模拟倒水 && 隐式图)

    题意 : 有装满水的6升的杯子.空的3升杯子和1升杯子,3个杯子中都没有刻度.不使用道具情况下,是否可量出4升水呢? 你的任务是解决一般性的问题:设3个杯子的容量分别为a, b, c,最初只有第3个杯 ...

  9. UVa 658 - It's not a Bug, it's a Feature!(Dijkstra + 隐式图搜索)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. 浅谈Struts2(一)

    一.Struts2引言 1.Struts2框架的概念 解决的MVC开发过程中,控制器(Controller)的通用问题. a.什么是MVC开发 MVC开发是一种编程思想,由设计者人为的把一个项目,划分 ...

  2. Maven+SpringMVC+MyBatis 上传图片

    上传文件我一直都觉得很难,好吧,所有涉及文件操作的我都觉得不容易.然后今天尝试了从网页上传图片保存到服务器.这个例子的前提是搭建好了服务器端框架:Maven+Spring MVC+MyBatis.当然 ...

  3. BZOJ 1218: [HNOI2003]激光炸弹( 前缀和 + 枚举 )

    虽然source写着dp , 而且很明显dp可以搞...但是数据不大 , 前缀和 + 枚举也水的过去..... -------------------------------------------- ...

  4. Arduino 时钟模块(clock module) DS1306

    http://www.pjrc.com/teensy/td_libs_DS1307RTC.html 下载相关的库程序 连接: DS1306: 1.接3.3V 2.SDA接A4 3.SCL接A5 读取: ...

  5. 不小心中了machook病毒

    此文运用的是优雅的Markdown而书 前段回家过年的日子,我心爱的小air在运行时不停的弹出"machook停止运行"的提醒,上网google一下不看不要紧,才得知是mac上的一 ...

  6. Symfony框架系列----常用命令

    一.常用命令 从Entity操作数据库: app/console doctrine:database:create # 创建数据库 app/console doctrine:schema:update ...

  7. [LeetCode]题解(python):072-Edit Distance

    题目来源: https://leetcode.com/problems/edit-distance/ 题意分析: word1最少通过多少步可以变成word2.word1只能进行一下的操作.a)插入一个 ...

  8. ECMAScript 5中新增的数组方法

    ECMAScript 5中定义了9个新的数组方法,用于遍历.映射.过滤.检测.简化和搜索数组. 在开始介绍之前,很有必要对这几个新增的数组方法做一个概述.首先,大多数方法的第一个参数接收一个函数,并且 ...

  9. java项目组会议纪要

    上周五项目经理开例会让我记录会议纪要,以下是我记录的纪要.给大家分享一下! 一.时间:2014年04月25日 二.地点:研发部 三.人物:xx,xx,xx 四.内容(相关项目的一些事项): 1.对待需 ...

  10. mahout贝叶斯算法开发思路(拓展篇)2

    如果想直接下面算法调用包,可以直接在mahout贝叶斯算法拓展下载,该算法调用的方式如下: $HADOOP_HOME/bin hadoop jar mahout.jar mahout.fansy.ba ...