It's not a Bug, It's a Feature! (poj 1482 最短路SPFA+隐式图+位运算)
|
Language:
Default
It's not a Bug, It's a Feature!
Description
It is a curious fact that consumers buying a new software product generally do not expect the software to be bug-free. Can you imagine buying a car whose steering wheel only turns to the right?
Or a CD-player that plays only CDs with country music on them? Input
The input contains several product descriptions. Each description starts with a line containing two integers n and m, the number of bugs and patches, respectively. These values satisfy 1 <= n <= 20 and 1 <= m <= 100. This is followed by m lines describing the
m patches in order. Each line contains an integer, the time in seconds it takes to apply the patch, and two strings of n characters each. The first of these strings describes the bugs that have to be present or absent before the patch can be applied. The i-th position of that string is a ``+'' if bug bi has to be present, a ``-'' if bug bi has to be absent, and a `` 0'' if it doesn't matter whether the bug is present or not. The second string describes which bugs are fixed and introduced by the patch. The i-th position of that string is a ``+'' if bug bi is introduced by the patch, a ``-'' if bug bi is removed by the patch (if it was present), and a ``0'' if bug bi is not affected by the patch (if it was present before, it still is, if it wasn't, is still isn't). The input is terminated by a description starting with n = m = 0. This test case should not be processed. Output
For each product description first output the number of the product. Then output whether there is a sequence of patches that removes all bugs from a product that has all n bugs. Note that in such a sequence a patch may be used multiple times. If there is such
a sequence, output the time taken by the fastest sequence in the format shown in the sample output. If there is no such sequence, output ``Bugs cannot be fixed.''. Print a blank line after each test case. Sample Input 3 3 Sample Output Product 1 Source |
这个题拿到手并不会做,没有好的思路,然后就看了网上的题解。第一次碰到不建图也能SPFA的,又学习了。另外这一题的位运算处理也非常巧妙,这是我不熟悉的,先放在这里,以后多来看几遍。
參考这两篇博客,写的非常好:http://www.cnblogs.com/scau20110726/archive/2012/12/16/2820739.html
http://www.cnblogs.com/staginner/archive/2011/10/25/2223489.html
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std; #define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 25;
const int MAXN = 111;
const int MAXM = (1<<20)+100;
const int N = 1005; char s1[maxn],s2[maxn];
int s[2][MAXN],t[2][MAXN],cost[MAXN];
int dist[MAXM];
bool inq[MAXM];
int n,m; void SPFA()
{
int i,j;
mem(inq,false);
mem(dist,INF);
queue<int>Q;
int start=(1<<n)-1;
Q.push(start);
inq[start]=true;
dist[start]=0;
while (!Q.empty())
{
int u=Q.front(); Q.pop();
inq[u]=false;
for (i=0;i<m;i++)
{
if ((u|s[1][i])==u&&(u&s[0][i])==u)
{
int v=u;
v|=t[1][i];
v&=t[0][i];
if (dist[v]>dist[u]+cost[i])
{
dist[v]=dist[u]+cost[i];
if (!inq[v])
{
inq[v]=true;
Q.push(v);
}
}
}
}
}
// for(i=0;i<=start;i++)
// pf("%d ",dist[i]);
// pf("\n");
if (dist[0]==INF)
pf("Bugs cannot be fixed.\n");
else
pf("Fastest sequence takes %d seconds.\n",dist[0]);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
int i,j,cas=0;
while (sff(n,m))
{
if (n==0&&m==0) break;
mem(s,0);
mem(t,0);
for (i=0;i<m;i++)
{
scanf("%d%s%s",&cost[i],s1,s2);
for (j=0;j<n;j++)
{
if (s1[j]=='+')
s[1][i]+=(1<<j);
if (s1[j]!='-')
s[0][i]+=(1<<j);
if (s2[j]=='+')
t[1][i]+=(1<<j);
if (s2[j]!='-')
t[0][i]+=(1<<j);
}
}
pf("Product %d\n",++cas);
SPFA();
pf("\n");
}
return 0;
}
It's not a Bug, It's a Feature! (poj 1482 最短路SPFA+隐式图+位运算)的更多相关文章
- 【UVA】658 - It's not a Bug, it's a Feature!(隐式图 + 位运算)
这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...
- uva_658_It's not a Bug, it's a Feature!(最短路)
It's not a Bug, it's a Feature! Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- UVA - 658 It's not a Bug, it's a Feature! (隐式图的最短路,位运算)
隐式的图搜索,存不下边,所以只有枚举转移就行了,因为bug的存在状态可以用二进制表示,转移的时候判断合法可以用位运算优化, 二进制pre[i][0]表示可以出现的bug,那么u&pre[i][ ...
- 【uva 658】It's not a Bug, it's a Feature!(图论--Dijkstra或spfa算法+二进制表示+类“隐式图搜索”)
题意:有N个潜在的bug和m个补丁,每个补丁用长为N的字符串表示.首先输入bug数目以及补丁数目.然后就是对M个补丁的描述,共有M行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,第 ...
- 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& ...
- 错误号码2003 Can't connect to MySQL server 'localhost' (0)
错误描写叙述 错误原因 近期,我一直都能够用SQLyog连接本地数据库,可是近几天却无法连接:而且一直都报上述错误,我查阅了非常多资料,发现有非常多中说法 总结一下 第一,MySQL中的my.ini出 ...
- 退役笔记一#MySQL = lambda sql : sql + ' Source Code 4 Explain Plan '
Mysql 查询运行过程 大致分为4个阶段吧: 语法分析(sql_parse.cc<词法分析, 语法分析, 语义检查 >) >>sql_resolver.cc # JOIN.p ...
- Error creating bean with name 'com.you.user.dao.StudentDaoTest': Injection of autowired dependencies
1.错误叙述性说明 七月 13, 2014 6:37:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadB ...
- error: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)'
[root@luozhonghua ~]# /usr/bin/mysqladmin -u root password 'aaaaaa' /usr/bin/mysqladmin: connect t ...
随机推荐
- 关于vector.size()和string.length() 的返回类型 size_type
今天写循环的时候碰到一个问题,发现:string.length()返回的类型是size_type.它是unsigned 类型.string::size_type它在不同的机器上,长度是可以不同的,并非 ...
- cloudera-scm-server启动时出现Caused by: java.io.FileNotFoundException: /var/lib/cloudera-scm-server/.keystore (No such file or directory)问题解决方法(图文详解)
不多说,直接上干货! 问题详情 查看/var/log/cloudera-scm-server.log的启动日志 问题来源 我在用cloudermanager安装好之后,然后,在对如下. 配置kerbe ...
- CF126B Password
思路: kmp略作修改. 实现: #include <iostream> #include <cstdio> using namespace std; ; int neXt[M ...
- Perforce 的基本使用教程
一.简介 P4是什么 二.基本使用方法 1.下载代码 下载最新代码 Get Latest Revision 下载指定commit代码 Get Revision 2. 检出代码 选择指定目录,右键 Ch ...
- TCP的send与recv函数小结
Send函数: 在阻塞模式下, send函数的过程是将应用程序请求发送的数据拷贝到发送缓存中发送并得到确认后再返回.但由于发送缓存的存在,表现为:如果发送缓存大小比请求发送的大小要大,那么send函数 ...
- Apache 在Linux上的安装
1.获取源码 wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.37.tar.gz 2.卸载centos自带的apache 3.解压apach ...
- shouldComponentUpdate不能直接比较object
凡是参阅过react官方英文文档的童鞋大体上都能知道对于一个组件来说,其state的改变(调用this.setState()方法)以及从父组件接受的props发生变化时,会导致组件重渲染,正所谓&qu ...
- 如何在mac里面,把xcode代码同步到 tfs 的 git库(新git库)
克隆篇请参考:http://www.cnblogs.com/IWings/p/6744895.html 在mac安装visual studio code https://code.visualstud ...
- Navicat 导出为 Excel 文件
1:先选择一个表 再点击导出向导 2:看图 3:选择保存的路径 4:选择字段 5:最后执行吧
- pig常用命令
一.pig: pig提供了一个基于Hadoop的并行地执行数据流处理的引擎.它包含了一种脚本语言,称为Pig Latin.(类似SQL) 二.Pig Latin: 1.注释: 单行:-- 多行:/* ...