CCF 201409-4 最优配餐

具体代码:

#include <iostream>
#include <queue>
using namespace std;
const int maxn = 1005;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, k, d, x, y, c, ans, dis[maxn][maxn];
bool vis[maxn][maxn];
struct node
{
    int x, y, c;
} a[maxn * maxn];
queue<pair<int, int>> q;
bool check(int x, int y)
{
    if (x >= 1 && x <= n && y >= 1 && y <= n && vis[x][y] == false)
        return true;
    return false;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n >> m >> k >> d;
    for (int i = 1; i <= m; ++i) // 输入分店的位置
    {
        cin >> x >> y;
        q.push(make_pair(x, y)); // 将每个分店都加入队列中,同时进行BFS
        vis[x][y] = true;
        dis[x][y] = 0;
    }
    for (int i = 1; i <= k; ++i) // 输入客户的位置
    {
        cin >> a[i].x >> a[i].y >> a[i].c;
    }
    for (int i = 1; i <= d; ++i) // 输入不能经过的点
    {
        cin >> x >> y;
        vis[x][y] = 1;
    }
    while (!q.empty())
    {
        pair<int, int> p = q.front();
        q.pop();
        for (int i = 0; i < 4; ++i)
        {
            if (check(p.first + dx[i], p.second + dy[i]))
            {
                vis[p.first + dx[i]][p.second + dy[i]] = true;
                dis[p.first + dx[i]][p.second + dy[i]] = dis[p.first][p.second] + 1;
                q.push(make_pair(p.first + dx[i], p.second + dy[i]));
            }
        }
    }
    for (int i = 1; i <= k; ++i)
    {
        ans += dis[a[i].x][a[i].y] * a[i].c;
    }
    cout << ans << endl;

    system("pause");
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值