洛谷-P2761 软件补丁问题
作者:互联网
软件补丁问题
状态压缩 + 最短路
对于当前 bug 的状态,可以作为一个点,表示的方式用状态压缩的方式,接着边就是通过一个补丁,转换到另外一个 bug 状态
直接最短路,dijkstra 或 bfs 或 SPFA 都可
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1 << 20 | 1;
const ll inf = 1e17 + 10;
ll vis[maxn];
ll pre[110][2], nex[110][2], t[110];
int n, m;
struct node
{
ll u, val;
node(){}
node(ll _u, ll _val){u = _u; val = _val;}
bool operator < (const node& a) const
{
return a.val < val;
}
};
ll dijkstra(ll s)
{
priority_queue<node>q;
q.push({s, 0ll});
while(q.size())
{
node now = q.top();
q.pop();
if(vis[now.u]) continue;
vis[now.u] = 1;
if(now.u == 0) return now.val;
for(int i=0; i<m; i++)
{
ll temp = now.u;
temp |= nex[i][0];
temp |= nex[i][1];
temp ^= nex[i][1];
if(vis[temp] == 0 && (now.u & pre[i][0]) == pre[i][0] && (now.u & pre[i][1]) == 0)
q.push({temp, now.val + t[i]});
}
}
return inf;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
ll len = (1 << n) - 1;
for(int i=0; i<m; i++)
{
cin >> t[i];
string s;
cin >> s;
for(int k=0; k<2; k++)
{
pre[i][k] = 0;
char x = k == 0 ? '+' : '-';
for(int j=0; j<n; j++)
{
pre[i][k] <<= 1;
pre[i][k] += x == s[j];
}
}
cin >> s;
for(int k=0; k<2; k++)
{
nex[i][k] = 0;
char x = k == 0 ? '+' : '-';
for(int j=0; j<n; j++)
{
nex[i][k] <<= 1;
nex[i][k] += x == s[j];
}
}
}
ll ans = dijkstra((1 << n) - 1);
if(ans == inf) ans = 0;
cout << ans << endl;
return 0;
}
标签:P2761,洛谷,vis,int,ll,补丁,include,now 来源: https://www.cnblogs.com/dgsvygd/p/16393492.html