CCF-CSP 201409-4 最优配餐(BFS)

来源:互联网 发布:手机淘宝社区首页 编辑:程序博客网 时间:2024/04/30 03:07

问题描述

  栋栋最近开了一家餐饮连锁店,提供外卖服务。随着连锁店越来越多,怎么合理的给客户送餐成为了一个急需解决的问题。
  栋栋的连锁店所在的区域可以看成是一个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 <algorithm>  #include <vector>#include <memory.h>#include <queue>#include <fstream>using namespace std;class Pt{public:    int x;    int y;    Pt(int xx = 0, int yy = 0) :x(xx), y(yy){};};class Custom{public:    int x;    int y;    int n;    Custom(int xx = 0, int yy = 0, int nn =0) :x(xx), y(yy), n(nn){};};const int inf = 0x3f3f3f3f;const int maxn = 1001;bool vld[maxn][maxn]; //标记点是否可通过bool vsd[maxn][maxn]; //标记点是否已到达int dist[maxn][maxn]; //距离喽~vector<Pt> shops; //店的坐标vector<Custom> ctms; //用户坐标、需求int n;int step; //当前距离// 入队操作void Enqueue(int x, int y, queue<Pt> & q){    if (!vsd[x][y] && vld[x][y]) //入队前提:有效点且没有入过队    {        q.push(Pt(x, y));        if (dist[x][y] > step)            dist[x][y] = step;        vsd[x][y] = true;    }}// 多个起点开始的BFSvoid bfs(){    memset(vsd, 0, sizeof(vsd));    queue<Pt> sq;    Pt p;    for (int i = 0; i < shops.size(); i++) //先把所有shops加入队列    {        sq.push(shops[i]);        dist[shops[i].x][shops[i].y] = 0;        vsd[shops[i].x][shops[i].y] = true;    }    while (!sq.empty())    {        queue<Pt> q = sq;        while (!sq.empty())            sq.pop();        step++;        while (!q.empty())        {            p = q.front();            q.pop();                    if (p.x > 1)                Enqueue(p.x - 1, p.y, sq);            if (p.y > 1)                Enqueue(p.x, p.y - 1, sq);            if (p.x < n)                Enqueue(p.x + 1, p.y, sq);            if (p.y < n)                Enqueue(p.x, p.y + 1, sq);        }    }}int main(){    //ifstream cin("a.txt");    int m, k, d;    int x, y, z;    memset(vld, true, sizeof(vld));    memset(dist, inf, sizeof(dist));    step = 0;    cin >> n >> m >> k >> d;    for (int i = 0; i < m; i++)    {        cin >> x >> y;        shops.push_back(Pt(x, y));    }    for (int i = 0; i < k; i++)    {        cin >> x >> y >> z;        ctms.push_back(Custom(x, y, z));    }    for (int i = 0; i < d; i++)    {        cin >> x >> y;        vld[x][y] = false;    }    bfs();    long long cost = 0; // int会越界, 必须long long, 这里是个坑。。    for (int i = 0; i < ctms.size(); i++)    {        x = ctms[i].x;        y = ctms[i].y;        cost += dist[x][y] * ctms[i].n;    }    cout << cost << endl;    //cin.close();    return 0;}
0 0
原创粉丝点击