Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

STL优先队列+BFS 为什么TLE呢

Posted by feir8510 at 2010-01-12 15:50:50 on Problem 2312
不知道是不是STL慢?还是自己的代码有问题?还有什么地方应该优化呢 ,大家给看看吧。

//STL 优先队列
#include<iostream>
#include<queue>
using namespace std;
struct point
{
    int row;
    int col;
    int d;
}start,end;
struct cmp
{
    bool operator()(const struct point &a,const struct point &b)
    {
            if(a.d>b.d)
                return 1;
            else
                return 0;
 
    }
};
typedef priority_queue< struct point, vector<struct point>, cmp > qu;
qu q;
int row,col;
char map[301][301];
int visit[301][301];
int dd[301][301];
int direct[][2]={{0,1},{1,0},{0,-1},{-1,0},};
int BFS(struct point start)
{
    struct point temp;
    struct point newp;
    int ii,jj;
    int i;
    int noway=0;
    q.push(start);
    while(q.size())
    {
            temp=q.top();
            visit[temp.row][temp.col]=1;
            q.pop();
            if(map[temp.row][temp.col]=='T')
            {
                cout<<temp.d<<endl;
                noway=1;
                break;
            }
            for(i=0;i<4;i++)
            {
                ii=temp.row+direct[i][0];
                jj=temp.col+direct[i][1];
                if(ii>=0&&jj>=0&&ii<row&&jj<col&&!visit[ii][jj]&&map[ii][jj]!='R'&&map[ii][jj]!='S')
                {
                    if(map[ii][jj]=='B')
                        newp.d=temp.d+2;
                    else
                        newp.d=temp.d+1;
                    if(newp.d>dd[ii][jj])continue;
                    else dd[ii][jj]=newp.d;
                    newp.row=ii;
                    newp.col=jj;
                    q.push(newp);
                }
            }
    }
    if(!noway)cout<<"-1"<<endl;
    return 0;
}
int Init()
{
    int i,j;
    for(i=0;i<301;i++)
    {
        for(j=0;j<301;j++)
        {
            visit[i][j]=0;
            dd[i][j]=0xffffff;
        }
        
    }
    return 0;
}
int main()
{
    
    int i,j;
    while(cin>>row>>col)
    {
        if(row==0&&col==0)break;
        Init();
        for(i=0;i<row;i++)
            cin>>map[i]; 
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
            {
                if(map[i][j]=='Y')
                {
                    start.row=i;
                    start.col=j;
                    start.d=0;
                    BFS(start);  
                }
            }
        }
             
    }
    return 0;
}
 

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator