(luogu)P4011 孤岛营救问题

硬是没看出来网络流和SPFA算法的写法
正解:SPFA(最短路)+分层图
我的乱搞算法:BFS+二进制状态压缩
考试的时候脑糊了,毕竟调了一上午,自闭了
巨坑:同一个单元格可能有多把钥匙 XSL
蒟蒻的乱搞代码

#include<bits/stdc++.h>
#define re register int
using namespace std;
int n,m,p,k,s;
int MAP[11][11][11][11],key[11][11],vis[11][11][(1<<11)];
struct node
{
	int a,b,tot,k;
};
const int dx[5]={0,1,-1,0,0},dy[5]={0,0,0,1,-1};
queue<node>q;
int read()
{
    int ans = 0;
    char w = ' ', ch = getchar();
    while(ch < '0' || ch > '9')
    {
        w = ch;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        ans = (ans << 3) + (ans << 1);
        ans += ch - '0';
        ch = getchar();
    }
    return (w == '-' ? -ans : ans);
}
bool check(int a1,int a2)
{
	return (a1>=1 && a1<=n && a2>=1 && a2<=m);
}
int BFS()
{
	q.push(node{1,1,0,key[1][1]});
	vis[1][1][key[1][1]]=1;
	while(!q.empty())
	{
		node now=q.front();
		q.pop();
		if(now.a==n && now.b==m)
		{
			return now.tot;
		}
		for(re i=1;i<=4;i++)
		{
			int f1=now.a+dx[i];
			int f2=now.b+dy[i];
			if(check(f1,f2))
			{
				if(MAP[now.a][now.b][f1][f2]==-1)
				{
					continue;
				}
				if(MAP[now.a][now.b][f1][f2]!=0)
				{
					if((now.k&(1<<MAP[now.a][now.b][f1][f2]-1))==0)
					{
						continue;
					}
				}
				int tt=(now.k|key[f1][f2]);
				if(!vis[f1][f2][tt])
				{
					vis[f1][f2][tt]=1;
					q.push(node{f1,f2,now.tot+1,tt});
				}
			}
		}
	}
	return -1;
}
int main()
{
	int a1,a2,b1,b2,g;
	n=read();m=read();p=read();k=read();
	for(re i=1;i<=k;i++)
	{
		a1=read();a2=read();b1=read();b2=read();g=read();
		if(g==0) MAP[a1][a2][b1][b2]=MAP[b1][b2][a1][a2]=-1;
		else MAP[a1][a2][b1][b2]=MAP[b1][b2][a1][a2]=g;
	}
	s=read();
	for(re i=1;i<=s;i++)
	{
		a1=read();a2=read();g=read();
		key[a1][a2]|=(1<<g-1);
	}
	cout<<BFS();
	return 0;
}

posted @ 2019-08-25 21:35  Leinsea  阅读(101)  评论(0编辑  收藏  举报
Live2D //博客园自带,可加可不加