CCF201409-4 最优配餐 简单bfs

问题描述
  栋栋最近开了一家餐饮连锁店,提供外卖服务。随着连锁店越来越多,怎么合理的给客户送餐成为了一个急需解决的问题。
  栋栋的连锁店所在的区域可以看成是一个n×n的方格图(如下图所示),方格的格点上的位置上可能包含栋栋的分店(绿色标注)或者客户(蓝色标注),有一些格点是不能经过的(红色标注)。
  方格图中的线表示可以行走的道路,相邻两个格点的距离为1。栋栋要送餐必须走可以行走的道路,而且不能经过红色标注的点。


  送餐的主要成本体现在路上所花的时间,每一份餐每走一个单位的距离需要花费1块钱。每个客户的需求都可以由栋栋的任意分店配送,每个分店没有配送总量的限制。

  现在你得到了栋栋的客户的需求,请问在最优的送餐方式下,送这些餐需要花费多大的成本。



#define MAX_SIZE 1000+5
#define INF 0x3f3f3f3f

#include<iostream>
#include<stack>
#include<string.h>
#include<queue>
using namespace std;


int map[MAX_SIZE][MAX_SIZE];
int cusMap[MAX_SIZE][MAX_SIZE];
int mmp[MAX_SIZE][MAX_SIZE];

const int dir[4][2] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };

struct po{
	int x, y, step;
};

queue<po> poi;
int n, m, k, d;

int fg[MAX_SIZE][MAX_SIZE];

void bfs(){
	memset(fg, 0, sizeof(fg));
	while (!poi.empty()){
		po p = poi.front(); poi.pop();
		for (int i = 0; i < 4; i++){
			int nx = p.x + dir[i][0];
			int ny = p.y + dir[i][1];
			if (nx > 0 && ny > 0 && nx <= n && ny <= n  && map[nx][ny] >= 0){
				po nnp; nnp.x = nx; nnp.y = ny; nnp.step = p.step + 1;
				if (mmp[nx][ny] > nnp.step){
					mmp[nx][ny] = nnp.step;
					poi.push(nnp); 
				}
				fg[nx][ny] = 1;
			}
		}
	}
}

int main(){
	cin >> n >> m >> k >> d;
	for (int i = 0; i < m; i++){
		int x, y;
		cin >> x >> y;
		po p; p.x = x; p.y = y; p.step = 0;
		poi.push(p); mmp[x][y] = 0;
	}
	for (int i = 0; i < k; i++){
		int x, y, c;
		cin >> x >> y >> c;
		cusMap[x][y] += c;
	}
	for (int i = 0; i < d; i++){
		int x, y;
		cin >> x >> y;
		map[x][y] = -1;
	}
	long long ans = 0;
	memset(mmp, INF, sizeof(mmp));
	bfs();
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= n; j++){
			if (cusMap[i][j] > 0){
				ans += cusMap[i][j] * mmp[i][j];
			}
		}
	}
	cout << ans << endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值