201409-4 最优配餐 多源bfs

#include <iostream>
#include <queue>
using namespace std;
const int maxn = 1e3+10;
typedef long long int ll;
struct Node{
    
    
    int x,y,dis;
    Node(){
    
    };
    Node(int _x,int _y,int _dis)
    {
    
    
        this->x = _x;
        this->y = _y;
        this->dis = _dis;
    }
};
Node node;
int Mgraph[maxn][maxn];
int order_graph[maxn][maxn];
int n,m,k,d;
int x[4] = {
    
    0,0,1,-1};
int y[4] = {
    
    1,-1,0,0};
queue<Node> q;
ll ans = 0;

void bfs()
{
    
    
    while(!q.empty())
    {
    
    
        node = q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
    
    
            int xx = node.x + x[i];
            int yy = node.y + y[i];
            if((xx>=1&&xx<=n)&&(yy>=1&&yy<=n))
            {
    
    
                if(Mgraph[xx][yy] == 0)
                {
    
    
                    int dis_ = node.dis + 1;
                    Mgraph[xx][yy] = 1;
                    ans += order_graph[xx][yy] * dis_;
                    q.push(Node(xx,yy,dis_));
                }
            }
        }
    }
}
int main()
{
    
    
    int i,order_x,order_y,order_num,ban_x,ban_y;
    cin>>n>>m>>k>>d;
    for(i=0;i<m;i++)
    {
    
    
        cin>>node.x>>node.y;
        node.dis = 0;
        q.push(node);
    }
    for(i=0;i<k;i++)
    {
    
    
        cin>>order_x>>order_y>>order_num;
        order_graph[order_x][order_y] = order_num;
    }
    for(i=0;i<d;i++)
    {
    
    
        cin>>ban_x>>ban_y;
        Mgraph[ban_x][ban_y] = 1;
    }
    bfs();
    cout<<ans;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44142774/article/details/114375339