1325. Dirt

http://acm.timus.ru/problem.aspx?space=1&num=1325

在求最短路的时候 如果既可以用bfs 又可以用 spfa 那么一般bfs的效率要高 因为spfa需要多次更新 而bfs只需更新一遍

比如说 在点与点之间是一单位长度的话 这时候用 bfs一遍就可以 用spfa的话有可能不止一遍

但是如果点与点之间的距离不是一单位长度或者更复杂的话 bfs就不适用了 这时候就得用spfa 了

用bfs的时候 用优先队列优化一下就很容易写了

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<cmath>
#define LL long long
using namespace std;

const int N=505;
const int INF=0x3f3f3f3f;
//typedef pair<int,int>point;
struct node
{
    int x,y;
    int l;
    const bool operator <(node a)const
    {
        return l>a.l;
    }
};
priority_queue<node>qt;
string path[N];
int dist1[N][N],dist2[N][N];
int X[]={-1,-1,-1,0,0,1,1,1};
int Y[]={-1,0,1,-1,1,-1,0,1};
int n,m;
void bfs2(int x1,int y1)
{
    memset(dist2,-1,sizeof(dist2));
    node a,b;
    a.x=x1;
    a.y=y1;
    a.l=0;
    qt.push(a);
    dist2[a.x][a.y]=0;
    while(!qt.empty())
    {
        a=qt.top();
        qt.pop();
        //cout<<a.x<<" "<<a.y<<" "<<a.l<<endl;
        for(int i=0;i<8;++i)
        {
            int l1=a.x+X[i];
            int l2=a.y+Y[i];
            if(l1>=0&&l1<n&&l2>=0&&l2<m&&path[l1][l2]!='0'&&dist2[l1][l2]==-1)
            {
                int tmp=(path[a.x][a.y]==path[l1][l2])?0:1;
                b.x=l1;
                b.y=l2;
                b.l=a.l+tmp;
                dist2[l1][l2]=b.l;
                qt.push(b);
            }
        }
    }
}
void bfs1(int x1,int y1)
{
    memset(dist1,-1,sizeof(dist1));
    node a,b;
    a.x=x1;
    a.y=y1;
    a.l=1;
    qt.push(a);
    dist1[a.x][a.y]=1;
    while(!qt.empty())
    {
        a=qt.top();
        qt.pop();
        for(int i=0;i<8;++i)
        {
            int l1=a.x+X[i];
            int l2=a.y+Y[i];
            if(l1>=0&&l1<n&&l2>=0&&l2<m&&path[l1][l2]!='0'&&dist1[l1][l2]==-1)
            {
                int tmp=(path[a.x][a.y]==path[l1][l2])?0:1;
                if(dist2[a.x][a.y]+tmp!=dist2[l1][l2])
                continue;
                b.x=l1;
                b.y=l2;
                b.l=a.l+1;
                dist1[l1][l2]=b.l;
                qt.push(b);
            }
        }
    }
}
int main()
{
    while(cin>>n>>m)
    {
        int stx,sty,ndx,ndy;
        cin>>stx>>sty;
        cin>>ndx>>ndy;
        --stx;--sty;--ndx;--ndy;
        for(int i=0;i<n;++i)
        cin>>path[i];
        bfs2(stx,sty);
        if(dist2[ndx][ndy]==-1)
        printf("0 0\n");
        else
        {
            bfs1(stx,sty);
            printf("%d %d\n",dist1[ndx][ndy],dist2[ndx][ndy]);
        }
    }
    return 0;
}

  

 

转载于:https://www.cnblogs.com/liulangye/archive/2013/01/18/2866516.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值