658 - It's not a Bug, it's a Feature!-----------优先队列和spfa----做了很长时间!!!(好题!)
2012-10-13 20:24
471 查看
可以直接用位运算去操作:
①判定某些位置是否为1,如判定2、4位置为1,则转化为判断x|0101是否等于x。
②判定某些位置是否为0,如判定2、4位置为0,则转化为判断x&1010是否等于x。
③将某些位置转化为1,如2、4位置转化为1,则令x=x|0101。
④将某些位置转化为0,如2、4位置转化为0,则令x=x&1010。
在用二进制表示状态的基础上采用这些位运算技巧之后,速度就变得比较快了。
用了两种方法(优先队列和spfa)
①判定某些位置是否为1,如判定2、4位置为1,则转化为判断x|0101是否等于x。
②判定某些位置是否为0,如判定2、4位置为0,则转化为判断x&1010是否等于x。
③将某些位置转化为1,如2、4位置转化为1,则令x=x|0101。
④将某些位置转化为0,如2、4位置转化为0,则令x=x&1010。
在用二进制表示状态的基础上采用这些位运算技巧之后,速度就变得比较快了。
用了两种方法(优先队列和spfa)
#include<cstdlib> #include<iostream> #include<sstream> #include<cstdio> #include<cmath> #include<cstring> #include <algorithm> #include<vector> #include<set> #include<queue> #define LL long long #define inf 1000000000 #define E 1e-9 #define N 100 #define M 2000000 using namespace std; int n,m,ans; char str1[25],str2[25]; int map[110][2][2],w[110]; int v[2000000]; typedef pair<int,int> pii; priority_queue<pii,vector<pii>,greater<pii> > q; int d[2000000],qu[2000000],inq[2000000]; void print(int p) { // for(int i=0; i<n; i++) // if(p&(1<<i)) // cout<<"1"; // else // cout<<"0"; } void spfa() { int r=0,f=0; int MAX=(1<<n); for(int i=0; i<MAX; i++) { d[i]=inf; inq[i]=0; } d[MAX-1]=0; qu[r++]=MAX-1; inq[MAX-1]=1; while(f<r) { int u=qu[f++]; if(f==M-1) f=0; inq[u]=0; for(int i=0; i<m; i++) { int t=u; if((t|map[i][0][0])==t&&(t&map[i][0][1])==t) { t|=map[i][1][0]; t&=(map[i][1][1]); if(d[t]>d[u]+w[i]) { d[t]=d[u]+w[i]; if(!inq[t]) { inq[t]=1; qu[r++]=t; if(r==M-1) r=0; } } } } } } int dfs() { while(!q.empty()) q.pop(); q.push(make_pair(0,(1<<n)-1)); while(!q.empty()) { pii u=q.top(); q.pop(); int p=u.second; v[p]=1;//只有出队列时,才说明不用在考虑他了!!!! if(!p) return u.first; for(int i=0; i<m; i++) { int t=p; if((t|map[i][0][0])==t&&(t&map[i][0][1])==t) { t|=map[i][1][0]; t&=(map[i][1][1]); if(v[t]) continue; q.push(make_pair(u.first+w[i],t)); } } } return -1; } int main() { #ifndef ONLINE_JUDGE freopen("ex.in","r",stdin); #endif int ncase=0; while(scanf("%d%d",&n,&m)!=EOF) { if(!n&&!m) break; memset(v,0,sizeof(v)); memset(map,0,sizeof(map)); for (int i=0; i<m; i++) { scanf("%d%s%s",&w[i],str1,str2); for(int j=0; j<n; j++) { if(str1[j]=='+') { map[i][0][0]+=(1<<j);/////////////////// } if(str2[j]=='+') { map[i][1][0]+=(1<<j); } if(str1[j]!='-') { map[i][0][1]+=(1<<j); } if(str2[j]!='-') { map[i][1][1]+=(1<<j); } } } int flag=dfs(); // spfa(); printf("Product %d\n",++ncase); // if(d[0]==inf) if(flag==-1) printf("Bugs cannot be fixed.\n"); else printf("Fastest sequence takes %d seconds.\n",flag); // printf("Fastest sequence takes %d seconds.\n",d[0]); printf("\n"); } return 0; }
相关文章推荐
- UVa 658 It's not a Bug, it's a Feature! 位运算技巧+dijkstra优先队列优化
- uva658 - It's not a Bug, it's a Feature! 状态压缩+隐式图搜索+优先队列的dijkstra
- UVa 658 It's not a Bug, it's a Feature! / SPFA
- UVA - 658 It's not a Bug, it's a Feature! SPFA
- UVA 658 It&#39;s not a Bug, it&#39;s a Feature!
- 658 - It's not a Bug, it's a Feature!
- 【UVA】658 - It's not a Bug, it's a Feature!(隐式图 + 位运算)
- HDU 1818 It's not a Bug, It's a Feature! (图论SPFA)
- UVA 658 - It's not a Bug, it's a Feature!(dijkstra+优先队列)
- 【UVA】658 - It's not a Bug, it's a Feature!(隐式图 + 位运算)
- uva_658 It's not a Bug, it's a Feature! Dijkstra(优先队列)
- UVa 658 It's not a Bug, it's a Feature! (Dijkstra+位运算技巧+好题)
- UVA 658 It's not a Bug, it's a Feature! 隐式图搜索
- UVa658 - It's not a Bug, it's a Feature!
- UVa 658It's not a Bug, it's a Feature! -- 最短路dijkstra
- uva 658 It's not a Bug, it's a Feature! 最短路
- 658 - It's not a Bug, it's a Feature! (Dijkstra算法)
- UVA 658 It's not a Bug, it's a Feature!
- UVA 658 - It's not a Bug, it's a Feature!
- It's not a Bug, It's a Feature! (poj 1482 最短路SPFA+隐式图+位运算)