ccf(最优配餐)

版权声明:欢迎转载!拒绝抄袭. https://blog.csdn.net/qq_36257146/article/details/83021900

问题描述

试题编号: 201409-4
试题名称: 最优配餐
时间限制: 1.0s
内存限制: 256.0MB
问题描述:

问题描述

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


  送餐的主要成本体现在路上所花的时间,每一份餐每走一个单位的距离需要花费1块钱。每个客户的需求都可以由栋栋的任意分店配送,每个分店没有配送总量的限制。
  现在你得到了栋栋的客户的需求,请问在最优的送餐方式下,送这些餐需要花费多大的成本。

输入格式

  输入的第一行包含四个整数n, m, k, d,分别表示方格图的大小、栋栋的分店数量、客户的数量,以及不能经过的点的数量。
  接下来m行,每行两个整数xi, yi,表示栋栋的一个分店在方格图中的横坐标和纵坐标。
  接下来k行,每行三个整数xi, yi, ci,分别表示每个客户在方格图中的横坐标、纵坐标和订餐的量。(注意,可能有多个客户在方格图中的同一个位置)
  接下来d行,每行两个整数,分别表示每个不能经过的点的横坐标和纵坐标。

输出格式

  输出一个整数,表示最优送餐方式下所需要花费的成本。

样例输入

10 2 3 3
1 1
8 8
1 5 1
2 3 3
6 7 2
1 2
2 2
6 8

样例输出

29

评测用例规模与约定

  前30%的评测用例满足:1<=n <=20。
  前60%的评测用例满足:1<=n<=100。
  所有评测用例都满足:1<=n<=1000,1<=m, k, d<=n^2。可能有多个客户在同一个格点上。每个客户的订餐量不超过1000,每个客户所需要的餐都能被送到。

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
#include <string.h>
#define maxn 1003

using namespace std;
struct Node
{
	int x,y,step;
	Node(){}
	Node(int a,int b,int c)
	{
		x = a;
		y = b;
		step = c;
	}
};

struct direct
{
	int x,y;
} direct[4] = {{-1,0},{0,-1},{1,0},{0,1}};

int n;//方格图的大小、。
int m;//栋栋的分店数量、
int k;//客户的数量,
int d;//以及不能经过的点的数量
int buys[maxn][maxn];//货物量的分配
bool visited[maxn][maxn];//访问地图
int buyers; //顾客数量
long long ans;//答案
queue<Node>q; //队列

bool legal(int x,int y)
{
	return x>=1&&x<=n&&y>=1&&y<=n;
}


void BFS(int num)
{
	int guest = 0;
	Node t;
	while(!q.empty())
	{
		t = q.front();
		q.pop();
		for(int i = 0; i<4; i++)
		{
			int t_x = t.x+direct[i].x;
			int t_y = t.y+direct[i].y;
			int t_step = t.step;
			if(!legal(t_x,t_y))
			{
				continue;
			}
			if(!visited[t_x][t_y])
			{
				t_step++;
				if(buys[t_x][t_y])
				{
					ans+=t_step*buys[t_x][t_y];
					guest++;
					if(guest==num)
						return;
				}
				visited[t_x][t_y] = 1;
				q.push(Node(t_x,t_y,t_step));
			}
		}
	}
}

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv)
{
	int i,j,k;
	int x,y,c;
	cin>>n>>m>>k>>d;
	//初始化
	memset(visited,0,sizeof(visited));
	memset(buys,0,sizeof(buys));
	//分店的初始化
	for(i = 0; i<m; i++)
	{
		cin>>x>>y;
		q.push(Node(x,y,0));
		visited[x][y] = 1;
	}
	//客户的初始化
	for(i = 0; i<k; i++)
	{
		cin>>x>>y>>c;
		if(buys[x][y]==0)//合并在同一地点的用户
		{
			buyers++;
		}
		buys[x][y] +=c;
	}
	for(i = 0;i<d;i++)
	{
		cin>>x>>y;
		visited[x][y] = 1;
	}
	BFS(buyers);
	cout<<ans;
	return 0;
}
/**
10 2 4 3
 1 1
 8 8
 1 5 1
 1 5 2
 2 3 3
 6 7 2
 1 2
 2 2
 6 8
 **/

猜你喜欢

转载自blog.csdn.net/qq_36257146/article/details/83021900