hdu5253-连接的管道(优先队列+bfs)

152 篇文章 0 订阅
46 篇文章 0 订阅

思路:

优先队列+bfs 走过的标记掉。

第一次判断标记仍在前面了结果TLE了。。vis放在判断里,不要仍进队列就过来。


#include <iostream>
#include <stdio.h>
#include <queue>
#include <cstring>
using namespace std;
int n,m;
int mp[1005][1005];
int vis[1005][1005];
long long ans;
struct node
{
    int x,y;
    int res;
    bool operator < (const node &a) const {
        return res>a.res; 
    }
};
bool judge(int x,int y)
{
    if(x<1||x>n||y<1||y>m||vis[x][y])
        return false;
    return true;
}
void bfs(node t)
{
    priority_queue<node>q;
    q.push(t);
    int cnt=n*m;
    while(!q.empty())
    {           
        t=q.top();       
        q.pop();         
        if(vis[t.x][t.y])
        {
            continue;
        }
        if(cnt==0)
            break;
        cnt--;
        vis[t.x][t.y]=1;
        ans+=t.res;
        int to[4][2]={0,1,0,-1,1,0,-1,0};
        for(int i=0;i<4;i++)
        {
            node c=t;
            c.x=t.x+to[i][0];
            c.y=t.y+to[i][1];
            if(judge(c.x,c.y))
            {
                c.res=mp[t.x][t.y]-mp[c.x][c.y];
                if(c.res<0)
                    c.res=-c.res;
                q.push(c);
            }
        }
    }
}
int main()
{
    int T;
    cin>>T;
    for(int k=1;k<=T;k++)
    {
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
                scanf("%d",&mp[i][j]);
        }
        ans=0;
        node t;
        t.res=0,t.x=1,t.y=1;
        bfs(t);
        printf("Case #%d:\n",k);
        printf("%lld\n",ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值