您的位置:首页 > 其它

hdu 4198 写spfa不能忘了优化

2012-04-03 17:41 239 查看
写spfa不能忘了优化~没加优化,超时,加了优化,93ms!

或者用优先队列优化也可以,绝对比dijkstra快!

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;

#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif

#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a)         for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a)        for( int i = (a)-1 ; i >= 0 ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define SZ(a)           ((int)a.size())
#define PP(n,m,a)       puts("---");FF(i,n){FF(j,m)cout << a[i][j] << ' ';puts("");}
#define pb              push_back
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-10;
const double pi = acos(-1.0);
const int maxn = 511;
const int way[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

struct zz
{
    int x;
    int y;
}zx;

int T;
int n,m,d;
char a[maxn][maxn];
int sx,sy;
int dp[maxn][maxn];
bool hash[maxn][maxn];
int ans;
int nowx,nowy;
deque<zz>q;

inline bool yes(int x,int y)
{
    if(x>=1 && x<=n && y>=1 && y<=m)
    {
        return true;
    }
    return false;
}

void spfa()
{
    q.clear();
    zx.x = sx;
    zx.y = sy;
    q.push_back(zx);
    hash[sx][sy] = true;
    zz now;
    int temp;
    int x,y;
    int nowx,nowy;
    while(!q.empty())
    {
        now = q.front();
        q.pop_front();
        x = now.x;
        y = now.y;
        for(int i=0;i<4;i++)
        {
            nowx = x + way[i][0];
            nowy = y + way[i][1];
            if(yes(nowx,nowy))
            {
                if(a[nowx][nowy]=='#')
                {
                     continue;
                }
                if(a[nowx][nowy]=='@')
                {
                    temp = dp[x][y]+d+1;
                    if(dp[nowx][nowy] > temp)
                    {
                        dp[nowx][nowy] = temp;

                        if(!hash[nowx][nowy])
                        {
                            zx.x = nowx;
                            zx.y = nowy;
                            hash[nowx][nowy] = true;
                            if(!q.empty() && temp<dp[q.front().x][q.front().y])
                            {
                                q.push_front(zx);
                            }
                            else
                            {
                                q.push_back(zx);
                            }
                        }
                    }
                }
                else if(a[nowx][nowy]=='.')
                {
                    temp = dp[x][y]+1;
                    if(dp[nowx][nowy] > temp)
                    {
                        dp[nowx][nowy] = temp;
                        if(!hash[nowx][nowy])
                        {
                            zx.x = nowx;
                            zx.y = nowy;
                            hash[nowx][nowy] = true;
                            if(!q.empty() && temp < dp[q.front().x][q.front().y])
                            {
                                q.push_front(zx);
                            }
                            else
                            {
                                q.push_back(zx);
                            }
                        }
                    }
                }
            }
            else
            {
                temp = dp[x][y]+1;
                ans = min(ans,temp);
            }
        }
        hash[x][y] = false;
    }
    return ;
}

int start()
{
    FF(i,n+1) FF(j,m+1)
    {
        hash[i][j] = false;
        dp[i][j] = inf;
    }
    ans = inf;
    dp[sx][sy] = 0;
    spfa();
    return ans;
}

int main()
{
    cin>>T;
    char ss[1000];
    while(T--)
    {
        SS(n);
        SS(m);
        SS(d);

        FOR(i,1,n)
        {
            scanf("%s",ss);
            for(int j=1;j<=m;j++)
            {
                a[i][j] = ss[j-1];
            }
            FOR(j,1,m)
            {
                if(a[i][j]=='S')
                {
                    sx = i;
                    sy = j;
                }
            }
        }
        printf("%d\n",start());
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: