CCF 最优配餐【暴力BFS】

0 0 一直没敢下手做,而且还想了好久。。因为觉得数据有点大。。?而且还被状压带跑偏了。。。。一直想着用DP或者网络流做。。直到我搜到了大神的题解。。。。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;
#define maxn 1100
#define ll long long
class node
{
	public:
		int flag; // 1-->shop 2--> buyer 0--> none -1-->can't move
		int x,y; // buger need;
		ll cost,val;
		node(){
			flag=0,val=0;
		}
		node(int xx,int yy,int f):x(xx),y(yy),flag(f){
		}
		void set(int xx,int yy,int f,int v){
			x=xx,y=yy,flag=f,val=v;
		}
}a[maxn][maxn];
int vis[maxn][maxn];
int n,m,k,d;
queue<node>que;
int cmpx[4]={1,-1,0,0};
int cmpy[4]={0,0,1,-1};
bool check(int x,int y)
{
	if(x<=0||y<=0||x>n||y>n) return false;
	if(a[x][y].flag==-1||vis[x][y]) return false;
	return true;
}
int main()
{
	while(scanf("%d%d%d%d",&n,&m,&k,&d)!=EOF)
	{
		memset(vis,0,sizeof(vis));
		ll ans=0;
		int x,y;ll z;
		for(int i=0;i<m;i++)
		{
			scanf("%d%d",&x,&y);
			a[x][y].x=x,a[x][y].y=y,a[x][y].flag=1;
			que.push(a[x][y]);
		}
		for(int i=0;i<k;i++)
		{
			scanf("%d%d%lld",&x,&y,&z);
			a[x][y].x=x,a[x][y].y=y,a[x][y].flag=2;
			a[x][y].cost+=z;
		}
		for(int i=0;i<d;i++)
		{
			scanf("%d%d",&x,&y);
			a[x][y].x=x,a[x][y].y=y,a[x][y].flag=-1;
		}
		while(!que.empty())
		{
			node tmp=que.front();
			que.pop();
			int nx=tmp.x,ny=tmp.y,val=tmp.val;
			vis[nx][ny]=1;
		//	printf("x=%d y=%d\n",nx,ny);system("pause");
			for(int i=0;i<4;i++)
			{
				int tx=nx+cmpx[i],ty=ny+cmpy[i];
				if(!check(tx,ty)) continue;
				a[tx][ty].x=tx,a[tx][ty].y=ty,a[tx][ty].val=val+1;
				vis[tx][ty]=1;
				que.push(a[tx][ty]);
				if(a[tx][ty].flag==2)
					ans+=a[tx][ty].cost*(val+1);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值