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

why tle...

Posted by ccl0326 at 2009-10-07 14:02:03 on Problem 3026
#include <iostream>
#include <fstream>
#include <list>
#include <vector>
using namespace std;
ifstream fin("t3026.in");

struct node
{
 int x,y;
};
struct node_
{
 int x,y;
 int step;
};
vector<node> vec;
list<node_> b_vec;
const int MAXN=200;
int n;
int x,y;
char ch[MAXN][MAXN];
bool hash[MAXN][MAXN];
bool hash_[MAXN];
int id[MAXN][MAXN];
int edge[MAXN][MAXN];
int dist[MAXN];
int v1[4]={0,0,1,-1},v2[4]={1,-1,0,0};
const int INF=0x7FFFFFFF;
void bfs(int ix,int iy)
{
     while(b_vec.size()>0)
     {
      node_ p=b_vec.front();
      int x=p.x;
      int y=p.y;
      int step=p.step;
      hash[x][y]=true;
      for (int i=0;i<4;i++)
      {
         
         int x_=x+v1[i],y_=y+v2[i];
         if (x_<1||y_<1||x>::y||y>::x) continue;
         if ('#'==ch[x_][y_]) continue;
         if (hash[x_][y_]) continue;

         if (' '==ch[x_][y_]) {node_ p_;p_.x=x_;p_.y=y_;p_.step=step+1;b_vec.push_back(p_);}
         if ('A'==ch[x_][y_]) {node_ p_;p_.x=x_;p_.y=y_;p_.step=step+1;edge[id[ix][iy]][id[x_][y_]]=p_.step;b_vec.push_back(p_);}
      }
      b_vec.pop_front();
     }
     
}
void init()
{
     memset(edge,0,sizeof(edge));
     for (int i=0;i<vec.size();i++)
     {
         node p=vec.at(i);
         memset(hash,0,sizeof(hash));
         node_ p_;
         p_.x=p.x;
         p_.y=p.y;
         p_.step=0;
         b_vec.clear();
         b_vec.push_back(p_);
    //     cout<<p.x<<" "<<p.y<<endl;
         bfs(p.x,p.y);
     }
     

     
}
void prim()
{
  memset(hash_,0,sizeof(hash_));
  int n_=vec.size();
  for (int i=0;i<n_;i++)
      dist[i]=INF;
  dist[0]=0;
  int answer=0;
  for (int i=0;i<n_;i++)
  {
      int min=INF;
      int u=-1;
      for (int j=0;j<n_;j++)
      {
          if (hash_[j]) continue;
          if (min>dist[j]) {min=dist[j];u=j;}
      }
      hash_[u]=true;
      answer+=dist[u];
      for (int j=0;j<n_;j++)
      {
          if (dist[j]>edge[u][j]) dist[j]=edge[u][j];
      }
  }

  cout<<answer<<endl;
  
}
int main()
{
    cin>>n;
    while(n--)
    {
       vec.clear();
       cin>>x>>y;
       getchar();
       for (int i=1;i<=y;i++)
       {
        for (int j=1;j<=x;j++)
        {
            ch[i][j]=getchar();

            if ('S'==ch[i][j]) ch[i][j]='A';
            if ('A'==ch[i][j]) {node n_;n_.x=i;n_.y=j;vec.push_back(n_);id[i][j]=vec.size()-1;}
        
        }
        while (getchar() != '\n');
       }
       init();
       prim();
    }
    return 0;
}



超的我没想法了.....orz..谁帮帮忙吧-.-..我都虚脱了

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