ccf_201409-4_最优配餐

试题编号: 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 <queue>
using namespace std;
class Point{//坐标点
public:
    int x,y,v,d;
    Point() = default;
}cost_map[1010][1010];
class Customer:Point{//顾客
public:
    int x,y,c;
    Customer(int x,int y,int c): Point(),x(x),y(y),c(c){}
};
class Store:Point{//分店
public:
    int x,y;
    Store(int x,int y):Point(),x(x),y(y){}
};
void init_map(int n){//初始化地图
    for (int i = 0; i < n + 2; ++i) {
        for (int j = 0; j < n + 2; ++j) {
            cost_map[i][j].x = i;//可省
            cost_map[i][j].y = j;//可省
            cost_map[i][j].d = 0;
            if(i == 0 || j == 0 || i == n + 1 || j == n + 1)//边界处理
                cost_map[i][j].v = 1;
            else cost_map[i][j].v = 0;
        }
    }
}
void create_map(const vector<Store>& s,int n) {//生成“消耗图”(多源广度优先搜索)
    queue<Point> que;
    for (auto & i : s) {
        cost_map[i.x][i.y].d = 0;
        cost_map[i.x][i.y].v = 1;
        que.push(cost_map[i.x][i.y]);
    }
    for (int i = 0; i < (int)s.size(); ++i){
        while (!que.empty()) {
            Point u = que.front();
            que.pop();
            Point *v[4];//上下左右四条路
            v[0] = &cost_map[u.x + 1][u.y];
            v[1] = &cost_map[u.x][u.y + 1];
            v[2] = &cost_map[u.x - 1][u.y];
            v[3] = &cost_map[u.x][u.y - 1];
            for (auto it : v)
                if (!it->v) {//障碍物、边界和已发现的点的v为1,不满足条件
                    it->v = 1;
                    it->d = u.d + 1;
                    que.push(*it);
                }
        }
    }
}
int main(){
    vector<Store> stores;
    vector<Customer> customers;
    int n,m,k,d;
    long long cost = 0;
    cin>>n>>m>>k>>d;
    
    init_map(n);//初始化图
    
    for (int i = 0; i < m; ++i) {
        int x,y;
        cin>>x>>y;
        stores.emplace_back(x,y);
    }
    for (int i = 0; i < k; ++i) {
        int x,y,c;
        cin>>x>>y>>c;
        customers.emplace_back(x,y,c);
    }
    for (int i = 0; i < d; ++i) {
        int x,y;
        cin>>x>>y;
        cost_map[x][y].v = 1;//设置障碍物
    }
    
    create_map(stores,n);//生成消耗图
    
    for (auto c: customers) {
        cost += cost_map[c.x][c.y].d*c.c;
    }
    cout<<cost;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值