Network flow problem [24] [Luogu P2761] software patch problem

Subject to the effect:

Has a length \ (n-\) contains only the '-' or '+' character string beginning all '+', and \ (m \) operations, each operation has a time \ (C \) , containing only two '-', '+', '0' of the character string \ (a \) and the string \ (B \) , if the string and the string \ (a \) is equal to (string \ (a \) position '0' is not), then the string can be changed string \ (B \) (string \ (a \) in the '0' position is not), each operation to consume \ (c \) . Q. How much time for at least the whole string into a '-'.

text:

Since \ (n-\ Leq 20 is \) , the state of compression can be used, '-' and '+' respectively '0' and '1' of FIG. The original problem, we can use bit operation to determine whether the original string and the string \ (A \) is equal to:

if((u & b1[i]) == b1[i] && (u & b2[i]) == 0)

( b1[i]And b2[i]it is mentioned in the text.)

According to the original question may still be a string into a string \ (B \) :

((u | f1[i]) ^ f1[i]) | f2[i]

( f1[i]And f2[i]it is also mentioned in the text.)

These can be obtained with the shortest path to solve this problem, I used the SPFA.

Code:

const int N = 110;

int n, m; 
int b1[N], b2[N], f1[N], f2[N], TiMe[N];
int dis[1 << 21];

inline void read(int &x) 
{
    char ch = getchar();
    while(ch < '0' || ch > '9')
		ch = getchar();
    x = ch - 48; ch = getchar();
    while(ch >= '0' && ch <= '9') 
	{
        x = x * 10 + (ch - 48);
        ch=getchar();
    }
}

queue <int> que;
bool vis[1 << 21];

void SPFA()
{
	memset(dis, 0x7f, sizeof dis);
	dis[(1 << n) - 1] = 0;
	que.push((1 << n) - 1);
	while (!que.empty())
	{
		int u = que.front();que.pop();
		for (int i = 1; i <= m; i++)
		{
			if((u & b1[i]) == b1[i] && (u & b2[i]) == 0)
			{
				int v = ((u | f1[i]) ^ f1[i]) | f2[i];
				if(dis[u] + TiMe[i] < dis[v])
				{
					dis[v] = dis[u] + TiMe[i];
					if(!vis[v])
					{
						que.push(v);
						vis[v] = 1;
					}
				}
			}
		}
		vis[u] = 0;
	}
}

int main()
{
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	read(n), read(m);
	for (int i = 1; i <= m; i++)
	{
		read(TiMe[i]);
	    char ch = getchar();
	    while(ch != '+' && ch != '-' && ch != '0')
			ch = getchar();
		for(int j = 1; j <= n; ++j)
		{
			if(ch == '+')
				b1[i] += 1 << (j - 1);
			if(ch == '-')
				b2[i] += 1 << (j - 1);
			ch = getchar();	
		}
	    while(ch != '+' && ch != '-' && ch != '0')
			ch = getchar();
		for(int j = 1; j <= n; ++j)
		{
			if(ch == '-')
				f1[i] += 1 << (j - 1);
			if(ch == '+')
				f2[i] += 1 << (j - 1);
			ch = getchar();	
		}
	}
	SPFA();
	if (dis[0] == dis[(1<<21)-1]) puts("0");
	else printf("%d\n", dis[0]); 
    return 0;
}

Guess you like

Origin www.cnblogs.com/GJY-JURUO/p/12596048.html